我最近开始与 Yii 合作,并对此印象深刻。但是,关于如何为 CGridView 创建 Ajax URL,我遇到了一个奇怪的问题。我正在尝试第一次搜索,然后通过单击分页链接在搜索结果中前进一些页面,然后再次搜索。但是,我的网格第二次以以下格式发送请求
http://127.0.0.1/myapp/backend/customers/index/XDEBUG_SESSION_START/1/Accounts%5BaccountID%5D//Accounts%5Bname%5D//Accounts%5Baddress%5D/address+1/Accounts%5Bbirthday%5D//Accounts%5BclientType%5D//Accounts%5Bemail%5D//Accounts%5Bbalance%5D//Accounts%5BhasAccess%5D//Accounts_page/3/ajax/yw1?ajax=yw1&Accounts%5BaccountID%5D=&Accounts%5Bname%5D=&Accounts%5Baddress%5D=&Accounts%5Bbirthday%5D=&Accounts%5BclientType%5D=&Accounts%5Bemail%5D=&Accounts%5Bbalance%5D=&Accounts%5BhasAccess%5D=&Accounts_page=1
你可以看到这个 URL 的一半是重写的(它是由 CPagination 在渲染分页链接时创建的)。然而,Yii 的 gridview js 将新查询附加到分页链接,最终导致 URL 中的变量具有先前值和新值的重复。因此,当代码执行到达 CUrlManager.php 中的第 411 行时
$_REQUEST[$name]=$_GET[$name]=$value;
$_GET 变量中的值丢失($value 包含 URL 的旧值,这些值包含在 URL 的重写部分(SEO 友好)中)。最后我又得到了以前的搜索结果。
我正在使用的规则
'urlManager' => array(
'urlFormat' => 'path',
'rules' => array(
'<module:\w+>/<controller:\w+>/<action:\w+>"=>"<module>/<controller>/<action>',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
'showScriptName' => false,
'caseSensitive' => true,
),
模型中的搜索功能
*/
public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('accountID', $this->accountID, true);
$criteria->compare('name', $this->name, true);
$criteria->compare('balance', $this->balance, true);
$criteria->compare('address', $this->address, true);
$criteria->compare('birthday', $this->birthday, true);
$criteria->compare('clientType', $this->clientType, true);
$criteria->compare('email', $this->email, true);
$criteria->compare('createAt', $this->createAt, true);
$criteria->compare('hasAccess', $this->hasAccess);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
模型中的行为
public function behaviors() {
return array('CAdvancedArBehavior' => array(
'class' => 'application.extensions.CAdvancedArBehavior'));
}
动作索引代码
public function actionIndex()
{
if (isset($_POST['attribute']))
{
$attribute =$_POST['attribute'];
$attribute = @explode('_',$attribute);
$row = Accounts::model()->findByPk($attribute[1]);
if($row != null )
{
$row->save();
}
$dataProvider= new Accounts('search');
$dataProvider->unsetAttributes(); // clear any default values
if(isset($_GET['Accounts']))
$dataProvider->attributes=$_GET['Accounts'];
$this->render('index', array('model' => $dataProvider));
}
}
我的视图文件:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array (
'name' => 'accountID',
'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
),
array(
'name' => 'name',
'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
),
array(
'name' => 'address',
'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
),
array( // display 'create_time' using an expression
'name'=>'birthday',
'value'=>'date("M j, Y", strtotime($data->birthday))',
'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
),
array(
'name' => 'clientType',
'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
),
array(
'name' => 'email',
'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
),
array(
'name' => 'balance',
'type' => 'raw',
'header' => 'Balance',
'value' =>'($this->grid->owner->widget("application.extensions.jeditable.DsJEditableWidget", array(
"model" => $this->grid->dataProvider,
"name" => "balance_".$data->accountID,
"value" => $data->balance,
"jeditable_type" => "text",
),true))',
),
array( //
'class' => 'JToggleColumn',
'name' => 'hasAccess',
'filter' => array(1=>'Yes', 0=>'No'),
'checkedButtonImageUrl'=> Yii::App()->baseUrl.'/images/checked.png', // checked image
'uncheckedButtonImageUrl'=> Yii::App()->baseUrl.'/images/unchecked.png', // unchecked image
'checkedButtonLabel'=>'Yes', // tooltip
'uncheckedButtonLabel'=>'No', // tooltip
'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
),
)
)
我不想用
'ajaxUrl' => $this->createUrl('index')
当我尝试使用 jtogglecolumn 时,由于请求 url 中没有查询数据,它会重置我的搜索和分页。