2016-05-15 81 views
1

這裏是爲了表現出一定的關係表中的控制器的Yii:如何添加日期選擇器以過濾的GridView

$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中的新成員,請告訴我

回答

1

首先安裝任何datepicker widg等即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, 
     ]); 
    } 
+0

感謝,請問是什麼搜索模型 – Ali

+0

它只是一個過濾器模型,你可以更改/刪除,如果你不需要它... 如果答案適合你,那麼請接受這個答案 – JKLM

+0

請分享搜索模型代碼以及,謝謝。我會這樣做 – Ali

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, 
      ]); 

    } 
相關問題