您有两种方法可以做您想做的事。要么像现在一样一次性加载所有数据,并在客户端进行过滤,要么在服务器端过滤数据。解决方案 1 将仅触发一个 HTTP 请求,该组合将非常被动。
服务器端过滤
如果要在服务器上进行过滤,则应该捕获 HTTP 请求的参数“query”。这可以通过选项进行配置queryParam
组合框的。
例如:
$query = isset($_REQUEST['query']) ? $_REQUEST['query'] : false;
$record1 = Word::model()->findAll();
$main = array();
foreach($record1 as $record) {
// Only add data for records matching the query
if ($query === false || substr($record->word, 0, strlen($query)) === $query) {
$main[]=array("wordId"=>$record->wordId,"word"=>$record->word);
}
}
echo "{ \"data\":".CJSON::encode($main)."} ";
对于这样的服务器,客户端代码应该如下所示:
var store = Ext.create('Ext.data.JsonStore', {
fields: ['wordId', 'word']
,proxy: {
// TODO...
}
});
Ext.widget('combo', {
renderTo: 'comboCt'
,queryMode: 'remote' // you have this one wrong, 'mode' was in Ext 3
,triggerAction: 'all'
,displayField: 'word'
,idField: 'wordId'
,minChars: 1
,store: store
// not needed because 'query' is the default, but you could customize that here
,queryParam: 'query'
});
客户端过滤
对于解决方案1,即加载一次并在本地过滤,您必须设置queryMode
到“本地”,并独立加载商店。您可以使用store.load()
方法,或autoLoad
option.
应该与您的服务器一起使用的示例客户端代码:
var store = Ext.create('Ext.data.JsonStore', {
fields: ['wordId', 'word']
,proxy: {
// TODO...
}
// Load the store once
,autoLoad: true
});
Ext.widget('combo', {
renderTo: 'comboCt'
// local means the combo will work with data in the store buffer
,queryMode: 'local'
,triggerAction: 'all'
,displayField: 'word'
,idField: 'wordId'
,store: store
,minChars: 1
});