Easyuidatagrid dynamically generates Columns

Source: Internet
Author: User
Easyuidatagrid defines columns by using [javascript] $ (& amp; #39; # grid & amp; #39 ;). datagrid ({columns: [[{field: & amp; #39; f1 & amp; #39;, title: & amp; #39; field 1 & amp; #39;, width: 160}, {field: & am

The easyui datagrid defines columns by using the following methods:

[Javascript]
$ ('# Grid'). datagrid ({columns :[[
{Field: 'f1', title: 'field 1', width: 160 },
{Field: 'F2', title: 'field 2', width: 160}
]});

However, in actual projects, we often need the datagrid to dynamically generate columns.

I searched an article on the Internet: I will talk about data loading in easyui datagrid again.

I really admire this author. He can find the function he wants to extend in the obfuscated source code of easyui, which is full of variable combinations of numbers and letters... Strong!

But I do not like this method. After all, modifying the source code has many side effects, and I still like the original ecology.

Dynamic generation of columns, which is a common requirement, is unlikely to be supported by easyui.

It is not mentioned in the official document. Maybe the author wants to earn consulting fees by using these advanced functions...


After talking a lot of nonsense, I have posted a lot of twists and turns:


[Javascript]
Var options = {};
$ (Function (){
Var myNj = 9;
// Initialization
$ ("# Disgrid"). datagrid ({
Type: 'post ',
Nowrap: false,
Striped: true,
Fit: true,
Width: 1024,
Height: 500,
Url :'',
PageSize: 30,
RemoteSort: false,
Pagination: true,
Rownumbers: true,
SingleSelect: true,
QueryParams :{
Nj: myNj,
UnitType: 1
}
});

FetchData (myNj );
});
 
Function fetchData (nj ){
Var s = "";
S = "[[";
S = s + "{field: 'unitname', title: 'unit ', width: 160}, {field: 'practicetime', title: 'test time', width: 160}, {field: 'userid', title: 'userid', width: 120, hidden: 'true'}, {field: 'serial', title: 'serial ', width: 120, hidden: 'true'}, {field: 'unitid', title: 'unitid', width: 100, hidden: 'true '},";

// The definition of the lu todo column can be obtained from the server.
If (nj = 9 ){
S = s + "{field: 'aipanel _ text_exam ', title: 'short-text reading', width: 80}, {field: 'aipanel _ scene_exam', title: 'situational dialogs ', width: 80}, {field: 'aipanel _ oral_exam', title: 'short talking', width: 80 }";
S = s + ", {field: 'tatal _ score ', title: 'Total', width: 60, formatter: function (value, rec) {return paraseIntValue (rec. aipanel_text_exam) + paraseIntValue (rec. aipanel_scene_exam) + paraseIntValue (rec. aipanel_oral_exam );}}";
} Else if (nj = 7 | nj = 8 ){
//....
}
S = s + "]";
Options = {};
Options. url = '/app/search. do ';
Options. queryParams = {
Nj: nj,
UnitType: 1
};
Options. columns = eval (s );
// Add a column to the lu
Options. columns [0]. push (
{
Field: 'desc', title: 'view details', width: 60,
Formatter: function (value, rec ){
Return "details ";
}
}
);

$ ('# Disgrid'). datagrid (options );
$ ('# Disgrid'). datagrid ('reload ');

}

It should be noted that the JSON format defined by the column is very important and cannot be wrong! If the format is incorrect, it cannot work normally.

Related Article

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.