[Easyui] [DataGrid] easyui DataGrid dynamically merges cells based on fields

Source: Internet
Author: User

1. Merge Method

/**
* Easyui DataGrid dynamically merges cells based on fields
* Tableid: ID of the table to be merged
* Columns whose collist parameter is to be merged are separated by commas (,) (for example, "name, department, office ");
*/
Function mergecellsbyfield (tableid, collist ){
VaR colarray = collist. Split (",");
VaR tTable = $ ("#" + tableid );
VaR tablerowcnt = tTable. DataGrid ("getrows"). length;
VaR tmpa;
VaR tmpb;
VaR pertxt = "";
VaR curtxt = "";
VaR alertstr = "";
For (j = colarray. Length-1; j> = 0; j --){
Pertxt = "";
Tmpa = 1;
Tmpb = 0;

For (I = 0; I <= tablerowcnt; I ++ ){
If (I = tablerowcnt ){
Curtxt = "";
}
Else {
Curtxt = tTable. DataGrid ("getrows") [I] [colarray [J];
}
If (pertxt = curtxt ){
Tmpa + = 1;
}
Else {
Tmpb + = tmpa;

TTable. DataGrid ("mergecells ",{
Index: I-tmpa,
Field: colarray [J], // merge Fields
Rowspan: tmpa,
Colspan: NULL
});
TTable. DataGrid ("mergecells", {// merge Based on colarray [J]
Index: I-tmpa,
Field: "ideparture ",
Rowspan: tmpa,
Colspan: NULL
});

Tmpa = 1;
}
Pertxt = curtxt;
}
}
}

2. Call the merge Method

// Present the list data
$ ('# List'). DataGrid ({pagination: True,
Onloadsuccess: function (data ){
If (data. Rows. length> 0 ){
// Call mergecellsbyfield () to merge Cells
Mergecellsbyfield ("tblist", "no ");
}
}
});

Note:

Mergecellsbyfield (tableid, collist );

ID of the table to be merged
Columns to be merged with the collist parameter, separated by commas (,) (for example, "name, department, office ");

This method is generally not used because it is not beautiful.

Suppose:

1 1 1
1 5 1
1 5 1
1 5 2
1 1 2

As follows:

1 1 1
5 1
1
2
1 2

 

Due to the limitations of the table, we cannot merge the third column. If we merge three "1" and two "2", we will understand why it is not beautiful.

Haha, based on the red section in my method, we use one primary column, and the others are merged based on the number of its rows. Success

 

Thank you very much.

Http://www.gogogogo.me/development/EasyUI-DataGrid-mergeCells.html

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.