如何限制从组合框Extjs中选择值的方法

时间:2018-09-09 18:56:38

标签: javascript user-interface extjs

在用于创建和编辑记录的窗口中,我有一个组合框类型字段

Ext.define('BookApp.view.Book', {
extend: 'Ext.window.Window',
alias: 'widget.bookwindow',
width   : 450,
title: 'Book',
layout: 'fit',
autoShow: true,
modal   : true,
initComponent: function() {
    this.items = [{
            xtype: 'form',
            items: [               
             {
                xtype: 'combobox',
                fieldLabel: 'Status',
                name: 'status',
                store: Ext.data.StoreManager.lookup('Statuses'),
                valueField: 'id',
                displayField: 'name',
                typeAhead: true,
                queryMode: 'remote'

            },

......

存储状态提供表中具有以下字段的记录:id,名称,order_install,其中order_install是状态的顺序。

表格状态

id名称order_install

23个新1

24在工作2

29已推迟3

34个已发货4

31次运输5

如何根据order_install字段将状态列表中的值选择限制为向上或向下仅一个值?

例如:如果处于“工作中”状态,则只有“已推迟”和“新建”状态可供选择

2 个答案:

答案 0 :(得分:0)

所以我有两种解决方案,一种是防止选择组合项(但它将保持可见),另一种是隐藏不想选择的值(使用存储过滤器)。

1)FIDDLE: beforeselect
2)FIDDLE: filterBy

答案 1 :(得分:0)

解决方案

  • 使用具有自定义过滤功能的store filterBy()方法。
  • 因为您的id值是随机的,所以您必须获取存储中记录的内部位置才能找到上一条和下一条记录。

在下面的示例中,filterCombo()是用于过滤商店的函数。此功能用于组合框选择事件。您可以在需要的地方使用它。 ExtJS 4和ExtJS 6版本之间存在差异,因此有两个示例:

ExtJS 4

Ext.onReady(function(){

    Ext.QuickTips.init();
    Ext.FocusManager.enable();

    var store = Ext.create('Ext.data.Store', {
        fields: ['order', 'id', 'name'],
        data : [
            {"id": 23, name: "New", order_install: 1},
            {"id": 24, name: "In Work", order_install: 2},
            {"id": 29, name: "Postponed", order_install: 3},
            {"id": 34, name: "Shipped", order_install: 4},
            {"id": 31, name: "In_transit", order_install: 5}
        ]
    });

    function filterCombo(combobox, index) {
        store = combobox.getStore();
        store.clearFilter();
        store.filterBy(
            function(record) {
                if ((record.index == index - 1) || (record.index == index) || (record.index == index + 1)) {
                    return true;
                } else {
                    return false;
                }
            }
        );
    };

    Ext.create('Ext.form.ComboBox', {
        fieldLabel: 'Choose items',
        store: store,
        queryMode: 'local',
        displayField: 'name',
        valueField: 'id',
        multiSelect: false,
        renderTo: Ext.getBody(),
        value: 'In Work',
        listeners: {
            'select': function (combo, records) {
                index = records[0].index;
                filterCombo(combo, index);
            },
            'render': function (combo) {
                index = combo.store.find('name', combo.getValue());
                filterCombo(combo, index);
            }
        }
    });
});

ExtJS 6

Ext.application({
    name : 'Fiddle',

    launch : function() {
        var store = Ext.create('Ext.data.Store', {
            fields: ['order', 'id', 'name'],
            data : [
                {"id": 23, name: "New", order_install: 1},
                {"id": 24, name: "In Work", order_install: 2},
                {"id": 29, name: "Postponed", order_install: 3},
                {"id": 34, name: "Shipped", order_install: 4},
                {"id": 31, name: "In_transit", order_install: 5}
            ]
        });

        function filterCombo(combobox, index) {
            store = combobox.getStore();
            store.clearFilter();
            store.filterBy(
                function(record) {
                    if ((record.internalId == index - 1) || (record.internalId == index) || (record.internalId == index + 1)) {
                        return true;
                    } else {
                        return false;
                    }
                }
            );
        };

        Ext.create('Ext.form.field.ComboBox', {
            fieldLabel: 'Choose items',
            store: store,
            queryMode: 'local',
            displayField: 'name',
            valueField: 'id',
            value: '24', // Equals to "In Work"
            multiSelect: false,
            renderTo: Ext.getBody(),
            listeners: {
                'select': function (combo, records) {
                    index = records.internalId;
                    filterCombo(combo, index);
                },
                'render': function (combo) {
                    index = combo.getSelection().internalId;
                    filterCombo(combo, index);
                }
            }
        });     
    }
});