javascript - Sencha Touch store filterBy function -
hi trying work store filterby function unable make work want. have 4 buttons(a b c d) , list data related buttons when click on button should filter accordingly note:- when click ,i should records of when click b , should records of b appended a's list of records when click on buttons should have array of button ids , filter the list using these id's.
filterlist:function (list, index, target, record){ var categories=[]; categories.push(record.get('id')); for(c=0;c<categories.length;c++) { ext.getstore('mystore').filterby(function(record){ var id = record.get('id'); if(id==categories[c]) { return true; } }); } }
any appreciated.
what wrong code
the filterby
return function should always return boolean value
. returning true
, , no false
. besides filtering
store
in loop
, , parameter record
exists twice (as parameter in function filterlist and in return function of filterby).
example
i created fiddle show came with. know extjs
instead of sencha touch
, idea. step through maincontroller
, logic is.
i didn't clean code there duplication, it's concept.
setting logic
first create set of buttons on view
. see i'm setting action property
.
... tbar: [{ text: 'filter a', action: 'a' }, { text: 'filter b', action: 'b' }], ...
then bind onclick
event button. in event use action property searchvalue, of course.
... onclick: function(button, event, eopts) { var me = this, grid = me.getmaingrid(), store = grid.getstore(), filters = store.getfilters(), searchvalue = button.action, regex = regexp(searchvalue, 'i'), filter = new ext.util.filter({ id: button.id, filterfn: function(record) { var match = false; ext.object.each(record.data, function(property, value) { match = match || regex.test(string(value)); }); return match; } }); if (filters.containskey(button.id)) { store.removefilter(filter); } else { store.addfilter(filter); } }, ...
the magic
the magic in filter instance
. adding new filter instances
each time filter, can filter several filters. if push button a
, button b
respect each other. in filter use regex
search through data
of current model
. config id
recognize filter can remove afterwards.
filter = new ext.util.filter({ id: button.id, filterfn: function(record) { var match = false; ext.object.each(record.data, function(property, value) { match = match || regex.test(string(value)); }); return match; } });
if filter doesn't exists added, otherwise removed.
if (filters.containskey(button.id)) { store.removefilter(filter); } else { store.addfilter(filter); }
reset filters
i created textfield
search through data. instead of adding , removing filters
call clearfilter
, add new filter
new searchvalue
.
onkeyup: function(textfield, event, eopts) { this.filterstore(textfield.getvalue()); }, filterstore: function(searchvalue) { var me = this, grid = me.getmaingrid(), store = grid.getstore(), regex = regexp(searchvalue, 'i'); store.clearfilter(true); store.filter(new ext.util.filter({ filterfn: function(record) { var match = false; ext.object.each(record.data, function(property, value) { match = match || regex.test(string(value)); }); return match; } })); }
the complete maincontroller
ext.define('filtering.controller.maincontroller', { extend: 'ext.app.controller', config: { refs: { maingrid: 'maingrid' } }, init: function() { var me = this; me.listen({ global: {}, controller: {}, component: { 'segmentedbutton': { toggle: 'ontoggle' }, 'toolbar > button': { click: 'onclick' }, 'textfield': { keyup: 'onkeyup' } }, store: { '*': { metachange: 'onmetachange', filterchange: 'onfilterchange' } }, direct: {} }); }, onlaunch: function() { var store = ext.storemanager.lookup('users') || ext.getstore('users'); store.load(); }, onmetachange: function(store, metadata) { var grid = this.getmaingrid(), model = store.getmodel(), // metadata fields = metadata.fields, columns = metadata.columns, gridtitle = metadata.gridtitle; model.fields = fields; grid.settitle(gridtitle); grid.reconfigure(store, columns); }, onfilterchange: function(store, filters, eopts) { var me = this, grid = me.getmaingrid(); grid.getselectionmodel().select(0); }, /** * used segmented buttons */ ontoggle: function(container, button, pressed) { var me = this, grid = me.getmaingrid(), store = grid.getstore(), //filters = store.getfilters(), searchvalue = button.action, regex = regexp(searchvalue, 'i'), filter = new ext.util.filter({ id: button.id, filterfn: function(record) { var match = false; ext.object.each(record.data, function(property, value) { match = match || regex.test(string(value)); }); return match; } }); if (pressed) { store.addfilter(filter); } else { store.removefilter(filter); } }, /** * used toolbar buttons */ onclick: function(button, event, eopts) { var me = this, grid = me.getmaingrid(), store = grid.getstore(), filters = store.getfilters(), searchvalue = button.action, regex = regexp(searchvalue, 'i'), filter = new ext.util.filter({ id: button.id, filterfn: function(record) { var match = false; ext.object.each(record.data, function(property, value) { match = match || regex.test(string(value)); }); return match; } }); if (filters.containskey(button.id)) { store.removefilter(filter); } else { store.addfilter(filter); } }, /** * used textfield */ onkeyup: function(textfield, event, eopts) { this.filterstore(textfield.getvalue()); }, filterstore: function(searchvalue) { var me = this, grid = me.getmaingrid(), store = grid.getstore(), regex = regexp(searchvalue, 'i'); store.clearfilter(true); store.filter(new ext.util.filter({ filterfn: function(record) { var match = false; ext.object.each(record.data, function(property, value) { match = match || regex.test(string(value)); }); return match; } })); } });
the complete view
ext.define('filtering.view.maingrid', { extend: 'ext.grid.panel', xtype: 'maingrid', tbar: [{ xtype: 'segmentedbutton', allowmultiple: true, items: [{ text: 'filter a', action: 'a' }, { text: 'filter b', action: 'b' }] }, { text: 'filter a', action: 'a' }, { text: 'filter b', action: 'b' }, { xtype: 'textfield', emptytext: 'search', enablekeyevents: true }], //title: 'users', // title set through metadata //store: 'users', // reconfigure store in metachange event of store columns: [] // columns set through metadata of store (but must set empty array avoid problems) });
Comments
Post a Comment