【问题标题】:how to pass the array to gridview in yii2如何在yii2中将数组传递给gridview
【发布时间】:2016-07-04 06:30:11
【问题描述】:

我正在使用 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
                                            )

                                    )

                            )

                    )

            )

【问题讨论】:

    标签: php arrays gridview yii2 yii2-advanced-app


    【解决方案1】:

    如果数组包含 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'],
            ],
        ]);
    

    dataproviderhttp://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;']],
        ],
    ]); ?>
    

    【讨论】:

    • OK 我现在应该在视图中给出什么?
    • 我已经添加了 griview 的建议
    • 是的,我想要网格视图本身,如果我们没有 id,name 在数组中怎么办?因为每个数组在这里都会有不同的属性。那我怎么通过呢
    • 考虑根据数组的“类型”显示不同列的可能性..您可以在适当的 $varsColumnArray 中设置列​​..并将其设置为 gridview 的列部分..但这是另一个问题..
    • 完美运行。但是,如果您还需要使用搜索模型呢?
    猜你喜欢
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多