2

我在 CGridview 中的过滤器功能有问题。过滤框根本不起作用。当我输入内容并按 Enter 时,什么也没有发生。

这里是查看代码 selectproducts.php::

<div id="shortcodes" class="page">
<div class="container">

    <!-- Title Page -->
    <div class="row">
        <div class="span12">
            <div class="title-page">
                <h2 class="title">Available Products</h2>

            </div>
        </div>
    </div>
    <!-- End Title Page -->



    <!-- Start Product Section -->
    <div class="row">


        <?php


        $this->widget('bootstrap.widgets.TbGridView', array(
            'id' => 'products-grid',
            'dataProvider' => $dataProvider,
            'filter' => $dataProvider->model,
            'ajaxUpdate' => TRUE,
            'pager' => array(
                'header' => '',
                'cssFile' => false,
                'maxButtonCount' => 25,
                'selectedPageCssClass' => 'active',
                'hiddenPageCssClass' => 'disabled',
                'firstPageCssClass' => 'previous',
                'lastPageCssClass' => 'next',
                'firstPageLabel' => '<<',
                'lastPageLabel' => '>>',
                'prevPageLabel' => '<',
                'nextPageLabel' => '>',
            ),
            'columns' => array(
                'id',
                array(
                  'name' => 'name',

                ),
                'category',
                'brand',
                'weight_unit',
                'price_unit',
                'flavors',
                array(
                    'name' => 'providers',
                    'value' => function($data) {
                        return '<div class="provider-label label label-info"><a href="http://www.'.$data->providers. '">'. $data->providers .'</a></div>';
                    },
                    'type' => 'raw',
                ),
            ),
        ));
        ?>

    </div>
    <!-- End Product Section -->


</div>

这是 ProductsController 中呈现此视图的相关操作。视图中的 $dataprovider 变量是一个 CActiveDataProvider 对象,它保存条件查询的结果。

public function actionDropdown() {

    if (isset($_GET["Dropdown"])) {
        $dropdownData = $_GET["Dropdown"];
        $this->category = $dropdownData["category"];
        $this->price = $dropdownData["price"];
    }
    // separate the price text into min and max value
    $priceText = explode(" - ", $this->price);  



    $criteria = new CDbCriteria;
    $criteria->compare('category', $this->category, true);

    $criteria->addBetweenCondition('price', substr($priceText[0], 1), substr($priceText[1], 1), 'AND');

    $dataProvider = new CActiveDataProvider('Products', array(
        'criteria' => $criteria,
        'pagination' => array(
            'pageSize' => 20,
        ),
        'sort' => array(
            'defaultOrder' => 'price_unit, name',
        ),
    ));


    $this->render('selectproducts', array(
        'dataProvider' => $dataProvider,
        'criteria' => $criteria,
    ));
}

这是 Products() 模型 search() 函数::

public function search()
    {
            // @todo Please modify the following code to remove attributes that should not be searched.

            $criteria=new CDbCriteria;

            $criteria->compare('id',$this->id);
            $criteria->compare('name',$this->name,true);
            $criteria->compare('category',$this->category,true);
            $criteria->compare('brand',$this->brand,true);
            $criteria->compare('weight',$this->weight,true);
            $criteria->compare('weight_unit',$this->weight_unit,true);
            $criteria->compare('price',$this->price,true);
            $criteria->compare('price_unit',$this->price_unit,true);
            $criteria->compare('flavors',$this->flavors,true);
            $criteria->compare('providers',$this->providers,true);

            return new CActiveDataProvider($this, array(
                    'criteria'=>$criteria,
            ));
    }

现在,当我在过滤器框中按回车键时,我收到了这个错误,它记录在 firebug

TypeError: $.param.querystring is not a function
[Break On This Error]   

options.url = $.param.querystring(options.url, options.data);

In file jquery.yiigridview.js

有谁知道是什么导致了这个错误。我只是无法弄清楚是什么原因造成的。而且我也不知道我应该在 jquery.yiigridview.js 中做些什么修改来修复这个错误。

提前致谢。麦克斯

编辑

根据下面 tinyByte 的建议。我试图将 CActiveDataProvider 逻辑移动到模型中,以便我可以在 GridView 'dataProvider' 属性中使用 $model->search() ,但它仍然没有帮助同样的错误。

这是代码

控制器:::

public function actionDropdown() {

    $dataProvider = new Products;

    if (isset($_GET["Dropdown"])) {
        $dropdownData = $_GET["Dropdown"];
        $this->category = $dropdownData["category"];
        $this->price = $dropdownData["price"];
    }


    $this->render('selectproducts', array(
        'dataProvider' => $dataProvider,
        'category' => $this->category,
        'price' => $this->price,
        //'num' => $this->numResults,
    ));
}

