Sometimes a grid with multiple headers is encountered during project creation. In extjs4, the implementation of multiple headers is very simple. This article describes how to implement the multi-header gird function.
In the previous article, we talked about how to implement the grid paging function (Address: www.mhzg.net/a/20115/201151811170246.html). On this basis, this article makes modifications to achieve the multi-header grid + paging function.
Let's take a look at it first:
The implementation code is as follows:
HTML code:
- <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <HTML xmlns = "http://www.w3.org/1999/xhtml">
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
- <Title> GroupHeaderGrid-MHZG.NET </title>
- <LINK rel = "stylesheet" type = "text/CSS" href = "../resources/CSS/ext-all.css"/>
- <SCRIPT type = "text/JavaScript" src = "..././Bootstrap. js"> </SCRIPT>
- <SCRIPT type = "text/JavaScript" src = ".../locale/ext-lang-zh_CN.js"> </SCRIPT>
- <SCRIPT type = "text/JavaScript" src = "group-header.js"> </SCRIPT>
- </Head>
- <Body>
- <Div id = "Demo"> </div>
- </Body>
- </Html>
Group-header.js:
- Ext. Require ([
- 'Ext. Grid .*',
- 'Ext. toolbar. paging ',
- 'Ext. util .*',
- 'Ext. Data .*'
- ]);
- Ext. onready (function (){
- Ext. Define ('mydata ',{
- Extend: 'ext. Data. model ',
- Fields :[
- 'Title', 'author ',
- // You must specify mapping for the first field. You can omit other fields.
- {Name: 'hits ', type: 'int '},
- 'Addtime'
- ]
- });
- // Create a data source
- VaR store = ext. Create ('ext. Data. store ',{
- // Page size
- Pagesize: 50,
- Model: 'mydata ',
- // Whether to sort data on the server
- Remotesort: True,
- Proxy :{
- // Obtain data asynchronously. The URL here can be changed to any dynamic page, as long as JSON data is returned.
- Type: 'ajax ',
- URL: 'mydata. asp ',
- Reader :{
- Root: 'items ',
- Totalproperty: 'Total'
- },
- Simplesortmode: True
- },
- Sorters :[{
- // Sort fields.
- Property: 'others ',
- // Sorting type. The default value is ASC.
- Direction: 'desc'
- }]
- });
- // Create a grid
- VaR grid = ext. Create ('ext. Grid. Panel ',{
- Store: store,
- Columnlines: True,
- Columns :[{
- Text: "basic information ",
- Columns :[
- {Text: "title", width: 120, dataindex: 'title', sortable: true },
- {Text: "author", width: 200, dataindex: 'author', sortable: false },
- {Text: "clicks", width: 100, dataindex: 'hits ', sortable: true}]
- },
- {Text: "add time", width: 100, dataindex: 'addtime', sortable: true}
- ],
- Height: 400,
- Width: 520,
- X: 20,
- Y: 40,
- Title: 'example of multiple headers with grid pagination in extjs4 ',
- Disableselection: True,
- Loadmask: True,
- Renderto: 'Demo ',
- Viewconfig :{
- ID: 'gv ',
- Trackover: false,
- Striperows: false
- },
- Bbar: Ext. Create ('ext. pagingtoolbar ',{
- Store: store,
- Displayinfo: True,
- Displaymsg: 'Show {0}-{1} items, total {2} items ',
- Emptymsg: "No data"
- })
- })
- Store. loadpage (1 );
- })
JS Code:
1. Remember to load 'ext. util .*',
2. The width of the second-level header must be specified. If the width is not specified, an error is prompted ., In the red box, you must specify the width.
Server code, which is written in ASP. For more information, see the previous article.
- <%
- Response. contenttype = "text/html"
- Response. charset = "UTF-8"
- %>
- <%
- 'Return JSON data and customize some test data ..
- 'The parameter here is the same as ext3.x, but the difference is that the sorting field and sorting method use new attributes.
- 'Because this is the test data, only start and limit parameters are received. In the actual operation process, you can add sorts and Dir to the SQL order by statement.
- Start = request ("START ")
- Limit = request ("Limit ")
- If start = "" then
- Start = 0
- End if
- If Limit = "" then
- Limit = 50
- End if
- Sorts = Replace (TRIM (request. Form ("sort ")),"'","")
- Dir = Replace (TRIM (request. Form ("dir ")),"'","")
- Dim counts: counts = 300
- 'Note that the counts here is equivalent to Rs. recordcount, that is, the total number of records.
- Dim ls: ls = CINT (start) + CINT (Limit)
- If ls> = counts then
- Ls = counts
- End if
- Echo ("{")
- Echo ("Total "":")
- Echo ("& counts &""",")
- Echo ("" items "":[")
- For I = start + 1 to LS
- Echo ("{")
- Echo ("title" ":" "newstitle" & I &"""")
- Echo (",")
- Echo ("author" ":" "author" & I &"""")
- Echo (",")
- Echo ("hits" ":" & I &"""")
- Echo (",")
- Echo ("addtime" ":" & now ()&"""")
- Echo ("}")
- If I <ls then
- Echo (",")
- End if
- Next
- Echo ("]}")
- Function echo (STR)
- Response. Write (STR)
- End Function
- %>