简体   繁体   English

ExtJs ComboBox 刷新选定(原始)值

[英]ExtJs ComboBox Refresh Selected (Raw) Value

I have a combobox with store items ({ value, text}), and sometimes I need to update the texts.我有一个带有商店项目({值,文本})的 combobox,有时我需要更新文本。 When I do that the selected value (text) is not updated.当我这样做时,所选值(文本)不会更新。

Here is the fiddle to illustrate and the code looks like below.这是要说明的小提琴,代码如下所示。 We pick an item in the drop down then we click on update text.我们在下拉列表中选择一个项目,然后单击更新文本。 This will update the text in the drop down but not the combobox raw value.这将更新下拉列表中的文本,但不会更新 combobox 原始值。

[Update]: replaced model.set() with store.loadData() [更新]:将 model.set() 替换为 store.loadData()

 Ext.application({ name: 'Fiddle', launch: function() { Ext.create("Ext.form.Panel", { renderTo: Ext.getBody(), width: 300, height: 200, bodyPadding: 20, layout: "form", items: [ { xtype: "combobox", itemId: "pickmin", fieldLabel: "Test", queryMode: "local", store: { fields: ["value", "text"], data: [ { value: 1, text: "Text 1" }, { value: 2, text: "Text 2" }, { value: 3, text: "Text 3" }, { value: 4, text: "Text 4" }, ] } } ], buttons: [ { text: "Update Text", handler: function (btn) { const combo = btn.up("form").down("#pickmin"); const newData = [] combo.store.each(r => { newData.push({ value: r.data.value, text: r.data.text + "0" }); }); combo.store.loadData(newData); } } ] }); } });

You have to use你必须使用

 r.set("text", r.data.text + "0");

to change the value in each record.更改每条记录中的值。 Your button code has to be:您的按钮代码必须是:

 buttons: [{
            text: "Update Text",
            handler: function (btn) {
                const combo = btn.up("form").down("#pickmin");
                combo.store.each(r => {
                    r.set("text", r.data.text + "0"); 
                });
            }
          }]

Update更新

You can solve it with that code:您可以使用该代码解决它:

combo.getStore().on("load",
    function (store, records, successful, operation, eOpts) {
       combo.setValue(combo.getValue());
    },
    this
)

I have written a Sencha fiddle for you showing the solution: https://fiddle.sencha.com/#view/editor&fiddle/3cf6我已经为你写了一个 Sencha fiddle 来展示解决方案: https://fiddle.sencha.com/#view/editor&fiddle/3cf6

I glad if it helped.如果有帮助,我很高兴。 Please consider accepting the best answer.请考虑接受最佳答案。

You have forgotten to set the valueField and displayField Property for the combobox The combobox attributes should be like this您忘记为 combobox 设置 valueField 和 displayField 属性 combobox 属性应该是这样的

 xtype: "combobox",
 itemId: "pickmin",
 valueField:"value",
 displayField :"text"

Now the rawValue can be fetched from the combobox现在可以从 combobox 中获取 rawValue

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

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