这是模型::

public function searchDropdown($category, $price) {

    $this->priceText = explode(" - ", $price);  

    $this->criteria = new CDbCriteria;
    $this->criteria->compare('category', $category, true);

    $this->criteria->addBetweenCondition('price', substr($this->priceText[0], 1), substr($this->priceText[1], 1), 'AND');

    return new CActiveDataProvider('Products', array(
        'criteria' => $this->criteria,
        'pagination' => array(
            'pageSize' => 25,
        ),
        'sort' => array(
            'defaultOrder' => 'price_unit, name',
        ),
    ));



}

这是视图::

<div id="shortcodes" class="page">
<div class="container">

    <!-- Title Page -->
    <div class="row">
        <div class="span12">
            <div class="title-page">
                <h2 class="title">Available Products</h2>

            </div>
        </div>
    </div>
    <!-- End Title Page -->



    <!-- Start Product Section -->
    <div class="row">


        <?php


        $this->widget('bootstrap.widgets.TbGridView', array(
            'id' => 'products-grid',
            'dataProvider' => $dataProvider->searchDropdown($category, $price),
            'filter' => $dataProvider,
            'ajaxUpdate' => TRUE,
            'pager' => array(
                'header' => '',
                'cssFile' => false,
                'maxButtonCount' => 25,
                'selectedPageCssClass' => 'active',
                'hiddenPageCssClass' => 'disabled',
                'firstPageCssClass' => 'previous',
                'lastPageCssClass' => 'next',
                'firstPageLabel' => '<<',
                'lastPageLabel' => '>>',
                'prevPageLabel' => '<',
                'nextPageLabel' => '>',
            ),
            'columns' => array(
                array(
                    'name' => 'id',
                    'type' => 'raw',
                ),
                array(
                  'name' => 'name',


                ),
                'category',
                'brand',
                'weight_unit',
                'price_unit',
                'flavors',
                array(
                    'name' => 'providers',
                    'value' => function($data) {
                        return '<div class="provider-label label label-info"><a href="http://www.'.$data->providers. '">'. $data->providers .'</a></div>';
                    },
                    'type' => 'raw',
                ),
            ),
        ));
        ?>

    </div>
    <!-- End Product Section -->


</div>

仍然无法正常工作。即当我在过滤器框中输入内容并按回车时,什么也没有发生。出现同样的错误::

在萤火虫

TypeError: $.param.querystring is not a function
[Break On This Error]   

options.url = $.param.querystring(options.url, options.data);

In file jquery.yiigridview.js

在 Chrome 中,我收到一条更具描述性的错误消息。

    Uncaught TypeError: Object function (e,n){var r,i=[],o=function(e,t)

{t=x.isFunction(t)?t():null==t?"":t,i[i.length]=encodeURIComponent(e)+"="+encodeURIComponent(t)};if(n===t&&(n=x.ajaxSettings&&

x.ajaxSettings.traditional),x.isArray(e)||e.jquery&&!x.isPlainObject(e))x.each(e,function(){o(this.name,this.value)});
else for(r in e)gn(r,e[r],n,o);return i.join("&").replace(cn,"+")} 

has no method 'querystring' 

jquery.yiigridview.js:310

我只是不知道该怎么做。

编辑

好吧,经过长时间的努力,我设法摆脱了错误,在 Yii 论坛http://www.yiiframework.com/forum/index.php/topic/47904-error-with-cgridview的一位论坛伙伴的帮助下-filter-property-error-code-has-no-method-querystring/ page_view _findpost_ p _224169

这是我添加的内容。

 <script src="<?php echo Yii::app()->assetManager->baseUrl; ?>/28e7347b/jquery.ba-bbq.js"></script>
    <script src="<?php echo Yii::app()->assetManager->baseUrl; ?>/28e7347b/jquery.jquery.ajaxqueue.js"></script>
    <script src="<?php echo Yii::app()->assetManager->baseUrl; ?>/28e7347b/jquery.autocomplete.js"></script>
    <script src="<?php echo Yii::app()->assetManager->baseUrl; ?>/28e7347b/jquery.bgiframe.js"></script>
    <script src="<?php echo Yii::app()->assetManager->baseUrl; ?>/28e7347b/jquery.maskedinput.js"></script>
    <script src="<?php echo Yii::app()->assetManager->baseUrl; ?>/28e7347b/jquery.metadata.js"></script>
    <script src="<?php echo Yii::app()->assetManager->baseUrl; ?>/28e7347b/jquery.treeview.js"></script>
    <script src="<?php echo Yii::app()->assetManager->baseUrl; ?>/28e7347b/jquery.treeview.async.js"></script>
    <script src="<?php echo Yii::app()->assetManager->baseUrl; ?>/28e7347b/jquery.treeview.edit.js"></script>
    <script src="<?php echo Yii::app()->assetManager->baseUrl; ?>/28e7347b/jquery.yii.js"></script>
    <script src="<?php echo Yii::app()->assetManager->baseUrl; ?>/28e7347b/jquery.yiiactiveform.js"></script>
    <script src="<?php echo Yii::app()->assetManager->baseUrl; ?>/28e7347b/jquery.yiitab.js"></script>
    <script src="<?php echo Yii::app()->assetManager->baseUrl; ?>/28e7347b/punycode.js"></script>

错误消失了,但过滤器仍然无法正常工作。ajax 加载图标显示不到一秒钟,然后什么也没有发生。我还看到数组中传递了正确的搜索参数。

这是我看到的数组

Dropdown[category]  Chant Books
Dropdown[price] $340 - $476
Products[brand] 
Products[category]  
Products[flavors]   
Products[name]  Owl Chant Scroll
Products[price_unit]    
Products[providers] 
Products[weight_unit]   
Products_page   1
ajax    products-grid
yt0 
4

6 回答 6

2

根据我的经验,这个错误的原因是 jQuery 冲突:有一个是你加载的,另一个是由 cgridview 加载的。解决方案是删除您的 jquery,或者如果您必须拥有它 - 删除 Yii 的:

$cs=Yii::app()->clientScript;
$cs->scriptMap=array(
    'jquery.js'=>false,
);
于 2014-03-23T21:46:38.140 回答
1

