2014-03-04 24 views
3

我有以下問題。我有一個城市,我想把兩個盒子日期(從 - 到)。當選擇一段時間我想更新網格,但在這裏是問題。這裏是我的代碼到目前爲止Yii + datepicker boostrap如何更新網格

$dateisOn = CHtml::textField('Event[from_date]', '', 
    array('id' => 'from', 'data-date-format' => 'yyyy-mm-dd')) . 
    ' До ' . CHtml::textField('Event[from_date]', '', 
    array('id' => 'to', 'data-date-format' => 'yyyy-mm-dd')); 
$this->widget('ext.widgets.StyledGridWidget', array(
'id' => 'event-grid', 
'dataProvider' => $model->search(), 
'enablePagination' => false, 
'filter' => $model, 
'columns' => array(
    array(
     'name' => 'product_id', 
     'value' => '$data->product_id' 
    ), 
    array(
     'name' => 'cnt', 
     'header' => 'Брой', 
     'value' => '$data->cnt' 
    ), 
    array(
     'name' => 'code', 
     'value' => '$data->code' 
    ), 
    array(
     'name' => 'date', 
     'filter' => $dateisOn, 
    ), 
), 

));

這裏是JS日期選擇器:

var checkin = $('#from').datepicker({ 

    onRender: function(date) { 

    } 
}).on('changeDate', function(ev) { 

    var newDate = new Date(ev.date); 
    newDate.setDate(newDate.getDate() + 1); 
    checkout.setValue(newDate); 
    checkin.hide(); 
    $('#to')[0].focus(); 
}).data('datepicker'); 

var checkout = $('#to').datepicker({ 
    onRender: function(date) { 

    } 
}).on('changeDate', function(ev) { 
    checkout.hide(); 
}).data('datepicker'); 

這是我的模型功能搜索

public function search() { 
    //var_dump($this->date_first); exit; 
    $criteria = new CDbCriteria; 
    $criteria->with = (array('order', 'product')); 
    $criteria->compare('product.id', $this->product_id); 
    $criteria->compare('product.code', $this->code); 
    $criteria->compare('cnt', $this->cnt); 

    $criteria->select = 'count(t.product_id) AS cnt, t.product_id, product.code'; 
    $criteria->order = 'cnt DESC'; 
    $criteria->group='t.product_id'; 
    $criteria->limit = 10;  
    if (!empty($this->from_date) && empty($this->to_date)) { 
     $criteria->condition = "order.updated_at >= '$this->from_date'"; 
    } elseif (!empty($this->to_date) && empty($this->from_date)) { 
     $criteria->condition = "order.updated_at <= '$this->to_date'"; 
    } elseif (!empty($this->to_date) && !empty($this->from_date)) { 
     $criteria->condition = "order.updated_at >= '$this->from_date' and order.updated_at <= '$this->to_date'"; 
    } 


    $criteria->together = true; 

    return new CActiveDataProvider($this, array(
     'criteria' => $criteria, 
    )); 
} 

回答

0

首先,你要修復to_date領域:

$dateisOn = CHtml::textField('Event[from_date]', '', 
    array('id' => 'from', 'data-date-format' => 'yyyy-mm-dd')) . 
    ' До ' . CHtml::textField('Event[to_date]', '', 
    array('id' => 'to', 'data-date-format' => 'yyyy-mm-dd')); 

你也可以修改您的搜索功能,您可以簡單使用:

$criteria->compare('order.updated_at', '>='.$this->from_date); 
$criteria->compare('order.updated_at', '<='.$this->to_date);