Before our project in the foreground display only need to read the data from the database to display it, the table header field of the DataGrid is written dead, the data to throw, the basic nothing, the result of the customer's request a few days ago, one of the fields can not be dead, should be how many items show how many items, for example, Originally only need to display: Other items total, now need to show is: xx plus points, xx plus points, xx minus points, xx plus points .... field is not fixed, how many items are not sure, you need to find the corresponding fields from the database to display.
Can not ask customers to adapt to our system ah, and should wholeheartedly for the sake of customers, so, began to change. The original case is that all the fields are fixed, and the field to be displayed corresponds to a table in the database, just need to find out. Now the case is that some of the fields are fixed, the other field is not fixed, and then the data of the fixed field is isolated from the original table, and the data of the non-fixed field is found from another table.
After an afternoon and a night of efforts, finally initially completed the display effect, but there are shortcomings is no way to achieve pagination display, now the effect needs further optimization, the current completed part of the record.
The code is as follows:
View End:
DataGrid dynamic Load Column function Getjson () {$.getjson ('/queryscores/queryotherassess ', NULL, FUNCTI On (otherscores) {var columns = new Array (); var column2 = {field: ' Faculty number ', title: ' Faculty number ', width:50} columns.push (Colu MN2); var column3 = {field: ' Faculty name ', title: ' Faculty Name '} columns.push (COLUMN3); var column4 = {field: ' Productivity ', title: ' Productivity ', width:50} columns.push (COLUMN4); var column5 = {field: ' Professional Ethics ', title: ' Professional Ethics ', width:50} columns.push (COLUMN5); var column6 = {field: ' Business Capability ', Title: ' Business Capability ', width:50} columns.push (COLUMN6); var column7 = {field: ' Probity and self-discipline ', title: ' Probity and self-discipline ', width:50} columns.push (COLUMN7); var column8 = {field: ' Work result ', Title: ' Work Achievement ', width:50} columns.push (COLUMN8); //Loop for import other scores for (var i = 0; i < otherscores.sum; i++) {var column1={ Field:otherscores. Otherscoresassess[i],title:otherscores. OTHERSCORESASSESS[I],WIDTH:70} columns.push (Column1); } var column9 = {field: ' Total score ', Title: ' Total Score ', width:50} columns.push (COLUMN9); inittable (columns); })} function inittable (columns) {$ (' #tt '). DataGrid ({title: ' View Score ', URL: '/queryscores/queryscoresindex ', Width: ' 100% ', Rownumbers:true, col Umns: [Columns]}); }
The view end is tuned to the controller side two methods, queryotherassess to read to the header field of the column that needs to be loaded dynamically. Queryscoresindex is the method of checking the data to return.
Queryotherassess:
Public ActionResult queryotherassess () { ilist<string> otherscoresassess = Otherscoresasscssprogrambll.loadenities (U = u.isused = = True). Select (U = u.asscssprogram). ToArray (); var otherscoresassesslist = new { sum = Otherscoresassess.count (), otherscoresassess = otherscoresassess }; Return Json (Otherscoresassesslist, jsonrequestbehavior.allowget); }
Queryscoresindex first detects the data and then turns it into a JSON string to return to the foreground.
The specific data will not be posted, look at the assignment of the paragraph:
Table Head DataTable finaltable = new DataTable (); FINALTABLE.COLUMNS.ADD ("Faculty number", typeof (int)); FINALTABLE.COLUMNS.ADD ("Faculty name", typeof (String)); FINALTABLE.COLUMNS.ADD ("Productivity", typeof (String)); FINALTABLE.COLUMNS.ADD ("Professional Ethics", typeof (String)); FINALTABLE.COLUMNS.ADD ("Business Capability", typeof (String)); FINALTABLE.COLUMNS.ADD ("integrity and self-discipline", typeof (String)); FINALTABLE.COLUMNS.ADD ("Work Performance", typeof (String)); for (int i = 0; i < Yzotherprogramentity.count; i++) { FinalTable.Columns.Add (Yzotherprogramentity[i]. Asscssprogram, typeof (String)); } FINALTABLE.COLUMNS.ADD ("Total Score", typeof (String));
Then the assignment:
DataRow Filerow = Finaltable.newrow (); filerow["Faculty Number" = Scoresstaffid; filerow["faculty name"] = Staffname; filerow["productivity"] = Staffscoresentity[i]. workefficiency; filerow["Professional ethics"] = Staffscoresentity[i]. Professionalethics; filerow["business capability"] = Staffscoresentity[i]. businessability; filerow["integrity and self-discipline"] = Staffscoresentity[i]. Honestydiscipline; filerow["work result"] = Staffscoresentity[i]. Workperformance; if (yzotherprogramentity! = null) {for (int j = 0; J < yzotherprogramentity. Count; J + +) {Decimal othertotalscores = 0; Guid Otherprogramentityid = yzotherprogramentity[j].id; ilist<yzotherscoresentity> yzotherscoresentity = otherscoresbll.loadenities (U = U.CriticID = Staffguid &Amp;& U.program = = Otherprogramentityid && u.isused = = True). ToArray (); for (int k = 0; k < Yzotherscoresentity.count; k++) {Otherto Talscores = Othertotalscores + yzotherscoresentity[k]. number; } Filerow[yzotherprogramentity[j]. Asscssprogram] = othertotalscores; } filerow["Total score"] = Staffscoresentity[i]. Totalscores; FINALTABLE.ROWS.ADD (Filerow);
Last goto JSON string:
char[] Specialchars = new char[] {', '}; String jsonstring = "["; int index = 0; foreach (DataRow dr in Finaltable.rows) { jsonstring + = "{"; foreach (DataColumn dc in finaltable.columns) { jsonstring + = "\" + DC. ColumnName + "\": \ "" + DR[DC]. ToString () + "\", "; } jsonstring = Jsonstring.trimend (specialchars); Jsonstring + = "},"; index++; } jsonstring = Jsonstring.trimend (specialchars); Jsonstring + = "]"; return jsonstring;
And then the reception to receive, and finally can do dynamic loading columns
Results:
"DataGrid" Dynamically Loading columns