2014年6月9日 星期一

kendo ui grid 如何使用numeric 元件

開發單位尋問說如何讓grid的欄位透過JS的方式顯示numeric的元件
所以就簡單寫了一個SAMPLE
 
<!DOCTYPE html>
<html>
  <head>
    <title>KendoUI Test Page</title>
    <link href="http://cdn.kendostatic.com/2013.2.716/styles/kendo.common.min.css" rel="stylesheet" />
    <link href="http://cdn.kendostatic.com/2013.2.716/styles/kendo.default.min.css" rel="stylesheet" />
    <link href="http://cdn.kendostatic.com/2013.2.716/styles/kendo.dataviz.min.css" rel="stylesheet" />
    <link href="http://cdn.kendostatic.com/2013.2.716/styles/kendo.mobile.all.min.css" rel="stylesheet" />
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://cdn.kendostatic.com/2013.2.716/js/kendo.all.min.js"></script>          
  </head>
  <body>
    <div id="example" class="k-content">
      <div id="grid"></div>
      <script id="slotsTemplate" type="text/x-kendo-tmpl"> 
        <input class="numeric" type="number" value="#= UnitsInStock #" step="1" />
      </script>
      
      <script>
        $(document).ready(function () {
          var crudServiceBaseUrl = "http://demos.kendoui.com/service",
              dataSource = new kendo.data.DataSource({
                transport: {
                  read:  {
                    url: crudServiceBaseUrl + "/Products",
                    dataType: "jsonp"
                  },
                  update: {
                    url: crudServiceBaseUrl + "/Products/Update",
                    dataType: "jsonp"
                  },
                  destroy: {
                    url: crudServiceBaseUrl + "/Products/Destroy",
                    dataType: "jsonp"
                  },
                  create: {
                    url: crudServiceBaseUrl + "/Products/Create",
                    dataType: "jsonp"
                  },
                  parameterMap: function(options, operation) {
                    if (operation !== "read" && options.models) {
                      return {models: kendo.stringify(options.models)};
                    }
                  }
                },
                batch: true,
                pageSize: 20,
                schema: {
                  model: {
                    id: "ProductID",
                    fields: {
                      ProductID: { editable: false, nullable: true },
                      ProductName: { validation: { required: true } },
                      UnitPrice: { type: "number" },
                      Discontinued: { type: "boolean" },
                      UnitsInStock: { type: "number", validation: { min: 0, required: true } }
                    }
                  }
                }
              });
          
          $("#grid").kendoGrid({
            dataSource: dataSource,
            dataBound: function(){
               $(".numeric").kendoNumericTextBox();
            },
            pageable: true,
            height: 430,
            toolbar: ["create"],
            columns: [
              "ProductName",
              { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "100px" },
              { field: "UnitsInStock", title:"Units In Stock", template: kendo.template($("#slotsTemplate").html()) },
              { field: "Discontinued", width: "100px" },
              { command: ["edit", "destroy"], title: "&nbsp;", width: "172px" }],
            editable: "inline"
          });
        });
      </script>
    </div> 
 
  </body>
</html>

以上
附上檔案

沒有留言:

張貼留言