ArrayDataProvider gridview yii2

时间:2017-01-19 10:04:28

标签: yii2

我有这个

 public function actionIndex()
{
    $data = order::getSome();
    $dataProvider = new ArrayDataProvider([
        'allModels' => $data,
        'sort' => [
            'attributes' => ['paid']
        ]


    ]);
    return $this->render('index',['dataProvider' =>$dataProvider]);
}

如果在gridview中删除'columns'=> [,则会在数据中发布所有列。如果在'columns'=> [中放置了列的名称,则会出现“' 0' 0在所有列中。如何显示特定列?

`public static function getSome(){
     return   Yii::$app->getDb()->createCommand(
            "SELECT order_customFields.order_customFields_delivery_method,

    sum(case `order`.order_status when 'paid' then 1 else 0 end) paid,
    sum(case `order`.order_status when 'later' then 1 else 0 end) later,
  sum(case `order`.order_status when 'delivery-approved' then 1 else 0 end) deliveryapproved,

FROM order_customFields 
  INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
  order_customFields.order_customFields_order_date >= '2016-12-01' AND
  order_customFields.order_customFields_order_date <= '2016-12-31' 
AND order_customFields.order_customFields_delivery_method is not null
GROUP BY    
   order_customFields.order_customFields_delivery_method"
        )->queryAll();
    }`

2 个答案:

答案 0 :(得分:1)

根据您的样本,您可能正在使用activeRecord订单(而不是订单)

 Order::getSome();

这个函数给你相关的模型,为什么你不使用yii2的规范方式

/**
 * Lists all AntigoneResidente models.
 * @return mixed
 */
public function actionIndex()
{
    $searchModel = new OrderSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


    return $this->render('@common/views/antigone-residente/index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

然后在gridview中,您可以访问特定列

<?= GridView::widget([
      'dataProvider' => $dataProvider,
      'filterModel' => $searchModel,
      'columns' => [
          ['class' => 'yii\grid\SerialColumn'],
          //'id',
          'your_column_name1',
          'your_column_name2',
          .....

          ['class' => 'yii\grid\ActionColumn',],
      ],
]); ?>

答案 1 :(得分:0)

如果要使用arrayDataProvider,则必须以数组格式传递数据而不是对象

     public function actionIndex()
    {
        $data = order::getSome()->asArray();
        $dataProvider = new ArrayDataProvider([
            'key'=>'id',
            'allModels' => $data,
            'sort' => [
                'attributes' => ['id','paid','vendorid','orderno'],
            ],
    ]);

}

在你的Gridview中

    echo GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'id',
            'paid', 
            'vendorid',
'orderno'
            ],

    ]
]);