6在内存代理中使用自定义过滤器功能

6在内存代理中使用自定义过滤器功能

本文介绍了ExtJS 6在内存代理中使用自定义过滤器功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配置有内存的商店代理enablePaging: true.商店的remoteFilterremoteSort设置为true,因此可以过滤和排序由代理处理的请求.

I have a store configured with memory proxy with enablePaging: true. Store's remoteFilter and remoteSort set to true so filtering and sorting requests handled by proxy.

当我按多个字段过滤商店时,我想使用OR条件,而不是AND.

When I filter my store by multiple fields I want to use OR condition, not AND.

在没有内存代理分页的情况下,我可以使用remoteFilter: false和自定义过滤器功能,例如:

Without memory proxy paging I could use remoteFilter: false and custom filter function like:

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;
});

但是我如何通过启用分页来实现呢?覆盖内存代理?

But how I can achieve this with enabled paging? Override memory proxy?

推荐答案

Ext.data.proxy.Memory.read 使用 Ext.util.Filter.createFilterFn 来创建基于传递的过滤器函数Ext.util.Filter [](具有属性值配置):

Ext.data.proxy.Memory.read use Ext.util.Filter.createFilterFn to create a filter function based on passed Ext.util.Filter[] (with property-value configs):

// 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);
}

因此,如果要使用不同的过滤器逻辑,则可以覆盖内存代理读取方法,并使用自定义函数来创建过滤器功能:

So, if you want to use different filter logic, you can override memroy proxy read method and use custom function to create filter function:

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);
            }

        ...

        }
    }
});

这篇关于ExtJS 6在内存代理中使用自定义过滤器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:53