这是我的代码,以便在控制器中显示一些关系表数据
$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的新人请告诉我
答案 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,
]);
}