2

我在将正确的值传递给视图中的网格小部件时遇到问题。数据是使用 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,
    ));



}

这是视图,称为 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>

现在这给了我一个 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}

我一生都无法弄清楚导致此错误的原因。请提供任何帮助,任何能让我朝着正确方向前进的提示。

提前致谢。麦克斯

4

1 回答 1

4

问题出在filter参数中TbGridView。过滤器必须是模型的实例,而不是数据提供者。

$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()为您的过滤器参数。

于 2013-10-11T06:54:06.717 回答