我的商店配置了memory proxy enablePaging: true
。将remoteFilter
和remoteSort
存储设置为true
,以便对代理处理的请求进行过滤和排序。
当我通过多个字段过滤我的商店时,我想使用OR条件,而不是AND。
如果没有内存代理分页,我可以使用remoteFilter: false
和自定义过滤功能,如:
store.filterBy(function (record)
{
for (var i = 0; i < searchFields.length; ++i) {
if (record.get(searchFields[i]).toLowerCase().indexOf(searchText) !== -1) {
return true;
}
}
return false;
});
但是如何通过启用分页来实现这一目标?覆盖内存代理?
答案 0 :(得分:2)
Ext.data.proxy.Memory.read使用Ext.util.Filter.createFilterFn根据传递的Ext.util.Filter []创建过滤函数(带有属性值配置):
// Filter the resulting array of records
if (filters && filters.length) {
// Total will be updated by setting records
resultSet.setRecords(records = Ext.Array.filter(records, Ext.util.Filter.createFilterFn(filters)));
resultSet.setTotal(records.length);
}
所以,如果你想使用不同的过滤器逻辑,你可以覆盖memroy代理读取方法并使用自定义函数来创建过滤器功能:
Ext.define('MyApp.extensions.FilterMemoryProxy', {
extend: 'Ext.data.proxy.Memory',
alias: 'proxy.filtermemory',
createCustomFilterFn: function (filters)
{
if (!filters) {
return Ext.returnTrue;
}
return function (candidate)
{
var items = filters.isCollection ? filters.items : filters,
length = items.length,
i, filter;
for (i = 0; i < length; i++) {
filter = items[i];
if (!filter.getDisabled() && candidate.get(filter.getProperty()).toLowerCase().indexOf(
filter.getValue().toLowerCase()) !== -1) {
return true;
}
}
return false;
};
},
read: function (operation)
{
...
if (operation.process(resultSet, null, null, false) !== false) {
// Filter the resulting array of records
if (filters && filters.length) {
// Total will be updated by setting records
resultSet.setRecords(records = Ext.Array.filter(records, me.createCustomFilterFn(filters)));
resultSet.setTotal(records.length);
}
...
}
}
});
答案 1 :(得分:0)
您可以覆盖内存代理以满足您的需求,或者,如果您只想在searchFields
中指定的某个字段中找到字符串,则可以添加虚拟&#39;指定convert
方法的模型字段。
例如:
// Extend Ext.data.Model or create it. Dealer's choice.
Ext.define('MyLocalModel', {
extend: 'Ext.data.model',
fields: [
...
{
name: 'searchField',
type: 'string',
convert: function(value, record) {
// Get search fields somewhere
var resultArray = [];
for (var i = 0; i < searchFields.length; ++i) {
var searchField = searchFields[i];
var fieldValue = record.get(searchField);
resultArray.push(fieldValue);
}
// Join by a string that would not normally be searched
// so that field values 'foo' and 'bar' are not valid when searching 'ooba'
var result = resultArray.join('**');
return result;
}
}
]
});
您在商店中需要做的就是:
store.filter('searchField', searchText);
这不完全是你要找的东西,但它应该完成工作。