在控制器中解析$ _Post数据和渲染Cgridview时出错

时间:2013-10-11 06:31:02

标签: 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 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的对象发送到控制器动作。在这种情况下,它/ 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,
    ));



}

以下是视图,称为选择产品。

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

我不能为我的生活找出造成这个错误的原因。请提供任何形式的帮助,任何有助于我朝着正确方向前进的提示。

先谢谢。 Maxx

1 个答案:

答案 0 :(得分:4)

问题在于filter中的TbGridView 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()指定为过滤器参数。