gpt4 book ai didi

php - 在 Controller 中解析 $_Post 数据并渲染 Cgridview 时出错

转载 作者:行者123 更新时间:2023-12-01 03:14:10 24 4
gpt4 key购买 nike

我在将正确的值传递给 View 中的网格小部件时遇到问题。数据是使用 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 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 的对象发送到 Controller 操作。在本例中为/products/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 的 View 。

<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>

现在这给了我一个 CException 错误。这是错误堆栈跟踪::

    CException

CActiveDataProvider and its behaviors do not have a method or closure named "getValidators". (/var/www/html/yii/framework/base/CComponent.php:266)

#0 /var/www/html/yii/framework/web/helpers/CHtml.php(2236): CComponent->__call('getValidators', Array)
#1 /var/www/html/yii/framework/web/helpers/CHtml.php(2236): CActiveDataProvider->getValidators('id')
#2 /var/www/html/yii/framework/web/helpers/CHtml.php(1434): CHtml::activeInputField('text', Object(CActiveDataProvider), 'id', Array)
#3 /var/www/html/EasyAesthetics/protected/extensions/yiibooster/widgets/TbDataColumn.php(109): CHtml::activeTextField(Object(CActiveDataProvider), 'id', Array)
#4 /var/www/html/EasyAesthetics/protected/extensions/yiibooster/widgets/TbDataColumn.php(74): TbDataColumn->renderFilterCellContent()
#5 /var/www/html/yii/framework/zii/widgets/grid/CGridView.php(532): TbDataColumn->renderFilterCell()
#6 /var/www/html/yii/framework/zii/widgets/grid/CGridView.php(510): CGridView->renderFilter()
#7 /var/www/html/yii/framework/zii/widgets/grid/CGridView.php(480): CGridView->renderTableHeader()
#8 /var/www/html/yii/framework/zii/widgets/CBaseListView.php(167): CGridView->renderItems()
#9 [internal function]: CBaseListView->renderSection(Array)
#10 /var/www/html/yii/framework/zii/widgets/CBaseListView.php(150): preg_replace_callback('/{(\w+)}/', Array, '{summary}?{item...')
#11 /var/www/html/yii/framework/zii/widgets/CBaseListView.php(135): CBaseListView->renderContent()
#12 /var/www/html/yii/framework/web/CBaseController.php(173): CBaseListView->run()
#13 /var/www/html/EasyAesthetics/protected/views/products/selectproducts.php(37): CBaseController->widget('bootstrap.widge...', Array)
#14 /var/www/html/yii/framework/web/CBaseController.php(126): require('/var/www/html/E...')
#15 /var/www/html/yii/framework/web/CBaseController.php(95): CBaseController->renderInternal('/var/www/html/E...', Array, true)
#16 /var/www/html/yii/framework/web/CController.php(869): CBaseController->renderFile('/var/www/html/E...', Array, true)
#17 /var/www/html/yii/framework/web/CController.php(782): CController->renderPartial('selectproducts', Array, true)
#18 /var/www/html/EasyAesthetics/protected/controllers/ProductsController.php(248): CController->render('selectproducts', Array)
#19 /var/www/html/yii/framework/web/actions/CInlineAction.php(49): ProductsController->actionDropdown()
#20 /var/www/html/yii/framework/web/CController.php(308): CInlineAction->runWithParams(Array)
#21 /var/www/html/yii/framework/web/filters/CFilterChain.php(133): CController->runAction(Object(CInlineAction))
#22 /var/www/html/yii/framework/web/filters/CFilter.php(40): CFilterChain->run()
#23 /var/www/html/yii/framework/web/CController.php(1145): CFilter->filter(Object(CFilterChain))
#24 /var/www/html/yii/framework/web/filters/CInlineFilter.php(58): CController->filterAccessControl(Object(CFilterChain))
#25 /var/www/html/yii/framework/web/filters/CFilterChain.php(130): CInlineFilter->filter(Object(CFilterChain))
#26 /var/www/html/yii/framework/web/CController.php(291): CFilterChain->run()
#27 /var/www/html/yii/framework/web/CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)
#28 /var/www/html/yii/framework/web/CWebApplication.php(282): CController->run('dropdown')
#29 /var/www/html/yii/framework/web/CWebApplication.php(141): CWebApplication->runController('products/dropdo...')
#30 /var/www/html/yii/framework/base/CApplication.php(180): CWebApplication->processRequest()
#31 /var/www/html/EasyAesthetics/index.php(13): CApplication->run()
#32 {main}

我一生都无法弄清楚是什么导致了这个错误。请提供任何形式的帮助、任何可以引导我走向正确方向的提示。

谢谢,提前。麦克斯

最佳答案

问题出在 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() 指定为您的过滤器参数。

关于php - 在 Controller 中解析 $_Post 数据并渲染 Cgridview 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19311676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com