Custom Editors with Kendo Grid(自定义列的编辑控件)

转自:http://blog.falafel.com/custom-editors-with-kendo-grid/
The Kendo UI Grid makes it easy to display and edit your data in a tabular format.  By default, the grid will use a TextBox for string and numeric data columns and a DropDownList for editing columns with a list of options.  What if you want to use a different editor type?  I’ll demonstrate two examples of using a custom editor for a grid column: MultiSelect and NumericTextBox.

Custom MultiSelect Editor

There are a few steps to use a MultiSelect as a column editor.  The first is to make sure your data is formatted correctly.  Looking at this sample DataSource, we will use a MultiSelect editor for our “countries” property.  If you specify a schema, notice how I do NOT give a type for countries because there is no “array” type and we don’t want Kendo to try to parse our array into a different format.  We want to leave it how it is.

dataSource: new kendo.data.DataSource({
    data: [{
        id: 1,
        product: 'X002',
        countries: ['Mexico''Canada'],
        price: 0.98
    }, {
        id: 2,
        product: 'X036',
        countries: ['United States'],
        price: 2.96
    }, {
        id: 3,
        product: 'X098',
        countries: [],
        price: 45.90
    }],
    schema: {
        model: {
            id: 'id',
            fields: {
                'id': { type: 'number' },
                'product': { type: 'string'},
                'countries': {},
                'price': { type: 'number' }
            }
        }
    }
})

Now we will set up our MultiSelect editor function.  You can specify this within the columns.editor property, or if you plan to use it elsewhere, write it as a separate function.  In this example, we are creating a kendoMultiSelect and appending it to the container object which is a parameter of the editor.  Notice how I set the dataSource to “options.values.”  This is an array that we will specify in the columns attributes.

var multiSelectEditor = function (container, options) {
    $('<input data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoMultiSelect({
           suggest: true,
           dataSource: options.values
    });
};

Next, let’s set up our Grid Columns.  In the abbreviated example below, you will see that we are using our multiSelectEditor function as the editor.  The “values” property isn’t part of the Kendo columns object, but since JavaScript is dynamic, we can add our own property here to pass along the possible values for the editor.  Here I have vm.countries specified which you will see in the full code example that it is just an array of country name strings.  We will look at the template next.

columns: [{
    field: 'countries',
    editor: multiSelectEditor, // function that generates the multiSelect control
    values: vm.countries, // custom property with array of values
    template: multiSelectArrayToString // template: how to display text in grid
}]

In the code example above, I provide a template called multiSelectArrayToString.  Because our column data is an array, we need to provide a template to display the array correctly.  Here we are just doing a .join() to turn our array into a string.

var multiSelectArrayToString = function (item) {
    return item.countries.join(', ');
};

We now have all the pieces we need to display and edit an array as a MultiSelect editor in the grid.

Custom NumericTextBox Editor

Now let’s look at using a NumericTextBox editor instead of a regular TextBox.  This is a bit more straightforward than a MultiSelect, but we’ll follow similar steps.  First, our numeric editor:

var numericEditor = function (container, options) {
    $('<input data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoNumericTextBox({
          decimals: 2,
          min: 0,
          format: 'c2'
    });
};

We will be editing “price” so the format will be “c2″ to show currency with two decimal places.  And finally, we will specify the editor and a format in the columns definition:

columns: [{
    field: 'price',
    editor: numericEditor,
    format: '{0:c}'
}]

Check out the full code on JSFiddle:

运行效果:

[html]
  1. <div id="main">  
  2.     <div id="grid" data-bind="source: dataSource"></div>  
  3. </div>  

[resources]

  1. Following resources are loaded into result:  
  2. kendo.common.min.css  
  3. kendo.default.min.css  
  4. kendo.all.min.js  

[css]

  1. 无  

