ExtJS 6 - 将禁用的属性绑定到商店中的新记录

时间:2017-03-22 14:57:55

标签: extjs extjs6

我试图在商店getNewRecords()函数返回长度时启用/禁用按钮,但不起作用!

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

小提琴:https://fiddle.sencha.com/fiddle/1sj5

有人知道如何解决这个问题吗?

3 个答案:

答案 0 :(得分:3)

您需要在viewmodel中创建一个公式:

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

然后您可以将它用于绑定:

bind: {
    disabled: "{hasNewRecords}"
}

(可能不是获取所需数据的最佳方式)。

您可以阅读hereherehere

答案 1 :(得分:1)

目前在框架中无法实现您想要做的事情。相反,您应该创建一个ViewModel数据值并在需要时进行修改,如下所示:

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()
});

答案 2 :(得分:1)

或者只是这样做。

在您的控制器中:

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

在您的ViewModel中,只需执行以下操作:

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

在您的视图中:

bind : {
  disabled : '{hasNewRecords}'
}