Kendo网格编辑弹出窗口mvvm绑定似乎失败

时间:2019-03-29 02:43:14

标签: kendo-ui kendo-mvvm

当我添加新行并在edit eventHandler(model.set(“ port”,“ udp”))中设置模型属性的值时,网格弹出窗口mvvm绑定似乎失败,但是select输入不会更改值,并且调试器控制台它显示event.model的属性值保持不变。

但是在我手动选择端口后,我尝试对model.set(“ port”,“ tcp”)进行设置!

我对输入元素的值字段的绑定机制与kendo模型绑定的时序感到困惑...

HTML部分如下:

<div class="k-block k-shadow">
<table style="width:100%;">
<tr><td><button class="btn-add" id="ButtonNew" type="button" onclick="addSetting()">New</button></td></tr></table>
</div>
<div class="k-block k-shadow">
  <div id="logForwardingGrid"></div>
</div>

javascript如下:

var dataSource = new kendo.data.DataSource({
  data:{"items":[{"seq":8,"ip":"10.0.0.99","port":36,"protocol":"udp","enable":false}]},
  schema: {
    data: "items",
    model: {
      id: "seq",
      fields: {
        seq: { type: "int", editable: false, validation: { required: false} },
        port: { type: "int",editable: true, validation: { required: true} },
        protocol: { type: "string",editable: true, validation: { required: true} },
        enable: { type: "boolean", validation: { required: true} },
      }
    }
  },
});
$("#logForwardingGrid").kendoGrid({
  dataSource: dataSource,
  columns: [{
    field: "seq",
    hiddenEdit: true,
    hidden: true
  }, {
    field: "ip",
    title: "IP",
  }, {
    field: "port",
    title: "Port",
  }, {
    field: "protocol",
    title: "Protocol",
    editor: protocalEditor
  }],
  editable: {
    mode:"popup",
  },
  edit: function(e) {
    if (e.model.isNew()) {
        //fail !! e.model's value of property("protocol") keep unchanged
       e.model.set("protocol","udp")
       console.log(e.model)
    }  
  }
});
    function protocalEditor(container,options){
  var data = [{ text: "udp", value: "udp" },{ text: "tcp", value: "tcp" }]
  $('<input id="protocol" name="protocol" required  data-bind="value:'+ options.field +'"/>')
        .appendTo(container).kendoDropDownList({
        dataSource:data,
        dataTextField: "text",
        dataValueField: "value",
  });

}

function addSetting(){
  $("#logForwardingGrid").data("kendoGrid").addRow();
}

1 个答案:

答案 0 :(得分:0)

尝试为模型声明中的字段提供defaultValue

var dataSource = new kendo.data.DataSource({
  data:{"items": [{"seq":8,"ip":"10.0.0.99","port":36,"protocol":"udp","enable":false}]},
  schema: {
    data: "items",
    model: {
      id: "seq",
      fields: {
        seq: { type: "int", editable: false, validation: { required: false} },
        port: { type: "int",editable: true, validation: { required: true} },
        protocol: { type: "string",editable: true, validation: { required: true}, defaultValue: "tcp" },
        enable: { type: "boolean", validation: { required: true} },
      }
    }
  },
});

我不知道为什么,但这似乎可以解决问题。有关工作示例,请参见dojo