Php 分析控制器中的$\u Post数据并呈现Cgridview时出错
将正确的值传递给视图中的网格小部件时,我遇到了一个问题。使用ajax从jquery传递数据 代码如下:Php 分析控制器中的$\u Post数据并呈现Cgridview时出错,php,jquery,ajax,yii,Php,Jquery,Ajax,Yii,将正确的值传递给视图中的网格小部件时,我遇到了一个问题。使用ajax从jquery传递数据 代码如下: $('#find-product').click(function(e) { e.preventDefault(); alert($('#find-product').data("url")); /* * Make the ajax call to send the selected option to the controller for proce
$('#find-product').click(function(e) {
e.preventDefault();
alert($('#find-product').data("url"));
/*
* Make the ajax call to send the selected option to the controller for processing
* URL : $('#find-product').data("url") , the path of the controller's action
* Dropdown : the obejct that is passed to the controller
*
* Dropdown.category : the category selected
* Dropdown.price : the price range selected
*
*
*/
$.ajax({
url: $('#find-product').data("url"),
data: {
Dropdown: {
category: $('#supp-category').find(":selected").text(),
price: $('#supp-price').find(":selected").text()
}
},
type: "POST",
error: function(xhr, tStatus, e) {
if (!xhr) {
alert(" We have an error ");
alert(tStatus + " " + e.message);
} else {
alert("else: " + e.message); // the great unknown
}
},
success: function(resp) {
document.location.href = $('#find-product').data("url");
}
})
});
因此,我想将名为Dropdown的对象发送到控制器操作。在这种情况下,它的/产品/下拉列表
在actionDropdown()中,我试图形成一个CDbCriteria并将其传递给CActiveDataProvider,以便使用它渲染网格
以下是ProductsController中actionDropdown的代码:
public function actionDropdown() {
$criteria = new CDbCriteria;
$criteria->compare('category', $_POST['Dropdown']['category'], true);
$dataProvider = new CActiveDataProvider('Products', array(
'criteria' => $criteria,
));
$this->render('selectproducts', array(
'dataProvider' => $dataProvider,
));
}
这是一个叫做selectproducts的视图
<div class="row">
<?php
$this->widget('bootstrap.widgets.TbGridView', array(
'id' => 'products-grid',
'dataProvider' => $dataProvider,
'filter' => $dataProvider,
'columns' => array(
'id',
'name',
'category',
'brand',
'weight_unit',
'price_unit',
'flavors',
'providers',
),
));
?>
</div>
我一辈子都搞不清楚是什么导致了这个错误。请提供任何帮助,任何能指引我正确方向的提示
提前谢谢。
Maxx问题在于
TbGridView
中的filter
param。筛选器必须是模型的实例,而不是数据提供程序
$this->widget('bootstrap.widgets.TbGridView', array(
'id' => 'products-grid',
'dataProvider' => $dataProvider,
'filter' => $dataProvider->model, // Simplest way
'columns' => array(
'id',
'name',
'category',
'brand',
'weight_unit',
'price_unit',
'flavors',
'providers',
),
));
或者只需将
YourModel::model()
指定为筛选参数。问题在于TbGridView
中的filter
参数。筛选器必须是模型的实例,而不是数据提供程序
$this->widget('bootstrap.widgets.TbGridView', array(
'id' => 'products-grid',
'dataProvider' => $dataProvider,
'filter' => $dataProvider->model, // Simplest way
'columns' => array(
'id',
'name',
'category',
'brand',
'weight_unit',
'price_unit',
'flavors',
'providers',
),
));
或者只需将
YourModel::model()
指定为筛选参数。非常感谢您解决了CEException错误。但我又犯了一个新错误。它给了我一个未定义的索引:在$criteria->compare('category',$_POST['Dropdown']['category'],true)行上的下拉列表;请您提供一些建议,说明如何正确执行此操作。未定义索引是php中的常见错误,请参阅此答案以供参考:或者问另一个问题,因为这是不相关的。是的,这很有帮助。再次感谢您的帮助非常感谢您解决了CException错误。但我又犯了一个新错误。它给了我一个未定义的索引:在$criteria->compare('category',$_POST['Dropdown']['category'],true)行上的下拉列表;请您提供一些建议,说明如何正确执行此操作。未定义索引是php中的常见错误,请参阅此答案以供参考:或者问另一个问题,因为这是不相关的。是的,这很有帮助。再次感谢你的帮助