请在此处请求健全性检查...

ExtJS 4.2 comboBox Typeahead可以工作,但是在以下情况下检索valueField时出现问题:

1)如果用户键入一个值,然后他们未点击Enter或单击选择组合列表,而是单击其他位置,则valueField为空,但存在所选值。

2)假设正确选择了组合框项目,如果我输入其他字符,然后退格该字符,则组合框将无法再找到valueField..it,几乎就像它已重置了自己一样。

小提琴的例子

https://fiddle.sencha.com/#fiddle/je1

如何繁殖

如果在组合框中输入Maggie,则将在控制台窗口中看到valueField ID,如果先添加字符然后退格该字符,则控制台窗口中的ID为null

(您将需要打开控制台窗口以查看输出)

forceSelection无法解决此问题,因为我有一个模板,并且它不会接受组合框中不属于商店的条目,并且我需要为我的valueField使用sumID,因为我需要检索该值并将其传递给服务器。

最佳答案

谢谢大家,拥有这样一个很棒的社区真棒!

我可以通过使用forceSelection并覆盖setValue来解决此问题,从而允许通过forceSelection选择不在商店中但在组合中的模板项目。为了更好的外观,从与IMO组合框一起玩耍,forceSelection是必经之路。

这是我的替代,请参见语句//替代开始

这是一个快速解决方案,当我回到办公室时,我将优化陈述,下面我从内存中粘贴解决方案,您就明白了。

setValue: function(value, doSelect) {
        var me = this,
            valueNotFoundText = me.valueNotFoundText,
            inputEl = me.inputEl,
            i, len, record,
            dataObj,
            matchedRecords = [],
            displayTplData = [],
            processedValue = [];

        if (me.store.loading) {
            // Called while the Store is loading. Ensure it is processed by the onLoad method.
            me.value = value;
            me.setHiddenValue(me.value);
            return me;
        }

        // This method processes multi-values, so ensure value is an array.
        value = Ext.Array.from(value);

        // Loop through values, matching each from the Store, and collecting matched records
        for (i = 0, len = value.length; i < len; i++) {
            record = value[i];
            if (!record || !record.isModel) {
                record = me.findRecordByValue(record);
            }
            // record found, select it.
            if (record) {
                matchedRecords.push(record);
                displayTplData.push(record.data);
                processedValue.push(record.get(me.valueField));
            }
            // record was not found, this could happen because
            // store is not loaded or they set a value not in the store
            else {
            //start of override

            // 'Select All Names' is the template item that was added                                     // to the combo box,  it looks like an entry from the store
            // but it is not in the store
            if (me.forceSelection && me.getDisplayValue() === 'Select All Names'){
                    processedValue.push(value[i]);
                    dataObj = {};
                    dataObj[me.displayField] = value[i];
                    displayTplData.push(dataObj);
                }
                //end of override

                if (!me.forceSelection) {
                    processedValue.push(value[i]);
                    dataObj = {};
                    dataObj[me.displayField] = value[i];
                    displayTplData.push(dataObj);
                    // TODO: Add config to create new records on selection of a value that has no match in the Store
                }
                // Else, if valueNotFoundText is defined, display it, otherwise display nothing for this value
                else if (Ext.isDefined(valueNotFoundText)) {
                    displayTplData.push(valueNotFoundText);
                }
            }
        }

    // Set the value of this field. If we are multiselecting, then that is an array.
    me.setHiddenValue(processedValue);
    me.value = me.multiSelect ? processedValue : processedValue[0];
    if (!Ext.isDefined(me.value)) {
        me.value = null;
    }
    me.displayTplData = displayTplData; //store for getDisplayValue method
    me.lastSelection = me.valueModels = matchedRecords;

    if (inputEl && me.emptyText && !Ext.isEmpty(value)) {
        inputEl.removeCls(me.emptyCls);
    }

    // Calculate raw value from the collection of Model data
    me.setRawValue(me.getDisplayValue());
    me.checkChange();

    if (doSelect !== false) {
        me.syncSelection();
    }
    me.applyEmptyText();

    return me;
},

关于javascript - ComboBox typeAhead有效,但在某些行为情况下valueField为null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28979652/

10-11 13:52