ExtJs中关于grid和store的应用分析(三)

第三部分:解析器与数据的关系


一、XmlReader的构造方法定义如下:

Js代码 复制代码
  1. XmlReader( Object meta, Object recordType )  
XmlReader( Object meta, Object recordType )



二、JsonReader的构造方法定义如下:

Js代码 复制代码
  1. JsonReader( Object meta, Object recordType )   
JsonReader( Object meta, Object recordType ) 



三、ArrayReader的构造方法定义如下:

Js代码 复制代码
  1. ArrayReader( Object meta, Object recordType )   
ArrayReader( Object meta, Object recordType ) 



都表示实例化这些解析器时,需要传递2个参数,一个是meta,一个是recordType。
但是不同的解析器他们需要的meta的值为不同的。只需看各自的Config Options(即配置选项)。也就是说,meta是由配置选项组成的。而recordType是指通过Recorde.create方法得到的数据结构的定义。一般也可以为var fields = ["id","name","email","sex","age"];这样定义的fields。

一、XmlReader所接受的配置选项有:

id :
其值是作为数据唯一标志的字符串,必须为fields中某列的name值。如:id:"id",也可以是id:"name",但是这个时候,字段name的值要确保是能唯一标志数据的。
record :
其值为可以指定数据的字符串。如:record:"row"
success :
totalRecords :
其值为可以指定数据总数的字符串。如:totalRecords:"total"

二、JsonReader所接受的配置选项有:

id :
同XmlReader
root :
其值为可以指定数据的字符串。如:root:"result"
successProperty :
totalProperty :
其值为可以指定数据总数的字符串。如:totalProperty:"total"

三、ArrayReader所接受的配置选项有:

id :
同XmlReader

现在再来看一下解析器与数据的关系。
数据只要满足了解析器的要求,那么数据就能获取成功。什么叫满足解析器的要求呢?有两种方式均可满足解析器的要求。
第一种:给予的数据不包含metaData
第二种:给予的数据包含了metaData

ArrayReader和XmlReader需要的数据是没有metaData的 ,此时给予的数据需要包含以下内容:
1、ArrayReader:
比如ArrayReader如下定义:
var fields = ["id","name","email","sex","age"];
var reader = new Ext.data.ArrayReader({
  id:"id"
},fields);

给予的数据应该如下:

Js代码 复制代码
  1. [[   
  2.      "1" , "langsin1" , "[email protected]" , "0" , "20"   
  3. ],[   
  4.      "2" , "langsin2" , "[email protected]" , "1" , "21"   
  5. ],[   
  6.      "3" , "langsin3" , "[email protected]" , "0" , "22"   
  7. ],[   
  8.      "4" , "langsin4" , "[email protected]" , "1" , "23"   
  9. ],[   
  10.      "5" , "langsin5" , "[email protected]" , "0" , "24"   
  11. ]]  
[[
	"1","langsin1","[email protected]","0","20"
],[
	"2","langsin2","[email protected]","1","21"
],[
	"3","langsin3","[email protected]","0","22"
],[
	"4","langsin4","[email protected]","1","23"
],[
	"5","langsin5","[email protected]","0","24"
]]


也就是必须为二维数组。
此时二维数组中的每列的数据顺序与fields中的字段名的定义是从左向右一一匹配,即,这里有5条数据,第一条数据是:
id的值为"1",
name的值为"langsin1",
email的值为"[email protected]",
sex的值为"0",
age的值为"20"

2、XmlReader
比如XmlReader如下定义:
var fields = ["id","name","email","sex","age"];
var reader = new Ext.data.XmlReader({
  id:"id",
  record:"row",
  totalRecords:"results"
},fields);

给予的数据应该如下:

