ExtJS 6 - Bind disabled property to new records in a store

这一生的挚爱 提交于 2020-01-05 09:14:20

问题


I'm trying to enable/disable a button when the store getNewRecords() function return the length, but not work!

bind: {
  disabled: "{!grid.getStore().getNewRecords().length}"
}

Fiddle: https://fiddle.sencha.com/fiddle/1sj5

Someone have idea to how resolve this?


回答1:


You need to create a formula in your viewmodel:

viewModel: {
    formulas: {
        hasNewRecords: function (r) {
            return this.getView().down("treepanel").getStore().getNewRecords().length > 0;
        }
    }
}

then you can use it for your bindings:

bind: {
    disabled: "{hasNewRecords}"
}

(probably not the best way to get the data you want).

You can read about it here, here and here .




回答2:


What you're wanting to do here is currently not possible in the framework. Instead, you should create a ViewModel data value and modify that where need be, like this:

var form = Ext.create("Ext.form.Panel", {
    viewModel: {
        data: {
            newRecords: false
        }
    },
    items: [{
        xtype: "textfield",
        labelField: "Add Child",
        name: "col1",
        value: "Teste 123"
    }],
    tbar: {
        xtype: "button",
        text: "Add new record",
        handler: function () {
            var data = this.up("form").getForm().getFieldValues();
            var rec = grid.getStore().getAt(0);
            data["treeCol"] = rec.childNodes.length + 1;
            // setting value, so binding updates
            this.lookupViewModel().set('newRecords', true);
            rec.appendChild(data);
        }
    },
    bbar: {
        xtype: "button",
        text: "button to disabled when new records",
        bind: {
            disabled: "{newRecords}"
        }
    },
    renderTo: Ext.getBody()
});



回答3:


Or by simply doing this.

In your controller:

me.getView().getViewModel().set('storeLen', store.getNewRecords().length);

In your ViewModel, simply do this:

formulas : {
    hasNewRecords : {
          get : function(get){
                var length = get('storeLen') // --> gets the one you set at your controller
                return length > 0 ? true : false;
            }
      }
}

In your View:

bind : {
  disabled : '{hasNewRecords}'
}


来源:https://stackoverflow.com/questions/42955317/extjs-6-bind-disabled-property-to-new-records-in-a-store

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!