提交后,CJuiDatePicker在文本字段中保留值

时间:2015-11-25 06:47:12

标签: javascript php jquery yii

我有一个搜索模块,用户在CJuiDatePicker中选择日期。 当我点击提交时,页面将加载,但我的datepicker中的日期将消失。我如何保留我的领域的价值? 搜索值很好。它返回正确的数据。但是字段中的值已经消失了。有人可以帮忙。?

我的形式是这样的。

<div class="panel-body">
    <?php
        $form = $this->beginWidget( 'CActiveForm', 
            array(      'id' => 'search-form', 
                        'action' => array('index'), 
                        'enableAjaxValidation'=>true,
                        'enableClientValidation' => true, 
                        'clientOptions' => array('validateOnSubmit' => true), 
                        'htmlOptions' => array(
                            'class' => 'form-horizontal', 
                            'onsubmit' => '
                                return  !(document.getElementById(\'to\').disabled) && 
                                        !(document.getElementById(\'from\').value == \'\') && 
                                        !(document.getElementById(\'to\').value == \'\');'
            ))
        );
    ?> 
    <form>
        <div class="form-group"><label>Date From</label>
            <?php 
                $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                    'model' => $model,  
                    'name' => 'start_date',
                    'options'=>array(
                        'dateFormat'=>'yy-mm-dd',
                    ),
                    'htmlOptions'=>array(
                        'class'=>'form-control',
                        'placeholder'=>'From',
                        'value' => $search_date_start,
                    ),
                ));  
            ?> 
        </div> 

        <div class="form-group"><label>To</label>
            <div class="controls" style='margin-top: 5px;'>
                <?php 
                $end_date = date('m-d-Y');
                    $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                        'model' => $model,  
                        'name' => 'end_date',
                        'options'=>array(
                            'dateFormat'=>'yy-mm-dd',
                        ),
                        'htmlOptions'=>array(
                            'class'=>'form-control',
                            'placeholder'=>'To',
                            'value' => $search_date_end,
                        ),
                    ));  
                ?>
            </div>
        </div>

        <div class="form-group"> <label>Employee Name</label>
            <div class="controls" style='margin-top: 5px;'>
                <?php
                    $form->widget('zii.widgets.jui.CJuiAutoComplete', array('name' => 'search',
                        'source' => Evaluation::searchEvaluation(),
                        'htmlOptions' => array('class' => 'form-control', 'placeholder' => 'Search'),
                        'options' => array(
                        ),
                        'value' => $search,
                    ));
                ?>  
            </div>
        </div>

        <div class="form-group">
            <input type="submit" name='search_btn' value="Search" class='btn btn-primary '>
            <input type="submit" name='reset_btn' value="Reset" class='btn btn-default '> 
        </div>
    </form>
    <?php $this->endWidget();?>
</div>

这是我的控制器代码:

class EvaluationController extends Controller {

    public function actionIndex()
    { 
        Helper_Site::initializePage();
        LoginForm::checkLogin();
        $user = LoginForm::getUser();

        if($user->username == $user->company_id){
            Yii::app()->getController()->redirect(array("site/initiallogin"));
        }else{
            $model = new EvaluationDetails('search');
            $model->unsetAttributes();  // clear any default values
            if (isset($_GET['EvaluationDetails'])) {
                $model->attributes = $_GET['EvaluationDetails'];
            }

            $modelE = new Evaluation('search');
            $modelE->unsetAttributes();  // clear any default values
            if (isset($_GET['Evaluation'])) {
                $modelE->attributes = $_GET['Evaluation'];
            }

            //start here
            $search = '';

            //SEARCH BTN
            if(isset($_POST['search_btn'])) {
                if (isset($_POST['start_date'])) {
                    $search = $_POST['search'];
                    Yii::app()->session['search_evaluation'] = $search;
                    $search_date_start = '';
                    $search_date_end = '';
                } else {
                    if (isset($_POST['start_date']) && isset($_POST['end_date'])) {
                    $search_date_start = $_POST['start_date'];
                    $search_date_end = $_POST['end_date'];
                    Yii::app()->session['start_date_evaluation'] = $search_date_start;
                    Yii::app()->session['end_date_evaluation'] = $search_date_end;
                } else {
                    var_dump("no start_date and no end_date passed");
                    if (count(Evaluation::model()->findAll()) == 0) {
                        $search_date_start = date('Y-m-d', strtotime("-1 month"));
                        $search_date_end = date('Y-m-d');

                    } else {
                        $criteria = new CDbCriteria();
                        $criteria->order = "start_date ASC";
                        $search_date_start = date("Y-m-d", strtotime(Evaluation::model()->find($criteria)->start_date));
                        $criteria->order = "end_date DESC";
                        $search_date_end = date("Y-m-d", strtotime(Evaluation::model()->find($criteria)->start_date));

                    }
                }

                /*if (isset($_POST['search'])) {
                    $search = $_POST['search'];
                    Yii::app()->session['search_evaluation'] = $search;
                }*/
                }
            }// RESET BTN
            else if (isset($_POST['reset_btn'])) {
            unset(Yii::app()->session['start_date_evaluation']);
            unset(Yii::app()->session['end_date_evaluation']);
            unset(Yii::app()->session['search_evaluation']);

            $search_date_start = date('Y-m-d', strtotime("-1 month"));
            $search_date_end = date('Y-m-d');
            } else {
            if (isset(Yii::app()->session['start_date_evaluation']) && isset(Yii::app()->session['end_date_evaluation'])) {
                $search_date_start = Yii::app()->session['start_date_evaluation'];
                $search_date_end = Yii::app()->session['end_date_evaluation'];

                if (isset(Yii::app()->session['search_evaluation'])) {
                    $search = Yii::app()->session['search_evaluation'];

                }
            } else {
                unset(Yii::app()->session['start_date_evaluation']);
                unset(Yii::app()->session['end_date_evaluation']);
                unset(Yii::app()->session['search_evaluation']);

                $search_date_start = date('Y-m-d', strtotime("-1 month"));
                $search_date_end = date('Y-m-d');
            } 
        }   //end here

            $this->render('index', array('model' => $model, 'modelE' => $modelE, 'search_date_start' => $search_date_start, 'search_date_end' => $search_date_end, 'search' => $search));
        }
    }

1 个答案:

答案 0 :(得分:0)

当使用带有小部件的param模型时,不要这样设置值:

$this->widget('zii.widgets.jui.CJuiDatePicker', array(
    'model' => $model,
    'attribute' => 'start_date',
    'htmlOptions' => array(
        'value' => $search_date_start
    )
));

在模型(更好的方式)或控制器中进行。

Controller的简短示例:

if (isset($_POST['EvaluationDetails'])) {
    $model->attributes = $_POST['EvaluationDetails']; // specify model scenario and rules() to set only needed params
} else {
    $model->start_date = date('Y-m-d', strtotime("-1 month"));
}

您还要检查$_POST['start_date']的值。在为CJuiDatePicker指定模型参数时,还应考虑模型名称$_POST['EvaluationDetails']['start_date']