kendo DataSource


1.transport

標准的DataSource格式,默認get請求:

var dataSource = new kendo.data.DataSource({
  transport: {
    read:  {
      url: "https://demos.telerik.com/kendo-ui/service/products",
      dataType: "jsonp" 
    },
    update: {
      url: "https://demos.telerik.com/kendo-ui/service/products/update",
      dataType: "jsonp"  
    }
  }
});

設置為post請求:

var dataSource = new kendo.data.DataSource({
  transport: {
    update: {
      type: "POST"
    }
  }
});

自己寫ajax請求:

注意:如果transport中有一個方法是自己寫的ajax請求,則其他方法也需要使用自己寫的ajax請求,需保持一致。

var dataSource = new kendo.data.DataSource({
  transport: {
    read: function(options) {
    },
    update: function(options) {
      $.ajax({
        url: "https://demos.telerik.com/kendo-ui/service/products/update",
        dataType: "jsonp", 
        data: {
          models: kendo.stringify(options.data.models)
        },
        success: function(result) {
          options.success(result);
        },
        error: function(result) {
          options.error(result);
        }
      });
    }
  }
});

需要傳遞的其他參數data:

//作為對象發送
var dataSource = new kendo.data.DataSource({
  transport: {
    update: {
      cache: true,    //是否緩存請求結果
      contentType: "application/json",  //發送到服務器的內容表頭
      dataType: "json",    //發送服務器的數據類型
      data: {
        name: "Jane Doe",
        age: 30
      }
    }
  }
})
//從函數返回發送其他參數
var dataSource = new kendo.data.DataSource({
  transport: {
    update: {
      data: function() {
        return {
          name: "Jane Doe",
          age: 30
        }
      }
    }
  }
});

2.schema

 model

var dataSource = new kendo.data.DataSource({
  schema:{
       model:{
          id:'',
          fields:{
             name: { type: "number",defaultValue:0, editable: false, nullable: true, validation: { 
                 required: true, min: 1,
                  NoValidation:function(input){
                     if (input.is("[name='UnitPrice']") && input.val() != "") {
                        if(input.val() > 30){
                          input.attr("data-NoValidation-msg", "請輸入小於30數字");
                             return false;
                         }
                     }
                     return true;
                }
              }}
           }
       } 
    }
});             

注意:

  • editable:初始化的時候,控制該列是否可編輯;
  • defaultValue:默認值,只適用於新建的單元格,比如:新增;
  • validation:單元格的驗證,可以寫自定義事件;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM