Kendo grid:如何创建在添加新行而不是在编辑上执行某些操作

时间:2017-08-31 07:12:07

标签: javascript jquery kendo-ui datagrid kendo-grid

在我的KendoGrid中,我想在弹出窗体中添加输入字段的默认值。

我创建了一个我打算在单击“创建”按钮时调用的函数,但以下函数不起作用。我搜索了很多但找不到任何帮助,所以如果有人能给我一个暗示问题的提示,那就太好了。

def num_doublings(initial_population, final_population):
   time = 0
   while initial_population < final_population:
       time+= 1
       initial_population = initial_population * 2
   print(time)

3 个答案:

答案 0 :(得分:0)

动态分配val或属性

edit

edit: function(e) {
    if (e.model.isNew()) {
       e.container.find("input[name=test]").val(5555); // name changed, but worked for me
      // e.container.find("input[name=device_id]").val(123);
    }
}

beforeEdit

beforeEdit: function(e) {
    if (e.model.isNew()) {
      $("#DeviceIP").val("123");
    }
}

答案 1 :(得分:0)

您可以使用beforeEdit事件,而不是create。它会在工具栏中单击create按钮时触发。

答案 2 :(得分:0)

这是工作DEMO

以下是在DeviceIP上粘贴Add row event默认值的代码段。

$("#turbingrid").kendoGrid({
....
.........
//ON CLICK ADD/EDIT BUTTON FOR PRODUCT ITEM
        edit: function(e) {

            // CHANGE ADD/EDIT PRODUCTITEM POPUP FORM TITLE
            if (e.model.isNew()) //ON ADD NEW
            {
                $(".k-window-title").text("Add New Turbine");
                // HERE e.container IS THE ADD/EDIT POPUP FORM ELEMENT
                e.container
                    .find("[name=DeviceIP]") // get the span element for the field
                    .val("123") // set the value
                .change(); // trigger change in order to notify the model binding

            } 
            else // ON EDIT
            {
                $(".k-window-title").text("Edit Turbine");
            }
        }
........
....
});

以下是DEMO小提琴的完整代码。

(function () {



      var dataSource = new kendo.data.DataSource({
            data: {
                id: 1,
                DeviceIP: "192.168.1.1",
                Producer: 'Producer',
                Model: 'Model',
                DeviceType: 'DeviceType',
                Description: 'Description',
                Username: 'Username',
                Password: 'Password',
                PublicIP: '216.168.123.156',
                TurbineId: 1,
                device_id: 2,
                ModelProducer: 'ModelProducer',
            },
            schema: {
                model: {
                    id: 'id',
                    fields: {
                        DeviceIP: {},
                        Producer: {},
                        Model: {},
                        DeviceType: {},
                        Description: {},
                        Username: {},
                        Password: {},
                        PublicIP: {},
                        TurbineId: {},
                        device_id: {},
                        ModelProducer: {},

                    }
                }
            }
        });


    $('#grid').kendoGrid({
     dataSource: dataSource,
     scrollable: false,
     //toolbar: ["create"],
     toolbar: [
                  {name: "create",text: "add new turbine"}
              ],
     columns: [
                  { field: 'DeviceIP', title: 'DeviceIP', width: '100px', id: 'DeviceIP' },
                  { field: 'Producer', title: 'Producer', width: '80px', id:'Producer'},//editor: ProductNameDropDownEditor,
                  { field: 'Model', title: 'Model', width: '220px',id:'Model' },
                  { field: 'DeviceType', title: 'DeviceType', width: '100px' },
                  { field: 'Description', title: 'Description', width: '220px' },
                  { field: 'Username', title: 'Username', width: '120px' },
                  { field: 'Password', title: 'Password', width: '100px' },
                  { field: 'PublicIP', title: 'PublicIP', width: '120px' },
                  { field: 'TurbineId', title: 'TurbineId', width: '120px', hidden: true },
                  { field: 'device_id', title: 'device_id', width: '120px', hidden: true },
                  { field: 'ModelProducer', title: 'Producer/Model', hidden: true },
                  {command: ["edit"], title: "&nbsp;"}
             ],
        editable: 'popup',
        //ON CLICK ADD/EDIT BUTTON FOR PRODUCT ITEM
        edit: function(e) {

            // CHANGE ADD/EDIT PRODUCTITEM POPUP FORM TITLE
            if (e.model.isNew()) //ON ADD NEW
            {
                $(".k-window-title").text("Add New Turbine");
                // HERE e.container IS THE ADD/EDIT POPUP FORM ELEMENT
                e.container
                    .find("[name=DeviceIP]") // get the span element for the field
                    .val("123") // set the value
                .change(); // trigger change in order to notify the model binding

            } 
            else // ON EDIT
            {
                $(".k-window-title").text("Edit Turbine");
            }
        }
    });
})()