Commonly used reader in Ext. Data

Source: Internet
Author: User

1. arrayreader

The data read from the proxy must be parsed. The data is converted to a record array before it can be used by Ext. Data. Store.

Arrayreader reads data from two-dimensional arrays sequentially and generates the corresponding record. By default, data in the array is read in the column order, but you can also use mapping to specify the column number corresponding to the record and the original array. The usage of arrayreader is simple, but the disadvantage is that pagination is not supported. The following describes how to use a two-dimensional array:Code.

VaR DATA = [

['Id1', 'name1', 'scn1 '],

['Id2', 'name2', 'scn2 ']

];

The corresponding arrayreader is shown in the following code.

VaR reader = new Ext. Data. arrayreader ({

ID: 1

},[

{Name: 'name', mapping: 1 },

{Name: 'desc', mapping: 2 },

{Name: 'id', mapping: 0 },

]);

We show that the field order is inconsistent. If the field order is the same as the column order, you do not need to configure mapping.

2. jsonreader

In JavaScript, JSON is a very important data format. The form of key: value is easier to understand than the complicated tag structure of XML, and the amount of code is smaller, many people tend to use it as the data exchange format of Ext. The code below shows the JSON data prepared for JSON-reader.

VaR DATA = {

ID: 0,

Totalproperty: 2,

Successproperty: True,

Root :[

{ID: 'id1', name: 'name1', descn: 'scn1 '},

{ID: 'id2', name: 'name2', descn: 'scn2 '}

]

};

Compared with arrays, JSON supports pagination. The totalproperty parameter can be used to represent the total amount of data. The successproperty parameter is optional. You can use it to determine whether the current request is successfully executed and whether data is loaded. If you do not want jsonreader to process the response data, you can set successproperty to false.

Now let's take a look at jsonreader to see how it corresponds to the above JSON data, as shown in the following code.

VaR reader = new Ext. Data. jsonreader ({

Successproperty: "successproperty ",

Totalproperty: "totalproperty ",

Root: "root ",

ID: "ID"

},[

{Name: 'id', mapping: 'id '},

{Name: 'name', mapping: 'name '},

{Name: 'desc', mapping: 'desc '}

]);

In the preceding example, the ing method is not concise, because the content of name and mapping is the same. In fact, mapping can be omitted here. By default, the name parameter is used to obtain the corresponding data from JSON. If you do not want to use the same name as in JSON, you can use mapping to modify the name. However, mapping has other purposes here, as shown in the code list.

The code list sets mapping for jsonreader for data ing

VaR DATA = {

ID: 0,

Totalproperty: 2,

Successproperty: True,

Root :[

{ID: 'id1', name: 'name1', descn: 'scn1 ', person :{

ID: 1, name: 'man ', sex: 'male'

}},

{ID: 'id2', name: 'name2', descn: 'scn2 ', person :{

ID: 2, name: 'Woman ', sex: 'female'

}}

]

};

VaR reader = new Ext. Data. jsonreader ({

Successproperty: "successproperty ",

Totalproperty: "totalproperty ",

Root: "root ",

ID: "ID"

},[

'Id', 'name', 'desc ',

{Name: 'person _ name', mapping: 'person. name '},

{Name: 'person _ sex ', mapping: 'person. Sex '}

]);

In the code above, we use JSON to support more complex nested structures. The person object itself has attributes such as ID, name, and sex. In jsonreader, you can use mapping to map these nested internal attributes to the corresponding record, while other fields remain unchanged.

3. xmlreader

XML is a very common data transmission format. The XML format data used by xmlreader is shown in the code list.

Code List data in XML format used by xmlreader

<? XML version = "1.0" encoding = "UTF-8"?>

<Dataset>

<ID> 1 </ID>

<Totalrecords> 2 </Totalrecords>

<Success> True </Success>

<Record>

<ID> 1 </ID>

<Name> Name1 </Name>

<Descn> Descn1 </Descn>

</Record>

<Record>

<ID> 2 </ID>

<Name> Name2 </Name>

<Descn> Descn2 </Descn>

</Record>

</Dataset>

 

It must be used hereDatasetAsXMLRoot element. Let's take a look at howXmlreaderTo readXMLData, as shown in the following code.

 

VaRReader =NewExt. Data. xmlreader ({

Totalrecords:'Totalrecords',

Success:'Success'

Record:'Record',

ID:"ID"

},['Id','Name','Desc']);

Dataset must be used as the XML root element. Let's take a look at how to configure xmlreader to read the XML data in the example above, as shown in the following code.

VaR reader = new Ext. Data. xmlreader ({

Totalrecords: 'totalrecords ',

Success: 'success'

Record: 'record ',

ID: "ID"

}, ['Id', 'name', 'desc']);

The parameters used by xmlreader are somewhat different from those described in jsonreader. We can see that totalrecords and record are used here. totalrecords is used to specify the total number of background data obtained from the 'totalrecords 'tag, record indicates that the data placed in the record label in XML is the result data we need to display. The meanings of the other two parameters success and ID are similar to those in jsonreader. They are used to determine whether the operation is successful and the returned ID respectively. Because the labels in XML and the names required in reader are the same, the configuration is simplified and [{Name: 'id'}, {Name: 'name'}, {Name: 'desc'}] is directly written as ['id', 'name', 'desc'].

Because xmlreader cannot automatically parse the strings in Javascript into XML data, we need to use other methods for demonstration. Refer to the XML Construction Method in localxhr. js. We have the following solution, as shown in the code list.

Code List: Construct an XML Object using a local string

VaRData ="<? XML version = '1. 0' encoding = 'utf-8'?>"+

"<Dataset> "+

"<ID> 1 </ID>"+

"<Totalrecords> 2 </totalrecords>"+

"<Success> true </success>"+

"<Record>"+

"<ID> 1 </ID>"+

"<Name> name1 </Name>"+

"<Descn> descn1 </descn>"+

"</Record>"+

"<Record>"+

"<ID> 2 </ID>"+

"<Name> name2 </Name>"+

"<Descn> descn2 </descn>"+

"</Record>"+

"</Dataset>";

 

VaRXdoc;

 

If(Typeof(Domparser) ='Undefined'){

Xdoc =NewActivexobject ("Microsoft. xmldom");

Xdoc. async ="False";

Xdoc. loadxml (data );

}Else{

VaRDomparser =NewDomparser();

Xdoc = domparser. parsefromstring (data,'Application/xml');

Domparser =Null;

}

 

VaRProxy =NewExt. Data. memoryproxy (xdoc );

 

VaRReader =NewExt. Data. xmlreader ({

Totalrecords:'Totalrecords',

Success:'Success',

Record:'Record',

ID :"ID"

},['Id', 'name', 'desc']);

 

VaRDS =NewExt. Data. Store ({

Proxy: proxy,

Reader: Reader

});

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.