1) // create 最基本建立Record和Store的方法
var myStore = new Ext.data.Store();
var TopicRecord = Ext.data.Record.create([
{name: 'title', mapping: 'topic_title'},
{name: 'author', mapping: 'username'},
{name: 'totalPosts', mapping: 'topic_replies', type: 'int'},
{name: 'lastPost', mapping: 'post_time', type: 'date'},
{name: 'lastPoster', mapping: 'user2'},
{name: 'excerpt', mapping: 'post_text'}
]); // 列表影射,columnModel
var myNewRecord = new TopicRecord({
title: 'Do my job please',
author: 'noobie',
totalPosts: 1,
lastPost: new Date(),
lastPoster: 'Animal',
excerpt: 'No way dude!'
}); // 實(shí)際的一條記錄
myStore.add(myNewRecord);
2) // 這種方法也可以構(gòu)建一個(gè)store
var store = new Ext.data.SimpleStore({
fields: ['abbr', 'state', 'nick'], // 一定要有這個(gè),如果放在grid中,與column的dataIndex屬性值對應(yīng)
data : [
['AL', 'Alabama', 'The Heart of Dixie'],
['AK', 'Alaska', 'The Land of the Midnight Sun'],
['IN', 'Indiana', 'The Hospitality State']
]
});
3) 還有一個(gè)種方法,用ArrayReader,數(shù)據(jù)源是js二維數(shù)組,用SimpleStore
var store = new Ext.data.SimpleStore({
/*
數(shù)據(jù)源:[ [1, 'AL', '0.1', 'The Heart of Dixie', '6', new Date()] ]
ArrayReader中, ajax會返回一個(gè)數(shù)據(jù)源給ArrayReader,
fields數(shù)組與數(shù)據(jù)源的序列的順序要相同
*/
fields: ['pid', 'company', 'price', 'change', 'pctChange', 'lastChange'],
url: 't2.php',
reader: new Ext.data.ArrayReader(
{id: 0},
/*
id 的取值應(yīng)為下面的mapping的值,如果在下面沒有出現(xiàn),則取數(shù)據(jù)源的0序列
,recordid可通過store.getById(recordid) 取得相應(yīng)的 Record
當(dāng)有 id 這個(gè)屬性設(shè)置的時(shí)候,如果id值為2,就表示用price作recordid
, 如果 name 的值與數(shù)據(jù)源的序列的名稱相同,就可以不指定mapping
*/
[{name:'company',mapping: 1}, {name:'price',mapping: 2}, {name:'change',mapping: 3}, {name:'pctChange',mapping: 4}, {name:'lastChange',mapping: 5}]
)
});
// 因?yàn)槭褂胾rl會觸發(fā)ajax, 是異步方式,如果是要ajax讀取數(shù)據(jù)后馬上觸發(fā)事件,
// 必須添加load事件的監(jiān)聽
store.addListener('load', function(st, rds, opts) {
// st 是當(dāng)前的store, rds是讀到的Record[], opts是store的配置
//alert(rds.getTotalCount());
nextstore.removeAll(); //先清除另一個(gè)store的內(nèi)容
nextstore.add(rds); // 給另一個(gè)store添加這些records
//for( var c=0; c<rds.length; c++ ) store.addSorted(rds[c]);
});
store.load();
4) 還有一個(gè)種方法,用XmlReader,數(shù)據(jù)源是xml, 只能用Ext.data.Store,不能用SimpleStore
就因?yàn)镾impleStore,害我搞了n久才發(fā)現(xiàn)只能用Store
先看數(shù)據(jù)源:
<?xml?>
<dataset>
<results>2</results>
<row>
<id>1</id>
<name>Bill</name>
<occupation>Gardener</occupation>
</row>
<row>
<id>2</id>
<name>Ben</name>
<occupation>Horticulturalist</occupation>
</row>
</dataset>
js:
var store = new Ext.data.Store({
fields: ['id', 'name', 'occupation'],
url: 't2.php',
reader: new Ext.data.XmlReader(
{
record: "row", // xml中每行數(shù)據(jù)的內(nèi)容
totalRecords: "results", // xml中的results節(jié)點(diǎn),表時(shí)記錄數(shù) (option)
id: 'id'
// 每行數(shù)據(jù)的素引,xml中是row標(biāo)簽的子節(jié)點(diǎn), 作recordid, (option),
// 如果id列在數(shù)據(jù)源中的值有重復(fù),就只會顯前面的列,后面的放棄.
},
/*
id 的取值應(yīng)為下面的mapping的值,如果在下面沒有出現(xiàn),則取數(shù)據(jù)源的0序列
當(dāng)有 id 這個(gè)屬性設(shè)置的時(shí)候,如果id值為2,就表示用price作recordid
下面的mapping,是定義的素引與xml中的節(jié)點(diǎn)對應(yīng)
, 如果 name 的值與數(shù)據(jù)源的序列的名稱相同,就可以不指定mapping
*/
[{name:'name',mapping: 'name'}, {name:'occupation'} ]
// 這里也可寫成 ['id', 'name', 'occupation']
)
});
// 因?yàn)槭褂胾rl會觸發(fā)ajax, 是異步方式,如果是要ajax讀取數(shù)據(jù)后馬上觸發(fā)事件,
// 必須添加load事件的監(jiān)聽
store.addListener('load', function(st, rds, opts) {
// st 是當(dāng)前的store, rds是讀到的Record[], opts是store的配置
//alert(rds.getTotalCount());
nextstore.removeAll(); //先清除另一個(gè)store的內(nèi)容
nextstore.add(rds); // 給另一個(gè)store添加這些records
//for( var c=0; c<rds.length; c++ ) store.addSorted(rds[c]);
});
store.load();
----------------http://cwq.jsp-tech.cn/2008/04/extjsrecordstore.html