how to pass the array to gridview in yii2
If the array contain the data like a dataProvider . you can use arrayDataProvider http://www.yiiframework.com/doc-2.0/yii-data-arraydataprovider.html eg (from yii2 guide):
$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'], ], ]);
a brief guide to dataprovider http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html
in your case
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]);
}
Refer to sample above where the array $data contain id, name and suppose your arrayDataProvider is in the var $array and have id,name too in gridview you should have
<?= GridView::widget([
'dataProvider' => $array,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
.....
['class' => 'yii\grid\ActionColumn',
'contentOptions' => ['style' => 'width:84px; font-size:18px;']],
],
]); ?>
Comments
-
Salman Riyaz almost 2 years
im using yii2 framework
I have array coming from controller to the view.
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]); }
In the above code im converting xml to an array. The $array has the array, and im passing it to view called products. Now i want to display that array in the view in Gridview, Since im new to yii2 im unable to do this, it says i have to use some dataprovider, but im not getting how to do this.
Can anyone please help me in this how do i display the array in gridview. Thank you
Thanks for the answer..
Actually its a amazon product array im fetching it from the Product API, we cant define any predefine attributes like id and all, since its different for each product. this is how the array looks like.
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 ) ) ) ) )
-
Salman Riyaz almost 8 yearsOK what should i give in the view now?
-
ScaisEdge almost 8 yearsI have added a suggestion for griview
-
Salman Riyaz almost 8 yearsYeah i want in grid view itself, What if we dont have the id,name in the array? Because every array will have different attributes here. Then how i pass it
-
ScaisEdge almost 8 yearsconsider the possibility of a show different column based on the "type" of array .. you can set the column in proper $varsColumnArray .. and the set this to the column part of the gridview .. but this is another question ..
-
Jonyx4 about 2 yearsWorks perfect. But what about if you need to use a search model also?
-
ScaisEdge about 2 years@Jonyx4 if you need a search model .. then you should create a proper search function for manage properly the params you need .. foe example you could add a new searchFunction to an existing seacrhModel with the query you need.