通过从yii2中的datepicker获取输入来搜索数据

时间:2016-06-08 03:20:57

标签: yii2

在这种情况下,搜索模型中的sql查询是 -

$query = (new Query())
    ->select (['billdate','billno','bills_partyname','billamount'])
    ->from('bills')
    ->where(['between', 'billdate', 'from_date', 'to_date']);

我在index.php文件中添加了一个daterangepicker。代码是 -

<?=  DatePicker::widget([
    'name' => 'from_date',
    'value' => '2014-01-01',
    'type' => DatePicker::TYPE_RANGE,
    'name2' => 'to_date',
    'value2' => '2016-01-01',
    'pluginOptions' => [
        'autoclose'=>true,
        'format' => 'yyyy-mm-dd'
    ]
]);
?>

控制器

public function actionIndex()
    {
        $searchModel = new PartiesSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    /**
     * Displays a single Parties model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

index.php看起来像

<?php

use yii\helpers\Html;
use kartik\grid\GridView;
use kartik\date\DatePicker;
use kartik\daterange\DateRangePicker;
use kartik\form\ActiveForm;
//use dosamigos\datepicker\DatePicker;
use frontend\modules\districtreport\models\ExpartiesSearch;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\districtreport\models\PartiesSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Parties';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="parties-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

<!--     <p>
        <?= Html::a('Create Parties', ['create'], ['class' => 'btn btn-success']) ?>
    </p> -->
    <!-- <div class="custom-filter">

    Date range:
     <input name="start" />
     <input name="end" />

    </div> -->




<?= '<label class="control-label">Select Date Range</label>'; ?>
<?=  DatePicker::widget([
    'model' => $searchModel,
    'attribute' => 'from_date',
    'value' => '2014-01-01',
    'type' => DatePicker::TYPE_RANGE,
    'attribute2' => 'to_date',
    'value2' => '2016-01-01',
    'pluginOptions' => [
        'autoclose'=>true,
        'format' => 'yyyy-mm-dd'
    ]
]);
?>
        <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'export' => false,
        'columns' => [
        // [
        //     'class' => 'kartik\grid\ExpandRowColumn',
        //     'value' => function($model, $key, $index, $column){
        //         return GridView::ROW_COLLAPSED;
        //     },
        //     'detail' => function($model, $key, $index, $column){
        //         $searchModel = new ExpartiesSearch();
        //         $searchModel-> parties_district = $model['district'];
        //         $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        //         return Yii::$app->controller->renderPartial('_exparties', [
        //             'searchModel' => $searchModel,
        //             'dataProvider' => $dataProvider,
        //             ]);                   

        //         },
        //     ], 
            // 'district', 
            // 'sell',
            // 'collection',
        'billdate',
        'billno',
        'bills_partyname',
        'billamount',

        ],
    ]); ?>
</div>

这不起作用。请告诉我需要做什么。

3 个答案:

答案 0 :(得分:1)

像这样的事情

在GridView之前的视图中

<?php

$form = ActiveForm::begin([
    'method' => 'get',
    'enableClientScript' => false
]);

?>
<?= '<label class="control-label">Select Date Range</label>'; ?>
<?=  DatePicker::widget([
    'model' => $searchModel,
    'attribute' => 'from_date',
    'value' => '2014-01-01',
    'type' => DatePicker::TYPE_RANGE,
    'attribute2' => 'to_date',
    'value2' => '2016-01-01',
    'pluginOptions' => [
        'autoclose'=>true,
        'format' => 'yyyy-mm-dd'
    ]
]);
?>

<?php echo Html::submitButton('Search') ?>

<?php ActiveForm::end(); ?>

在控制器

public function actionIndex(){

    $searchModel = new PartiesSearch();
    $dataProvider = $searchModel->search(\Yii::$app->request->get());

    return $this->render('index', compact('dataProvider', 'searchModel'));
}

在您的搜索模型中

class PartiesSearch extends Parties
{
    public $from_date;
    public $to_date;


    //add rule

    public function rules(){
        return [
            //... your rules,
            [['from_date', 'to_date'], 'safe']
        ];
    }

    //... some code

    public function search($params = []){

        $query = (new Query())
            ->select (['billdate','billno','bills_partyname','billamount'])
            ->from('bills');

        $dataProvider = new ActiveDataProvider([
            'query' => $query
        ]);

        if( !($this->load($params) && $this->validate()) ){
            return $dataProvider;
        }

        if($this->from_date && $this->to_date)
            $query->where(['between', 'billdate', $this->from_date, $this->to_date]);

        return $dataProvider;
    }

答案 1 :(得分:0)

from_date来自post / Get方法试试这个

$query = (new Query())->select (['billdate','billno','bills_partyname','billamount'])->from('bills')->where(['between', 'billdate', $_POST['from_date'], $_POST['to_date']]);

答案 2 :(得分:0)

您没有使用模型来创建日期范围选择器,因此可以使用模型或更改

中的名称
'name' => 'from_date'

'name' => 'PartiesSearch[from_date]'

*和to_date类似