簡體   English   中英

如何限制從組合框Extjs值的選擇

[英]How to restrict the selection of values from combobox 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是狀態的順序。

表狀態

編號名稱order_install

23新1

24在工作2

29推遲3

34已出貨4

31在途5

如何根據order_install字段將狀態列表中的值選擇限制為向上或向下僅一個值?

例如:如果狀態為“工作中”,則只有“已推遲”和“新建”狀態可供選擇

所以我有兩種解決方案,一種是防止選擇組合項(但它將保持可見),另一種是隱藏您不想選擇的值(使用存儲過濾器)。

1) FIDDLE:事前選擇
2) FIDDLE:filterBy

解決方案

  • 使用具有自定義過濾功能的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);
                }
            }
        });     
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM