简体   繁体   English

ExtJs组合在商店页面加载时失去选定的值

[英]ExtJs combo loses selected value on store page load

I have an ExtJS 4.1 combo box with a JsonStore and queryMode: 'remote', with paging and filtering, as such: 我有一个带有JsonStore和queryMode的ExtJS 4.1组合框:'remote',具有分页和过滤功能,如下所示:

...
queryMode:      'remote',
allowBlank:     true,
forceSelection: true,
autoSelect:     false,
pageSize:       25,
typeAhead:      true,
minChars:       2,
...

When I load my form with a saved value in this combo box, I load the store passing the saved value as a query (filtering) parameter, to make sure that the selected value is definitely within the returned records, and then I set that value as the combo selected value as such: 当我在此组合框中加载带有保存值的表单时,我加载商店将保存的值作为查询(过滤)参数传递,以确保所选值肯定在返回的记录中,然后我设置该值作为组合选择的值如下:

mycombo.getStore().load({
        params: {
            query: displayField
        },
        scope: {
            field: combo,
            valueField: valueField,
            displayField: displayField
        },
        callback: function(records, operation, success) {
            this.field.setValue(this.valueField);
        }
    });

So far, so good, the above works fine. 到目前为止,这么好,以上工作正常。 The problem is, that if the user then clicks on the dropdown arrow to select another value for the combo, the 1st page of the store is loaded, erasing all previously selected values, and even if nothing is selected, the previously selected value is lost. 问题是,如果用户然后单击下拉箭头为组合选择另一个值,则加载商店的第一页,删除所有先前选择的值,即使没有选择任何内容,先前选择的值也会丢失。

This problem is generic, and is quite similar to this question: ExtJS paged combo with remote JSON store. 这个问题是通用的,与这个问题非常相似: ExtJS分页组合与远程JSON存储。 Display selected value with paging and can be summarized as such: 使用分页显示所选值,可以这样总结:

In an ExtJS combo box with a remote store and paging, selected values are lost when the loaded page changes. 在具有远程存储和分页的ExtJS组合框中,当加载的页面更改时,选定的值将丢失。

I tried setting clearOnPageLoad: false for the store, but then each time a new page is loaded, the records are appended to the end of the list. 我尝试为商店设置clearOnPageLoad: false ,但每次加载新页面时,记录都会附加到列表的末尾。 I would have expected this parameter to cache the loaded pages and still show me the correct page while moving back and forth. 我本来希望这个参数缓存加载的页面,并在来回移动时仍然显示正确的页面。

So, any ideas on how to keep the selected value while moving between pages? 那么,关于如何在页面之间移动时保持所选值的任何想法? I suppose I could create a record with the selected value manually and append it to the store on each page load until a new value is selected, but this sounds like too much effort for something so basic. 我想我可以手动创建一个带有所选值的记录,并在每个页面加载时将其附加到商店,直到选择了一个新值,但这听起来像是太费力了。

We ended up contacting Sencha support since we have a paid license. 我们最终联系了Sencha支持,因为我们有付费许可证。 This is the answer we got back: 这是我们得到的答案:


Ext.override(Ext.form.field.ComboBox, {
    onLoad: function() {
        var me = this,
            value = me.value;

        if (me.ignoreSelection > 0) {
            --me.ignoreSelection;
        }

        if (me.rawQuery) {
            me.rawQuery = false;
            me.syncSelection();
            if (me.picker && !me.picker.getSelectionModel().hasSelection()) {
                me.doAutoSelect();
            }
        }

        else {

            if (me.value || me.value === 0) {
                if (me.pageSize === 0) { // added for paging; do not execute on page change
                    me.setValue(me.value);
                }
            } else {


                if (me.store.getCount()) {
                    me.doAutoSelect();
                } else {

                    me.setValue(me.value);
                }
            }
        }
    }
});

Had the same problem, and 'pruneRemoved: false' didn't work (it seems to be used only in grids). 有同样的问题,'pruneRemoved:false'不起作用(似乎只在网格中使用)。 This is the solution: 这是解决方案:

Ext.override(Ext.form.field.ComboBox,{

    // lastSelection is searched for records
    // (together with store's records which are searched in the parent call)

    findRecord: function(field, value) {
        var foundRec = null;
        Ext.each(this.lastSelection, function(rec) {
            if (rec.get(field) === value) {
                foundRec = rec;
                return false; // stop 'each' loop
            }
        });
        if (foundRec) {
            return foundRec;
        } else {
            return this.callParent(arguments);
        }
    }
});

Hope it doesn't have negative side effects. 希望它没有负面的副作用。 I've tested it a bit and it seems OK. 我已经测试了一下它似乎没问题。

I am experiencing this issue in extjs 6.0.1. 我在extjs 6.0.1中遇到了这个问题。

I discovered a work around that might be helpful for others. 我发现一个可能对其他人有帮助的工作。

I used override for onLoad to add the selected record from the combo to the store prior to calling the base onLoad. 在调用基于onLoad之前,我使用了onLoad的override来将组合中的选定记录添加到商店。

This works because if the selected record is in the page being viewed, the combo is smart enough to not clear the selection. 这是有效的,因为如果所选记录在正在查看的页面中,则组合足够智能,无法清除选择。 In other words, the reason the selection is being cleared as you page is because the selected record is not in the page you are viewing. 换句话说,在您翻页时清除选择的原因是因为所选记录不在您正在查看的页面中。

onLoad: function (store, records, success)
{
    var selection = this.getSelection();

    if (selection)
    {
        records.unshift(selection);
        store.insert(0, records);
    }

    this.callParent(arguments);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM