Ext JS 4: Filtrando uma TreeStore
Postei isso originalmente nos fóruns do Senchaaqu mas não obtive nenhuma resposta (além da minha própria resposta, que postarei em breve), então vou repassá-la aqui e ver se recebo mais ajuda.
Estive pensando em como filtrar um TreeStore na 4.0.7. Eu tentei o seguinte:
The model
Ext.define('model', {
extend: 'Ext.data.Model',
fields: [
{name: 'text',, type: 'string'},
{name: 'leaf', type: 'bool'},
{name: 'expanded', type: 'bool'},
{name: 'id', type: 'string'}
],
hasMany: {model: 'model', name: 'children'}
});
A loj
Ext.define('myStore', {
extend: 'Ext.data.TreeStore',
model: 'model',
storeId: 'treestore',
root: {
text: 'root',
children: [{
text: 'leaf1',
id: 'leaf1',
children: [{
text: 'child1',
id: 'child1',
leaf: true
},{
text: 'child2',
id: 'child2',
leaf: true
}]
},{
text: 'leaf2',
id: 'leaf2',
leaf: true
}]
},
proxy: {
type: 'memory',
reader: {
type: 'json'
}
}
});
A árvor
var myTree = Ext.create('Ext.tree.Panel', {
id: 'myTree',
selType: 'cellmodel',
selModel: Ext.create('Ext.selection.CellModel', {mode: 'MULTI'}),
rootVisible: false,
store: Ext.create('myStore'),
width: 300
});
O filtr
var filter = Ext.create('Ext.util.Filter', {
filterFn: function(item) {
return item.data.text == 'leaf1';
}
});
Então, acho que meu problema é ... não sei como usar esse filtro devido ao TreeStore não herdar nenhum tipo de função de filtro, como uma loja normal. Eu tentei
myTree.store.filters.add(filter);
myTree.store.filters.filter(filter); // This seems to work
// I can get into the filterFn when debugging, but I think item is the "this" of my filter object.
Normalmente, se eu tiver uma grade e criar um filtro como acima, eu posso fazermyTree.store.filter(filter)
e ele captura o item / filtro de cada linha no que eu retorno ... mas estou pensando porque o TreeStore não herda uma função de filtragem, que não está sendo passada.
Se alguém puder fornecer alguma clareza sobre o que estou fazendo de errado ou alguma ideia sobre como configurar uma função de filtro / meu processo de raciocínio, vá em frente. Agradeço qualquer ajuda.