【问题标题】:Setting a Default Value to a ComboBox using Json in ExtJS 3.4在 ExtJS 3.4 中使用 Json 将默认值设置为 ComboBox
【发布时间】:2021-12-02 02:45:00
【问题描述】:

我试图在 ExtJS 3.4 中的 ComboBox 中设置默认值 我试图在 ComboBox 配置中设置 value: 'Standard',但这只会在框中放置一个字符串。我做了一些挖掘并尝试设置 afterrender 功能,但仍然没有得到它来填充。目标是让框实际选择值并用 Json 数据填充框,以便用户能够从后续的 ComboBox 中进行选择。

        var hatComboStore = new Ext.data.JsonStore({
                autoLoad: true,
                fields: [
                    'BIN_ID',
                    'BIN_DESC'
                ],
                baseParams: {
                    method: 'post'
                },
                url: 'json_bin_hat.php'
        });

        var hatCombo = new Ext.form.ComboBox({
            allowBlank: false,
            autoSelect: true,
            displayField: 'BIN_DESC',
            emptyText: 'Select a hat...',
            fieldLabel: 'Hat',
            forceSelection: false,
            hiddenName: 'hatId',
            itemId: 'hatId',
            listEmptyText: 'No records found',
            listeners: {
                afterrender: function(combo, record, index) {
                    var hat = combo.getValue();
                    binCombo.store.baseParams.hat = hat;
                },      
                select: function(combo, record, index) {
                    var hat = combo.getValue();
                    binCombo.store.baseParams.hat = hat;
                },
                focus: function(combo) {
                    binCombo.clearValue();
                }
            },
            mode: 'remote',
            msgTarget: 'side',
            name: 'hatDesc',
            store: hatComboStore,
            submitValue: true,
            triggerAction: 'all',
            valueField: 'BIN_ID'
        });

有人有什么想法吗?感谢您的帮助!

【问题讨论】:

    标签: javascript json extjs extjs3


    【解决方案1】:

    我已经使用override method 让它工作了。

    基本上,displayValue 是我想要显示的 ('Standard'),value 是我想要执行的 (value: 1)。显然,使用远程存储有点棘手,所以这就是需要覆盖的原因。

            var hatComboStore = new Ext.data.JsonStore({
                    autoLoad: true,
                    fields: [
                        'BIN_ID',
                        'BIN_DESC'
                    ],
                    baseParams: {
                        method: 'post'
                    },
                    url: 'json_bin_hat.php'
            });
    
            var hatCombo = new Ext.form.ComboBox({
                allowBlank: false,
                autoSelect: true,
                displayField: 'BIN_DESC',
                displayValue: 'Standard',
                emptyText: 'Select a hat...',
                fieldLabel: 'Hat',
                forceSelection: false,
                hiddenName: 'hatId',
                itemId: 'hatId',
                listEmptyText: 'No records found',
                listeners: {
                    afterrender: function(combo, record, index) {
                        var hat = combo.getValue();
                        binCombo.store.baseParams.hat = hat;
                    },      
                    select: function(combo, record, index) {
                        var hat = combo.getValue();
                        binCombo.store.baseParams.hat = hat;
                    },
                    focus: function(combo) {
                        binCombo.clearValue();
                    }
                },
                mode: 'remote',
                msgTarget: 'side',
                name: 'hatDesc',
                store: hatComboStore,
                submitValue: true,
                triggerAction: 'all',
                valueField: 'BIN_ID'
                value: 1
            });
    
            //Override method to default hatCombo value to 'Standard' while underlying value = 1
            Ext.override(Ext.form.ComboBox, {
                initValue: Ext.form.ComboBox.prototype.initValue.createSequence(function() {
                    if (this.mode == 'remote' && !!this.valueField && this.valueField != this.displayField && this.displayValue) {
                        if (this.forceSelection) {
                            this.lastSelectionText = this.displayValue;
                        }    
                        this.setRawValue(this.displayValue);
                    }    
                })
            });
    
    

    【讨论】:

      【解决方案2】:

      看起来value 属性不适用于远程商店。可能在商店加载之前呈现组合。您可以在商店加载后设置该值。

      这对我有用(测试为ExtJS 3.4 fiddle)。

      var hatComboStore = new Ext.data.JsonStore({
          autoLoad: true,
          fields: [
              'id',
              'title'
          ],
          url: 'https://jsonplaceholder.typicode.com/todos'
      });
      
      var hatCombo = new Ext.form.ComboBox({
          fieldLabel: 'Hat',
          store: hatComboStore,
          displayField: 'title',
          valueField: 'id',
          mode: 'local',
          submitValue: true,
          triggerAction: 'all',
          // Apparently does not work with remote store.
          // value: '1'
      });
      
      hatComboStore.on('load', () => hatCombo.setValue('1'));
      
      new Ext.form.FormPanel({
          items: hatCombo,
          renderTo: Ext.getBody()
      });
      
      

      【讨论】:

      • 感谢您的帮助。我仍然只是将 Standard 的值作为字符串输入。这实际上并没有在组合框中选择“标准”选项,它只是将字符串放在框中,我不能在表单的其余部分中使用输入。我无法编辑存储,因为我正在从稍后在我的代码中引用的远程 SQL 数据库中读取值。我得到的最接近的是将值编辑为“1”,但我需要解码 1 的值以显示“标准”
      • 我已编辑我的答案以使用远程商店。
      【解决方案3】:

      查看设置默认值的示例:

      Combo config:
      
      {
          triggerAction: 'all',
          id: 'dir_id',
          fieldLabel: 'Direction',
          queryMode: 'local',
          editable: false,
          xtype: 'combo',
          store : dirValuesStore,
          displayField:'name',
          valueField:'id',
          value: 'all',
          width: 250,
          forceSelection:true
      }
      

      来源:Extjs 4 combobox default value

      【讨论】:

      • 这在我看来像 HTML,而不是 ExtJS。
      • 抱歉没看到正确我已经编辑了我的回答请更新
      猜你喜欢
      • 2015-07-09
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多