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