我想根据搜索表单的输入临时更新变量“tempSeats”的值。搜索表单更新CGridView,我用它来根据所选航班和客户ID生成链接。我还希望它将搜索表单中的“tempSeats”数量添加到生成的url中。
到目前为止,我已经能够毫无问题地传递客户ID和航班ID。在提交搜索表单时,我似乎无法更新tempSeats。传递的值总是为0。
非常感谢任何帮助。
这是我的搜索表单:
<?php $form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl($this->route),
'method'=>'get',
)); ?>
<span style="font-size:18px;font-weight:bold;">Route: </span>
<div class="row">
<?php echo CHtml::dropDownList(
'departLocation',// for "name" attribute of <select> html tag,
// this also becomes the "id" attribute, incase you don't specify
// it explicitly in the htmlOptions array
'', // the option element that is to be selected by default
CHtml::listData( // listData helps in generating the data for <option> tags
Airport::model()->findAll(), // a list of model objects. This parameter
// can also be an array of associative arrays
// (e.g. results of CDbCommand::queryAll).
'airportName', // the "value" attribute of the <option> tags,
// here will be populated with id column values from program table
'airportName' // the display text of the <option> tag,
// here will be populated with program_name column values from table
),
array('empty'=>'From') // the htmlOptions array, whose values will be
// generated as html attributes of <select> and <option> tags
);?>
<?php echo CHtml::dropDownList(
'arrivalLocation',
'empty',
CHtml::listData(
Airport::model()->findAll(),
'airportName',
'airportName'
),
array('empty'=>'To')
);?>
</div>
<hr>
<span style="font-size:18px;font-weight:bold;">Dates:</span>
<div class="row">
Departing:
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => Flights::model(),
'attribute' => 'departDay',
'value'=>Flights::model()->departDay,
'options' => array(
'numberOfMonths'=>1,
'showButtonPanel'=>true,
'dateFormat' => 'yy-mm-dd',),
'htmlOptions' => array(
'class' => 'date',
'value' => date("Y-m-d")),
));
?>
<?php echo $form->error($model,'date_from'); ?>
</div>
<hr>
<span style="font-size:18px;font-weight:bold;">Travelers:</span>
<div class="row">
<?php echo CHtml::activeDropDownList($model,'tempSeats',array('1' => '1 Adult', '2' => '2 Adults', '3' => '3 Adults', '4' => '4 Adults', '5' => '5 Adults', '6' => '6 Adults', '7' => '7 Adults', '8' => '8 Adults', '9' => '9 Adults', '10' => '10 Adults', '11' => '11 Adults', '12' => '12 Adults', '13' => '13 Adults', '14' => '14 Adults', '15' => '15 Adults', '16' => '16 Adults', '17' => '17 Adults', '18' => '18 Adults', '19' => '19 Adults', '20' => '20 Adults',)
);?>
<?php echo CHtml::dropDownList('infant-list', $select,
array('0' => '0 Lap Infants (under 2)','1' => '1 Lap Infant',)
); ?>
<?php echo CHtml::dropDownList(
'dogSeats',
'empty',
CHtml::listData(
Flights::model()->findAll(),
'allowDogs',
'allowDogs'
),
array('empty'=>'0 Dogs')
);?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
</div>
<?php $this->endWidget(); ?>
以下是呈现搜索表单的视图:
<?php $this->renderPartial('_searchFlights',array(
'model'=>$model,
)); ?>
<?php
Yii::app()->clientScript->registerScript('searchFlights', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$.fn.yiiGridView.update('my-list', {
data: $(this).serialize()
});
return false;
});
");
?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'my-list',
'dataProvider'=>$model->searchFlights(),
'filter'=>$model,
'columns'=>array(
'departTime',
'departLocation',
'departDay',
'arrivalTime',
'arrivalLocation',
'price',
'totalSeats',
array(
'header'=>'Seats',
'value'=>'0',
'id'=>'adultSeats',
),
/*
'allowDogs',
*/
array(
'class'=>'CButtonColumn',
'template'=>'{book}',
'buttons'=>array
(
'book' => array
(
'label'=>'Book Now',
//'url'=>'CHtml::encode($data->flightID)',
//'url'=>'$this->grid->controller->createUrl("/bookings/createBooking", array("flightID"=>$data->flightID))',
'url'=>'$this->grid->controller->createUrl("/bookings/createBooking/flightID/$data->flightID/tempSeats/$data->tempSeats")',
),
),
),
),
)); ?>
以下是我在航班模型中搜索的功能:
public function searchFlights()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('flightID',$this->flightID);
$criteria->compare('flightName',$this->flightName,true);
$criteria->compare('departDay',$this->departDay,true);
$criteria->compare('departTime',$this->departTime,true);
$criteria->compare('departLocation',$this->departLocation,true);
$criteria->compare('arrivalDay',$this->arrivalDay,true);
$criteria->compare('arrivalTime',$this->arrivalTime,true);
$criteria->compare('arrivalLocation',$this->arrivalLocation,true);
$criteria->compare('totalSeats',$this->totalSeats);
$criteria->compare('price',$this->price,true);
$criteria->compare('allowDogs',$this->allowDogs);
$criteria->compare('infantSeats',$this->infantSeats);
$criteria->compare('dogSeats',$this->dogSeats);
$criteria->compare('tempSeats',$this->totalSeats);
这是来自飞行控制器的actionIndex:
/**
* Lists all models.
*/
public function actionIndex()
{
$model = new Flights('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['Flights']))
$model->attributes = $_GET['Flights'];
$dataProvider=new CActiveDataProvider('Flights');
$this->render('index', array(
'model' => $model,
'dataProvider'=>$dataProvider,
));
}