如何在yii2中将数组传递给gridview
how to pass the array to gridview in yii2
我正在使用 yii2 框架
我有从控制器到视图的数组。
public function actionProducts()
{
$product = Yii::$app->httpclient->get('http://localhost/MWSProducts/src/MarketplaceWebServiceProducts/Samples/GetMatchingProductSample.php');
$array1 = str_replace("ns2:","",$product);
$array=json_decode(json_encode(simplexml_load_string($array1)),true);
return $this->render('products',['array'=>$array]);
}
在上面的代码中,我将 xml 转换为数组。
$array 有数组,我将它传递给名为 products 的视图。现在我想在 Gridview 的视图中显示该数组,因为我是 yii2 的新手,我无法执行此操作,它说我必须使用一些数据提供程序,但我不知道如何执行此操作。
任何人都可以帮助我如何在 gridview 中显示数组。谢谢
感谢您的回答..
实际上它是一个亚马逊产品数组,我从产品 API 中获取它,我们不能定义任何预定义属性,如 id 和 all,因为每个产品都不同。这就是数组的样子。
Array
(
[GetMatchingProductResult] => Array
(
[0] => Array
(
[@attributes] => Array
(
[ASIN] => 0886467918
[status] => Success
)
[Product] => Array
(
[Identifiers] => Array
(
[MarketplaceASIN] => Array
(
[MarketplaceId] => A21TJRUUN4KGV
[ASIN] => 0886467918
)
)
[AttributeSets] => Array
(
[ItemAttributes] => Array
(
[Author] => Kipling, Rudyard
[Binding] => Hardcover
[Edition] => Har/Cas
[Format] => Import
[Label] => Imprint unknown
[Languages] => Array
(
[Language] => Array
(
[0] => Array
(
[Name] => english
[Type] => Published
)
[1] => Array
(
[Name] => english
[Type] => Original Language
)
[2] => Array
(
[Name] => english
[Type] => Unknown
)
)
)
[Manufacturer] => Imprint unknown
[PackageDimensions] => Array
(
[Weight] => 1.74
)
[ProductGroup] => Book
[ProductTypeName] => ABIS_BOOK
[PublicationDate] => 1988-05-02
[Publisher] => Imprint unknown
[SmallImage] => Array
(
[URL] => http://ecx.images-amazon.com/images/I/412CsE6Mb8L._SL75_.jpg
[Height] => 75
[Width] => 50
)
[Studio] => Imprint unknown
[Title] => Jungle Book ("Read Along")
)
)
[Relationships] => Array
(
)
[SalesRankings] => Array
(
[SalesRank] => Array
(
[0] => Array
(
[ProductCategoryId] => book_display_on_website
[Rank] => 709468
)
[1] => Array
(
[ProductCategoryId] => 1318084031
[Rank] => 14260
)
[2] => Array
(
[ProductCategoryId] => 1318083031
[Rank] => 47016
)
)
)
)
)
如果数组包含类似 dataProvider 的数据。您可以使用 arrayDataProvider http://www.yiiframework.com/doc-2.0/yii-data-arraydataprovider.html 例如(来自 yii2 指南):
$data = [
['id' => 1, 'name' => 'name 1', ...],
['id' => 2, 'name' => 'name 2', ...],
...
['id' => 100, 'name' => 'name 100', ...],
];
$provider = new ArrayDataProvider([
'allModels' => $data,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'attributes' => ['id', 'name'],
],
]);
数据提供者简要指南http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html
你的情况
public function actionProducts()
{
$product = Yii::$app->httpclient->get('http://localhost/MWSProducts/src/MarketplaceWebServiceProducts/Samples/GetMatchingProductSample.php');
$array1 = str_replace("ns2:","",$product);
$array=json_decode(json_encode(simplexml_load_string($array1)),true);
$provider = new ArrayDataProvider([
'allModels' => $array,
'pagination' => [
'pageSize' => 10,
],
]);
return $this->render('products',['array'=>$array]);
}
参考上面的示例,其中数组 $data 包含 id、name 并假设您的 arrayDataProvider 在 var $array 中并且在 gridview 中也有 id、name 您应该有
<?= GridView::widget([
'dataProvider' => $array,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
.....
['class' => 'yii\grid\ActionColumn',
'contentOptions' => ['style' => 'width:84px; font-size:18px;']],
],
]); ?>
我正在使用 yii2 框架
我有从控制器到视图的数组。
public function actionProducts()
{
$product = Yii::$app->httpclient->get('http://localhost/MWSProducts/src/MarketplaceWebServiceProducts/Samples/GetMatchingProductSample.php');
$array1 = str_replace("ns2:","",$product);
$array=json_decode(json_encode(simplexml_load_string($array1)),true);
return $this->render('products',['array'=>$array]);
}
在上面的代码中,我将 xml 转换为数组。 $array 有数组,我将它传递给名为 products 的视图。现在我想在 Gridview 的视图中显示该数组,因为我是 yii2 的新手,我无法执行此操作,它说我必须使用一些数据提供程序,但我不知道如何执行此操作。
任何人都可以帮助我如何在 gridview 中显示数组。谢谢
感谢您的回答..
实际上它是一个亚马逊产品数组,我从产品 API 中获取它,我们不能定义任何预定义属性,如 id 和 all,因为每个产品都不同。这就是数组的样子。
Array
(
[GetMatchingProductResult] => Array
(
[0] => Array
(
[@attributes] => Array
(
[ASIN] => 0886467918
[status] => Success
)
[Product] => Array
(
[Identifiers] => Array
(
[MarketplaceASIN] => Array
(
[MarketplaceId] => A21TJRUUN4KGV
[ASIN] => 0886467918
)
)
[AttributeSets] => Array
(
[ItemAttributes] => Array
(
[Author] => Kipling, Rudyard
[Binding] => Hardcover
[Edition] => Har/Cas
[Format] => Import
[Label] => Imprint unknown
[Languages] => Array
(
[Language] => Array
(
[0] => Array
(
[Name] => english
[Type] => Published
)
[1] => Array
(
[Name] => english
[Type] => Original Language
)
[2] => Array
(
[Name] => english
[Type] => Unknown
)
)
)
[Manufacturer] => Imprint unknown
[PackageDimensions] => Array
(
[Weight] => 1.74
)
[ProductGroup] => Book
[ProductTypeName] => ABIS_BOOK
[PublicationDate] => 1988-05-02
[Publisher] => Imprint unknown
[SmallImage] => Array
(
[URL] => http://ecx.images-amazon.com/images/I/412CsE6Mb8L._SL75_.jpg
[Height] => 75
[Width] => 50
)
[Studio] => Imprint unknown
[Title] => Jungle Book ("Read Along")
)
)
[Relationships] => Array
(
)
[SalesRankings] => Array
(
[SalesRank] => Array
(
[0] => Array
(
[ProductCategoryId] => book_display_on_website
[Rank] => 709468
)
[1] => Array
(
[ProductCategoryId] => 1318084031
[Rank] => 14260
)
[2] => Array
(
[ProductCategoryId] => 1318083031
[Rank] => 47016
)
)
)
)
)
如果数组包含类似 dataProvider 的数据。您可以使用 arrayDataProvider http://www.yiiframework.com/doc-2.0/yii-data-arraydataprovider.html 例如(来自 yii2 指南):
$data = [ ['id' => 1, 'name' => 'name 1', ...], ['id' => 2, 'name' => 'name 2', ...], ... ['id' => 100, 'name' => 'name 100', ...], ]; $provider = new ArrayDataProvider([ 'allModels' => $data, 'pagination' => [ 'pageSize' => 10, ], 'sort' => [ 'attributes' => ['id', 'name'], ], ]);
数据提供者简要指南http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html
你的情况
public function actionProducts()
{
$product = Yii::$app->httpclient->get('http://localhost/MWSProducts/src/MarketplaceWebServiceProducts/Samples/GetMatchingProductSample.php');
$array1 = str_replace("ns2:","",$product);
$array=json_decode(json_encode(simplexml_load_string($array1)),true);
$provider = new ArrayDataProvider([
'allModels' => $array,
'pagination' => [
'pageSize' => 10,
],
]);
return $this->render('products',['array'=>$array]);
}
参考上面的示例,其中数组 $data 包含 id、name 并假设您的 arrayDataProvider 在 var $array 中并且在 gridview 中也有 id、name 您应该有
<?= GridView::widget([
'dataProvider' => $array,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
.....
['class' => 'yii\grid\ActionColumn',
'contentOptions' => ['style' => 'width:84px; font-size:18px;']],
],
]); ?>