我是 Sencha 的新手,我做了我的第一个应用程序。我需要将搜索放在列表中。我浏览了从 Sencha 下载的示例搜索,但不知道如何将其插入搜索列表。很高兴得到任何提示。 这是我的应用
问问题
1371 次
1 回答
0
起初,我很难让搜索字段在 MVC 风格的应用程序中工作。
我能够像这样在您的 sencha fiddle 应用程序中创建搜索字段。
在你的控制器中我做了
Ext.define('Sencha.controller.Main', {
extend: 'Ext.app.Controller',
config: {
refs: {
main: 'mainpanel'
},
control: {
'#list': {
disclose: 'showDetail'
},
'#view':{
activate:function(){
Ext.getCmp('list').add({
xtype:'toolbar',
docked:'top',
items:[{
xtype: 'searchfield',
itemId:'contact_search',
placeHolder: 'Search....',
listeners: {
scope: this,
clearicontap: this.onSearchClearIconTap,
keyup: this.onSearchKeyUp}
}]
})
}
}
}
},
showDetail: function(list, record) {
this.getMain().push({
xtype: 'recipedetail',
title: record.fullName(),
data: record.data
})
}, onSearchKeyUp: function(field) {
//get the store and the value of the field
var value = field.getValue(),
store = Ext.getCmp('list').getStore();
//first clear any current filters on thes tore
store.clearFilter();
//check if a value is set first, as if it isnt we dont have to do anything
if (value) {
//the user could have entered spaces, so we must split them so we can loop through them all
var searches = value.split(' '),
regexps = [],
i;
//loop them all
for (i = 0; i < searches.length; i++) {
//if it is nothing, continue
if (!searches[i]) continue;
//if found, create a new regular expression which is case insenstive
regexps.push(new RegExp(searches[i], 'i'));
}
//now filter the store by passing a method
//the passed method will be called for each record in the store
store.filter(function(record) {
var matched = [];
//loop through each of the regular expressions
for (i = 0; i < regexps.length; i++) {
var search = regexps[i],
didMatch = record.get('title').match(search);
//if it matched the first or last name, push it into the matches array
matched.push(didMatch);
}
//if nothing was found, return false (dont so in the store)
if (regexps.length > 1 && matched.indexOf(false) != -1) {
return false;
} else {
//else true true (show in the store)
return matched[0];
}
});
}
},
/**
* Called when the user taps on the clear icon in the search field.
* It simply removes the filter form the store
*/
onSearchClearIconTap: function() {
//call the clearFilter method on the store instance
this.getStore().clearFilter();
}
});
然后在 app.js 中,我向视口添加了一个 ID
launch: function() {
Ext.Viewport.add({
id:'view',
xtype: 'mainpanel'
});
}
我还在 RecieptList.js 中添加了一个 ID
xtype: 'recipelist',
requires: ['Sencha.store.Recipes'],
id:'list',
config: {
可能不是最传统的解决方案,但它确实有效。很容易看出这一切是如何协同工作的,希望对您有所帮助。
于 2012-05-14T22:00:05.523 回答