DataSet

2024-03-07 18:38 更新

DataSet最主要的功能就是為一份數(shù)據(jù)賦予一個名稱,使這份數(shù)據(jù)可以以配置的方式被其他控件引用,以便于建立綁定關(guān)系。 在定義DataSet時,我們可以選擇以下兩種定義數(shù)據(jù)的方式之一。

直接設(shè)置數(shù)據(jù)

直接把最終的數(shù)據(jù)交給DataSet。交給DataSet的數(shù)據(jù)的載體即可以是JSON+Array,也可以是Entity+EntityList。不過他們最終都會被包裝成Entity+EntityList

JSON設(shè)置數(shù)據(jù)

dataSet.setData({
   "id":1,
    "productName":"Chai1",
    "categoryId":1,
    "discontinued":true,
    "quantityPerUnit":"10 boxes x 20 bags",
    "reorderLevel":12,
    "unitPrice":18,
    "unitsInStock":39,
    "unitsOnOrder":0
});

Array設(shè)置數(shù)據(jù)

dataSet.setData([{
    "id":1,
    "productName":"Chai1",
    "categoryId":1,
    "discontinued":true,
    "quantityPerUnit":"10 boxes x 20 bags",
    "reorderLevel":12,
    "unitPrice":18,
    "unitsInStock":39,
    "unitsOnOrder":0
},{
    "id":2,
    "productName":"Chang",
    "categoryId":1,
    "discontinued":true,
    "quantityPerUnit":"24 - 12 oz bottles",
    "reorderLevel":25,
    "unitPrice":19,
    "unitsInStock":17,
    "unitsOnOrder":40
}]);

Entity設(shè)置數(shù)據(jù)

var entity = new dorado.Entity();
entity.fromJSON({
    "id":1,
    "productName":"Chai1",
    "categoryId":1,
    "discontinued":true,
    "quantityPerUnit":"10 boxes x 20 bags",
    "reorderLevel":12,
    "unitPrice":18,
    "unitsInStock":39,
    "unitsOnOrder":0
});
dataSet.setData(entity);

EntityList設(shè)置數(shù)據(jù)

var entityList = new dorado.EntityList();
entityList.fromJSON([{
    "id":1,
    "productName":"Chai1",
    "categoryId":1,
    "discontinued":true,
    "quantityPerUnit":"10 boxes x 20 bags",
    "reorderLevel":12,
    "unitPrice":18,
    "unitsInStock":39,
    "unitsOnOrder":0
},{
    "id":2,
    "productName":"Chang",
    "categoryId":1,
    "discontinued":true,
    "quantityPerUnit":"24 - 12 oz bottles",
    "reorderLevel":25,
    "unitPrice":19,
    "unitsInStock":17,
    "unitsOnOrder":40
}]);
dataSet.setData(entityList);

通過DataProvider定義數(shù)據(jù)

數(shù)據(jù)的提取方式告訴DataSet。這要利用DataProvider,我們接下來將會介紹這種對象。如果使用這種方式來定義DataSet中的數(shù)據(jù),那么DataSet將擁有數(shù)據(jù)懶裝載的特性,因此直到外界試圖通過DataSet提取數(shù)據(jù)時,相應(yīng)的DataProvider才會被真正的激活。

dataSet.set("dataProvider", new dorado.AjaxDataProvider("data/phones.js"));

不過大部分場景我們還是直接在IDE設(shè)計器中定義DataProvider:

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號