我在CGridview中的过滤器功能上遇到了问题。过滤器框根本不起作用。当我输入内容并按回车键时,没有任何反应。
以下是查看代码选择产品.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>
下面是产品控制器中呈现此视图的相关操作。视图中的$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,
));
}
这是产品()模型搜索()函数::
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
根据我的经验,此错误的原因是jQuery冲突:有一个你加载,另一个由cgridview加载。解决方案是删除你的jquery,或者如果你必须拥有它 - 删除Yii的:
$cs=Yii::app()->clientScript;
$cs->scriptMap=array(
'jquery.js'=>false,
);
试试这段代码
$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(),
...
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
),
),
问题终于解决了。事实证明,我需要添加 ajaxUpdate 和 Gridview widget 的 ajaxURL 属性。并为 ajax 过滤器执行另一个操作。
'ajaxUpdate' => 'products-grid',
'ajaxUrl' => Yii::app()->createUrl('products/UpdateGrid'),
无论如何,这是代码,现在可以工作了。
产品控制器中的操作方法 ::
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',
),
));
}
和观点:
查看名称::- 选择产品.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 过滤器需要一个新的操作方法,该方法将呈现一个只包含结果网格的部分视图。我希望它能帮助那些遇到相同错误的人。
谢谢麦克斯
刷新 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',
)
),
),
),
)
TypeError: $.param.querystring 不是函数
[中断此错误]
options.url = $.param.querystring(options.url, options.data);
在文件jquery.yiigridview.js
中
我也遇到了上面报告的相同问题,并且很容易解决。它通常发生在你包含的jQuery与jquery.yiigridview.js
添加的默认js冲突时。
尝试简单地删除您包含的最小.js,它将绝对正常工作。