[javascript]

  1. (function () {  
  2.   
  3.     var multiSelectEditor = function (container, options) {  
  4.         $('<input data-bind="value:' + options.field + '"/>')  
  5.             .appendTo(container)  
  6.             .kendoMultiSelect({  
  7.             suggest: true,  
  8.             dataSource: options.values  
  9.         });  
  10.     };  
  11.   
  12.     var numericEditor = function (container, options) {  
  13.         $('<input data-bind="value:' + options.field + '"/>')  
  14.             .appendTo(container)  
  15.             .kendoNumericTextBox({  
  16.             decimals: 2,  
  17.             min: 0,  
  18.             format: 'c2'  
  19.         });  
  20.     };  
  21.   
  22.     var multiSelectArrayToString = function (item) {  
  23.         return item.countries.join(', ');  
  24.     };  
  25.   
  26.     var vm = kendo.observable({  
  27.         countries: ['Canada', 'Mexico', 'United States'],  
  28.         dataSource: new kendo.data.DataSource({  
  29.             data: [{  
  30.                 id: 1,  
  31.                 product: 'X002',  
  32.                 countries: ['Mexico', 'Canada'],  
  33.                 price: 0.98  
  34.             }, {  
  35.                 id: 2,  
  36.                 product: 'X036',  
  37.                 countries: ['United States'],  
  38.                 price: 2.96  
  39.             }, {  
  40.                 id: 3,  
  41.                 product: 'X098',  
  42.                 countries: [],  
  43.                 price: 45.90  
  44.             }, ],  
  45.             schema: {  
  46.                 model: {  
  47.                     id: 'id',  
  48.                     fields: {  
  49.                         'id': {  
  50.                             type: 'number'  
  51.                         },  
  52.                             'product': {  
  53.                             type: 'string'  
  54.                         },  
  55.                             'countries': {},  
  56.                             'price': {  
  57.                             type: 'number'  
  58.                         }  
  59.                     }  
  60.                 }  
  61.             }  
  62.         }),  
  63.     });  
  64.   
  65.     $('#grid').kendoGrid({  
  66.         columns: [{  
  67.             command: 'edit',  
  68.             width: '120px',  
  69.         }, {  
  70.             field: 'product'  
  71.         }, {  
  72.             field: 'countries',  
  73.             editor: multiSelectEditor, // function that generates the multiSelect control  
  74.             values: vm.countries, // custom property with array of values  
  75.             template: multiSelectArrayToString // template: how to display text in grid  
  76.         }, {  
  77.             field: 'price',  
  78.             editor: numericEditor,  
  79.             format: '{0:c}'  
  80.         }],  
  81.         editable: 'inline',  
  82.   
  83.     });  
  84.   
  85.     kendo.bind('#main', vm);  
  86.   
  87. })() 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CNC-Editors是一种先进的计算机数控(CNC)编辑器,主要用于编辑和编程控制机器的运动轨迹和操作过程。它具有一系强大的功能,能够有效地提高生产效率和质量。 首先,CNC-Editors具有用户友好的界面和直观的操作方式,使得操作者能够轻松进行编辑和编程。它通常采用图形化界面,提供了丰富的操作工具和选项,使用户能够轻松创建和调整机器的运动轨迹和操作方式。 其次,CNC-Editors支持多种编程语言和指令格式,如G代码和M代码,以及各种机器品牌的独立指令。这意味着用户可以根据机器的需求选择最合适的编程语言,并根据其特定的指令格式进行编辑和编程。 此外,CNC-Editors还具有强大的仿真和模拟功能。用户可以通过软件中的仿真工具,在计算机上模拟机器的运行过程,查看运动轨迹和操作步骤。这种功能使得用户可以在实际操作前进行调试和优化,减少了出错的可能性。 最后,CNC-Editors还支持多种数据传输和接口,可与其他软件和设备进行互联互通。用户可以将编辑好的程序通过网络或传输设备发送给机器,实现远程编辑和控制。 总之,CNC-Editors是一种功能强大的编辑器,能够帮助用户轻松编辑和编程控制机器的运动轨迹和操作过程。它的用户友好界面、多种编程语言支持、强大的仿真功能以及数据传输接口,使其成为提高生产效率和质量的重要工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值