Yii:如何添加datepicker以过滤gridview

时间:2016-05-15 09:47:49

标签: php yii yii2

这是我的代码,以便在控制器中显示一些关系表数据

    $query = 'SELECT `Transaction`.`id`, SUM(`Transaction`.`amount`) AS all_amount, `App`.`packageId` as packageId, `title` FROM `Transaction` INNER JOIN `App` ON `Transaction`.`fk_app_id` = `App`.`id` GROUP BY `App`.`packageId`';
$model = \Yii::$app->db->createCommand($query)->queryAll();
    $provider = new ArrayDataProvider([
                'allModels' => $model,
               'sort' => [
                    'attributes' => ['id', 'all_amount', 'title'],
                ],
                'pagination' => [
                    'pageSize' => 20,
                ],

            ]);

是视图代码

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        'id',
        'packageId',
        'all_amount',
        'title'


    ],

]);

我想添加一个日期选择器来查看和使用它来过滤GridView
我是yii2的新人请告诉我

2 个答案:

答案 0 :(得分:1)

首先安装任何datepicker小部件,即2amigos date picker

use dosamigos\datepicker\DatePicker;
索引网格中的

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
    'columns' => [
    'id',
        'packageId',
        'all_amount',
        'title',
    [
                    'attribute'=>'attribute_name',
                    'value' =>'attribute_name',

                    'filter'=>DatePicker::widget([
            'model' => $searchModel,
            'attribute'=>'attribute_name',
                'clientOptions' => [
                    'autoclose' => true,
                    'format' => 'yyyy=M-dd'
                ]
        ])       
],
    ],
        ]); 

        ?>

actionindex()

中的控制器中
public function actionIndex()
    {
        $searchModel = new <model name>Search();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

答案 1 :(得分:0)

尝试这个...

include: use dosamigos\datepicker\DatePicker;//index.php//grid

</php echo GridView::widget
        ([
            'dataProvider' => $dataProvider,
             'filterModel' => $searchdata,
 'columns' => [
                   [
                            'label' => 'DATE',
                            'value' => 'date',
                            'filter'=> DatePicker::widget([
                            'attribute' => 'date',
                            'name' => 'DATE',
                            'value' => 'date',
                            'model' => $searchdata,
                            'clientOptions' => [
                                 'autoclose' => false,
                                'format' => 'yyyy-mm-dd'
                               ],
                            ]);
                     ],
             ]
       ]);

//controller
public function actionIndex()
    {
         $searchdata = new Model_NameSearch();
         $dataProvider = $searchdata->search(Yii::$app->request->queryParams);
            return $this->render('index', [
                'searchdata' => $searchdata,
                'dataProvider' => $dataProvider,
            ]);

    }