Extjs dynamic Table Generation

Source: Internet
Author: User

When data is displayed on the web, the number of columns and the number of rows in the grid are uncertain. How can we dynamically create tables based on data?
The JSON data of extjs brings us a good and simple method.
To create a grid, you need to determine the number of columns, and then determine the number of rows based on the number of data.
We can see that someone has used a method to put column attributes and data together in JSON data to achieve the effect, but it is not difficult to find that, in this case, it is difficult to paging or update the data in the table.
In fact, we can use the fixed number of columns on the extjs official website to dynamically generate tables.
First, use ajax to decompress the column information from the server and encapsulate it into JSON. Table data is obtained through another Ajax request. Because the column has been obtained, this can be encapsulated into a store. in this way, the storage, and CM required to generate the grid
The demo source code is as follows:CodeDescription: I have written the data to Javascript ):

Ext. onready (function (){

// Note: this is an example showing simple state management. during development,
// It is generally best to disable state management as dynamically-generated IDS
// Can change your SS page loads, leading to unpredictable results. The developer
// Shocould ensure that stable state IDs are set for stateful components in Real Apps.
Ext. state. Manager. setprovider (New Ext. state. cookieprovider ());
VaR mydata = [1, 1, 1],
[2, 2, 2]
];

// Example of custom Renderer Function
Function Change (VAL ){
If (Val> 0 ){
Return '<span style = "color: green;">' + Val + '</span> ';
} Else if (Val <0 ){
Return '<span style = "color: red;">' + Val + '</span> ';
}
Return val;
}
// Example of custom Renderer Function
Function pctchange (VAL ){
If (Val> 0 ){
Return '<span style = "color: green;">' + Val + '% </span> ';
} Else if (Val <0 ){
Return '<span style = "color: red;">' + Val + '% </span> ';
}
Return val;
}
// Create the Data Store

VaR fielddatas = "{'colummodle': [" +
"{'Header': 'seq ', 'dataindex': 'number', 'width': 40}," +
"{'Header': 'code', 'dataindex': 'text1'}," +
"{'Header': 'name', 'dataindex': 'info1'}," +
"{'Header': 'price', 'dataindex': 'special1'}" +
"], 'Fieldnames': [{Name: 'number'}," +
"{Name: 'text1'}, {Name: 'info1'}," +
"{Name: 'special1'}]}";

VaR JSON = new Ext. util. JSON. Decode (fielddatas );
VaR CM = new Ext. Grid. columnmodel (JSON. colummodle );
VaR store = new Ext. Data. simplestore ({
Fields: JSON. fieldsnames
});

Store. loaddata (mydata );
// Var DS = new Ext. Data. jsonstore ({
// Data: store. tosource (),
// Fields: JSON. fieldsnames
//});

// Create the grid
VaR grid = new Ext. Grid. gridpanel ({
Height: 200,
Width: 400,
Region: 'center ',
Split: True,
Border: false,
Store: store,
CM: cm
});

Grid. Render ('grid-example ');
});

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.