Xml代码 复制代码
  1. <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < dataset >   
  3.      < results > 2 </ results >   
  4.      < row >   
  5.          < id > 1 </ id >   
  6.          < name > langsin1 </ name >   
  7.          < email > [email protected] </ email >   
  8.          < sex > 0 </ sex >   
  9.          < age > 20 </ age >   
  10.      </ row >   
  11.      < row >   
  12.          < id > 2 </ id >   
  13.          < name > langsin2 </ name >   
  14.          < email > [email protected] </ email >   
  15.          < sex > 1 </ sex >   
  16.          < age > 21 </ age >   
  17.      </ row >   
  18.      < row >   
  19.          < id > 3 </ id >   
  20.          < name > langsin3 </ name >   
  21.          < email > [email protected] </ email >   
  22.          < sex > 0 </ sex >   
  23.          < age > 22 </ age >   
  24.      </ row >   
  25.      < row >   
  26.          < id > 4 </ id >   
  27.          < name > langsin4 </ name >   
  28.          < email > [email protected] </ email >   
  29.          < sex > 1 </ sex >   
  30.          < age > 23 </ age >   
  31.      </ row >   
  32.      < row >   
  33.          < id > 5 </ id >   
  34.          < name > langsin5 </ name >   
  35.          < email > [email protected] </ email >   
  36.          < sex > 0 </ sex >   
  37.          < age > 24 </ age >   
  38.      </ row >   
  39. </ dataset >   
<?xml version="1.0" encoding="utf-8"?>
<dataset>
	<results>2</results>
	<row>
		<id>1</id>
		<name>langsin1</name>
		<email>[email protected]</email>
		<sex>0</sex>
		<age>20</age>
	</row>
	<row>
		<id>2</id>
		<name>langsin2</name>
		<email>[email protected]</email>
		<sex>1</sex>
		<age>21</age>
	</row>
	<row>
		<id>3</id>
		<name>langsin3</name>
		<email>[email protected]</email>
		<sex>0</sex>
		<age>22</age>
	</row>
	<row>
		<id>4</id>
		<name>langsin4</name>
		<email>[email protected]</email>
		<sex>1</sex>
		<age>23</age>
	</row>
	<row>
		<id>5</id>
		<name>langsin5</name>
		<email>[email protected]</email>
		<sex>0</sex>
		<age>24</age>
	</row>
</dataset>


这里dataset标签可以换车其他任何字母组成的标签,如aaa,不会影响最后数据的读取。
但是这个dataset标签对里就必须要有1个或多个的results和row标签对。

现在再来看下JsonReader。
JsonReader可以用第一种也可以用第二种的数据给予方式。
1、给予的数据不包含metaData的情况时:
比如JsonReader如下定义:
var fields = ["id","name","email","sex","age"];
var reader = new Ext.data.JsonReader({
  id:"id",
  root:"result",
  totalProperty:"total"
},fields);

给予的数据应该如下:

Js代码 复制代码
  1. {   
  2.     total:5,   
  3.     result:[{   
  4.         id: "1" ,   
  5.         name: "langsin1" ,   
  6.         email: "[email protected]" ,   
  7.         sex: "0" ,   
  8.         age: "20"   
  9.     },{   
  10.         id: "2" ,   
  11.         name: "langsin2" ,   
  12.         email: "[email protected]" ,   
  13.         sex: "1" ,   
  14.         age: "21"   
  15.     },{   
  16.         id: "3" ,   
  17.         name: "langsin3" ,   
  18.         email: "[email protected]" ,   
  19.         sex: "0" ,   
  20.         age: "22"   
  21.     },{   
  22.         id: "4" ,   
  23.         name: "langsin4" ,   
  24.         email: "[email protected]" ,   
  25.         sex: "1" ,   
  26.         age: "23"   
  27.     },{   
  28.         id: "5" ,   
  29.         name: "langsin5" ,   
  30.         email: "[email protected]" ,   
  31.         sex: "0" ,   
  32.         age: "24"   
  33.     }]   
  34. }  
{
	total:5,
	result:[{
		id:"1",
		name:"langsin1",
		email:"[email protected]",
		sex:"0",
		age:"20"
	},{
		id:"2",
		name:"langsin2",
		email:"[email protected]",
		sex:"1",
		age:"21"
	},{
		id:"3",
		name:"langsin3",
		email:"[email protected]",
		sex:"0",
		age:"22"
	},{
		id:"4",
		name:"langsin4",
		email:"[email protected]",
		sex:"1",
		age:"23"
	},{
		id:"5",
		name:"langsin5",
		email:"[email protected]",
		sex:"0",
		age:"24"
	}]
}