试试这个代码

  $this->widget('bootstrap.widgets.TbGridView', array(
            'id' => 'products-grid',
            'dataProvider' => $model->customeSearch(), // add your method of search here
            'filter' => $model, // for validation
             'ajaxUrl'=> Yii::app()->request->getUrl(),
            ...
于 2014-01-28T16:10:38.350 回答
0

filter 属性用于验证,您需要为网格的属性“dataprovider”提供搜索功能

    $this->widget('bootstrap.widgets.TbGridView', array(
        'id' => 'products-grid',
        'dataProvider' => $model->customeSearch(), // add your method of search here
        'filter' => $model, // for validation
        ...
        'columns' => array(
             array(
               'name' => 'someCol',
               'value' => '$data->someCol',
               'filter' => CHtml::dropdownList( ... ), // put an input here for your filter
             ),

),

于 2013-10-12T08:09:02.410 回答
0

好在最后问题解决了。事实证明,我需要添加 Gridview 小部件的 ajaxUpdate 和 ajaxURL 属性。并为 ajax 过滤器执行另一个操作。

'ajaxUpdate' => 'products-grid',
            'ajaxUrl' => Yii::app()->createUrl('products/UpdateGrid'),

无论如何,这里是代码,现在可以使用。

products 控制器中的操作方法 ::

public function actionDropdown() {

    /*
     * create the session to store the dropdown variables
     * 
     * 
     */

    $this->session = new CHttpSession;
    $this->session->open();

    $dataProvider = new Products;
    $products = new Products('search');
    $products->unsetAttributes();

    if (isset($_GET["Dropdown"])) {
        $dropdownData = $_GET["Dropdown"];
        $this->category = $dropdownData["category"];
        $this->price = $dropdownData["price"];

        $this->session["category"] = $this->category;
        $this->session["price"] = $this->price;
    }

    if (isset($_GET["Products"])) {
        $products->attributes = $_GET["Products"];
    }

    $this->render('selectproducts', array(
        'dataProvider' => $dataProvider,
        'products' => $products,
        'category' => $this->category,
        'price' => $this->price,
            //'num' => $this->numResults,
    ));
}

public function actionUpdateGrid() {

    $products = new Products;

    $products->unsetAttributes();


    if (isset($_GET["Products"])) {
        $products->attributes = $_GET["Products"];

    }




    $this->renderPartial('_selectproducts', array(
        'products' => $products,
        'category' => $this->category,
        'price' => $this->price,

    ));

}

产品型号代码::

/**
 * Retrieves a list of models based on the current search/filter conditions.
 *
 * Typical usecase:
 * - Initialize the model fields with values from filter form.
 * - Execute this method to get CActiveDataProvider instance which will filter
 * models according to data in model fields.
 * - Pass data provider to CGridView, CListView or any similar widget.
 *
 * @return CActiveDataProvider the data provider that can return the models
 * based on the search/filter conditions.
 */
public function search() {
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria = new CDbCriteria;

    $criteria->compare('id', $this->id);
    $criteria->compare('name', $this->name, true);
    $criteria->compare('category', $this->category, true);
    $criteria->compare('brand', $this->brand, true);
    $criteria->compare('weight', $this->weight, true);
    $criteria->compare('weight_unit', $this->weight_unit, true);
    $criteria->compare('price', $this->price, true);
    $criteria->compare('price_unit', $this->price_unit, true);
    $criteria->compare('flavors', $this->flavors, true);
    $criteria->compare('providers', $this->providers, true);




    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
    ));
}

/**
 * Returns the static model of the specified AR class.
 * Please note that you should have this exact method in all your CActiveRecord descendants!
 * @param string $className active record class name.
 * @return Products the static model class
 */
public static function model($className = __CLASS__) {
    return parent::model($className);
}

/*
 * Dropdown search
 * 
 * 
 */

public function searchDropdown($category, $price) {

    $this->priceText = explode(" - ", $price);

    $this->criteria = new CDbCriteria;
    $this->criteria->compare('category', $category, true);

    $this->criteria->addBetweenCondition('price', substr($this->priceText[0], 1), substr($this->priceText[1], 1), 'AND');

    return new CActiveDataProvider('Products', array(
        'criteria' => $this->criteria,
        'pagination' => array(
            'pageSize' => 25,
        ),
        'sort' => array(
            'defaultOrder' => 'price_unit, name',
        ),
    ));
}

public function searchGrid() {

    $session = new CHttpSession;
    $session->open();


    $category = $session["category"];


    $this->criteria = new CDbCriteria;

    $this->priceText = explode(" - ", $session["price"]);
    $this->criteria->compare('category', $category, true);

    $this->criteria->addBetweenCondition('price', substr($this->priceText[0], 1), substr($this->priceText[1], 1), 'AND');

    /*
     * 
     * compare the names to see if the flavor or weight is present
     * 
     */

    $this->criteria->compare('name', $this->flavors, TRUE);
    $this->criteria->compare('name', $this->weight, TRUE);

    $this->criteria->compare('id', $this->id);
    $this->criteria->compare('name', $this->name, true);
    $this->criteria->compare('category', $this->category, true);
    $this->criteria->compare('brand', $this->brand, true);
    $this->criteria->compare('weight', $this->weight, true);
    $this->criteria->compare('weight_unit', $this->weight_unit, true);
    $this->criteria->compare('price', $this->price, true);
    $this->criteria->compare('price_unit', $this->price_unit, true);
    $this->criteria->compare('flavors', $this->flavors, true);
    $this->criteria->compare('providers', $this->providers, true);


    return new CActiveDataProvider($this, array(
        'criteria' => $this->criteria,
        'pagination' => array(
            'pageSize' => 25,
        ),
        'sort' => array(
            'defaultOrder' => 'price_unit, name',
        ),
    ));

}

和意见::

查看名称::- selectproducts.php

<div id="shortcodes" class="page">
<div class="container">

    <!-- Title Page -->
    <div class="row">
        <div class="span12">
            <div class="title-page">
                <h2 class="title">Available Products</h2>

            </div>
        </div>
    </div>
    <!-- End Title Page -->



    <!-- Start Product Section -->
    <div class="row">


        <?php
        $this->widget('bootstrap.widgets.TbGridView', array(
            'id' => 'products-grid',
            'dataProvider' => $dataProvider->searchDropdown($category, $price),
            'filter' => $products,
            'ajaxUpdate' => 'products-grid',
            'ajaxUrl' => Yii::app()->createUrl('products/UpdateGrid'),
            'pager' => array(
                'header' => '',
                'cssFile' => false,
                'maxButtonCount' => 25,
                'selectedPageCssClass' => 'active',
                'hiddenPageCssClass' => 'disabled',
                'firstPageCssClass' => 'previous',
                'lastPageCssClass' => 'next',
                'firstPageLabel' => '<<',
                'lastPageLabel' => '>>',
                'prevPageLabel' => '<',
                'nextPageLabel' => '>',
            ),
            'columns' => array(
                array(
                    'name' => 'name',
                    'value' => function($data) {
                        return '<div class="custom-badge">' . $data->name . '</div>';
                    },
                    'type' => 'raw',
                ),
                array(
                    'name' => 'category',
                    'value' => function($data) {
                        return '<div class="grid-glow">' . $data->category . '</div>';
                    },
                    'type' => 'raw',
                ),
                array(
                    'name' => 'brand',
                    'value' => function($data) {
                        return '<div class="grid-glow">' . $data->brand . '</div>';
                    },
                    'type' => 'raw',
                ),
                array(
                    'name' => 'weight_unit',
                    'value' => function($data) {
                        return '<div class="grid-glow">' . $data->weight_unit . '</div>';
                    },
                    'type' => 'raw',
                ),
                 array(
                    'name' => 'price_unit',
                    'value' => function($data) {
                        return '<div class="grid-price-glow">' . $data->price_unit . '</div>';
                    },
                    'type' => 'raw',
                ),
                array(
                    'name' => 'flavors',
                    'value' => function($data) {
                        return '<div class="grid-glow">' . $data->flavors . '</div>';
                    },
                    'type' => 'raw',
                ),
                array(
                    'name' => 'providers',
                    'value' => function($data) {
                        return '<div class="provider-label label label-info"><a href="http://www.' . $data->providers . '">' . $data->providers . '</a></div>';
                    },
                    'type' => 'raw',
                ),
            ),
        ));
        ?>

    </div>
    <!-- End Product Section -->


</div>

视图名称::- _selectproducts.php

<?php

        $this->widget('bootstrap.widgets.TbGridView', array(
            'id' => 'products-grid',
            'dataProvider' => $products->searchGrid($category, $price),
            'filter' => $products,
            'ajaxUpdate' => TRUE,
            'pager' => array(
                'header' => '',
                'cssFile' => false,
                'maxButtonCount' => 25,
                'selectedPageCssClass' => 'active',
                'hiddenPageCssClass' => 'disabled',
                'firstPageCssClass' => 'previous',
                'lastPageCssClass' => 'next',
                'firstPageLabel' => '<<',
                'lastPageLabel' => '>>',
                'prevPageLabel' => '<',
                'nextPageLabel' => '>',
            ),
            'columns' => array(
                array(
                    'name' => 'name',
                    'value' => function($data) {
                        return '<div class="custom-badge">' . $data->name . '</div>';
                    },
                    'type' => 'raw',
                ),
                array(
                    'name' => 'category',
                    'value' => function($data) {
                        return '<div class="grid-glow">' . $data->category . '</div>';
                    },
                    'type' => 'raw',
                ),
                array(
                    'name' => 'brand',
                    'value' => function($data) {
                        return '<div class="grid-glow">' . $data->brand . '</div>';
                    },
                    'type' => 'raw',
                ),
                array(
                    'name' => 'weight_unit',
                    'value' => function($data) {
                        return '<div class="grid-glow">' . $data->weight_unit . '</div>';
                    },
                    'type' => 'raw',
                ),
                 array(
                    'name' => 'price_unit',
                    'value' => function($data) {
                        return '<div class="grid-price-glow">' . $data->price_unit . '</div>';
                    },
                    'type' => 'raw',
                ),
                array(
                    'name' => 'flavors',
                    'value' => function($data) {
                        return '<div class="grid-glow">' . $data->flavors . '</div>';
                    },
                    'type' => 'raw',
                ),
                array(
                    'name' => 'providers',
                    'value' => function($data) {
                        return '<div class="provider-label label label-info"><a href="http://www.' . $data->providers . '">' . $data->providers . '</a></div>';
                    },
                    'type' => 'raw',
                ),
            ),
        ));
        ?>

正如您所见,ajax 过滤器需要一个新的操作方法,该方法将呈现仅包含结果网格的局部视图。我希望它可以帮助遇到同样错误的人。

谢谢,麦克斯

于 2013-10-16T12:21:06.417 回答
0

刷新 yii js 文件。在配置中:

'components'=>array(

    'clientScript' => array(
        'packages' => array(
            'jquery' => array(
                'baseUrl' => 'js',
                'js' => array(
                    'jquery-1.10.2.js', 
                    'jquery-migrate-1.2.1.min.js',
                )
            ),
        ),
    ),

)

于 2015-07-01T06:21:41.730 回答
0

TypeError: $.param.querystring 不是函数

[打破这个错误]

options.url = $.param.querystring(options.url, options.data);

在文件中jquery.yiigridview.js

我也遇到了上面报告的同样问题,很容易解决。当您包含的 jQuery 与添加的默认 js 冲突时,通常会发生这种情况jquery.yiigridview.js

尝试简单地删除您包含的 min.js,它绝对可以正常工作。

于 2017-12-13T07:53:33.390 回答