首页 > 解决方案 > 如何限制从组合框 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'

            },

…………………………………………………………………………

Store Statuses 提供表中的记录,其中包含以下字段:id、name、order_install,其中 order_install 是状态的顺序。

表状态

ID 名称 order_install

23 新 1

24 工作中 2

29 推迟 3

34 已发货 4

31 中转 5

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

例如:如果状态为 In Work,则只有 Postponed 和 New 状态可供选择

标签: javascriptuser-interfaceextjs

解决方案


解决方案

  • filterBy()使用带有自定义过滤功能的store方法。
  • 因为您的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);
                }
            }
        });     
    }
});

推荐阅读