如果定义时不包含root配置选项时,那么给予的数据应该如下:

Js代码 复制代码
  1. [{   
  2.     id: "1" ,   
  3.     name: "langsin1" ,   
  4.     email: "[email protected]" ,   
  5.     sex: "0" ,   
  6.     age: "20"   
  7. },{   
  8.     id: "2" ,   
  9.     name: "langsin2" ,   
  10.     email: "[email protected]" ,   
  11.     sex: "1" ,   
  12.     age: "21"   
  13. },{   
  14.     id: "3" ,   
  15.     name: "langsin3" ,   
  16.     email: "[email protected]" ,   
  17.     sex: "0" ,   
  18.     age: "22"   
  19. },{   
  20.     id: "4" ,   
  21.     name: "langsin4" ,   
  22.     email: "[email protected]" ,   
  23.     sex: "1" ,   
  24.     age: "23"   
  25. },{   
  26.     id: "5" ,   
  27.     name: "langsin5" ,   
  28.     email: "[email protected]" ,   
  29.     sex: "0" ,   
  30.     age: "24"   
  31. }]  
[{
	id:"1",
	name:"langsin1",
	email:"[email protected]",
	sex:"0",
	age:"20"
},{
	id:"2",
	name:"langsin2",
	email:"[email protected]",
	sex:"1",
	age:"21"
},{
	id:"3",
	name:"langsin3",
	email:"[email protected]",
	sex:"0",
	age:"22"
},{
	id:"4",
	name:"langsin4",
	email:"[email protected]",
	sex:"1",
	age:"23"
},{
	id:"5",
	name:"langsin5",
	email:"[email protected]",
	sex:"0",
	age:"24"
}]



2、给予的数据包含metaData的情况时:
比如JsonReader如下定义:
var reader = new Ext.data.JsonReader({
  id:"id",
},fields);

给予的数据应该如下:

Js代码 复制代码
  1. {   
  2.     metaData:{   
  3.         fields:[ "id" , "name" , "email" , "sex" , "age" ],   
  4.         root: "result"   
  5.     },   
  6.     result : [{   
  7.         id :  "1" ,   
  8.         name :  "langsin1" ,   
  9.         email :  "[email protected]" ,   
  10.         sex :  "0" ,   
  11.         age :  "20"   
  12.     }, {   
  13.         id :  "2" ,   
  14.         name :  "langsin2" ,   
  15.         email :  "[email protected]" ,   
  16.         sex :  "1" ,   
  17.         age :  "21"   
  18.     }, {   
  19.         id :  "3" ,   
  20.         name :  "langsin3" ,   
  21.         email :  "[email protected]" ,   
  22.         sex :  "0" ,   
  23.         age :  "22"   
  24.     }, {   
  25.         id :  "4" ,   
  26.         name :  "langsin4" ,   
  27.         email :  "[email protected]" ,   
  28.         sex :  "1" ,   
  29.         age :  "23"   
  30.     }, {   
  31.         id :  "5" ,   
  32.         name :  "langsin5" ,   
  33.         email :  "[email protected]" ,   
  34.         sex :  "0" ,   
  35.         age :  "24"   
  36.     }]   
  37. }  
{
	metaData:{
		fields:["id","name","email","sex","age"],
		root:"result"
	},
	result : [{
		id : "1",
		name : "langsin1",
		email : "[email protected]",
		sex : "0",
		age : "20"
	}, {
		id : "2",
		name : "langsin2",
		email : "[email protected]",
		sex : "1",
		age : "21"
	}, {
		id : "3",
		name : "langsin3",
		email : "[email protected]",
		sex : "0",
		age : "22"
	}, {
		id : "4",
		name : "langsin4",
		email : "[email protected]",
		sex : "1",
		age : "23"
	}, {
		id : "5",
		name : "langsin5",
		email : "[email protected]",
		sex : "0",
		age : "24"
	}]
}



这里将root和fields等都转移到了给予的数据的metaData中。

你可能感兴趣的:(数据结构,xml,ext)