ASP. Jqgrid the same page to query different tables, Jqgrid display headers and data for different tables and pagination

Source: Internet
Author: User
Tags jqgrid

Based on my previous post <a href= "http://www.cnblogs.com/alasai/p/4765756.html" >asp.net MVC Excel import </a> Different departments upload different types of Excel files, what to do when querying on the same page.

Solution: According to the table name and time parameter passed to all the data loaded into the table, where the table header for us to decide, the order of the main body we also want to and the table header, load to the foreground and then the table paging control to page, I have about 100 more Excel file types, their column names do not , it is not practical to think of such practices.

There are no other solutions, see a lot of jqgrid examples, their headers (colnames) and content (Colmodel) are the first to die. Here I think of a solution is to make colnames and Colmodel are made alive, so that can not be perfect to solve the above problem,

The idea is always good, but it's not smooth sailing, but only if you have to think about it.

Ideas and ideas:

1. Jqgrid colnames and Colmodel are made alive, but the colnames of each table are different, and their order must be consistent, how to do, in <a href= "http://www.cnblogs.com/ alasai/p/4765756.html ">asp.net MVC Excel import </a> after I upload a file in this article I will save their colnames and Colmodel in a txt file named after their table name. The code snippet is as follows

So it's not a problem to show the colnames and Colmodel of each table.

The background C # code is as follows:

[HttpPost] PublicActionResult Gettestdata (stringDepartmentstringTableNamestringStartTime,stringEndTime) {Stopwatch Watch=Commonhelper.timerstart (); stringSQL5 ="SELECT * from"+ Department +"_"+ tablename +"where 1=1 and enabled= ' 1 '"; if(!string. IsNullOrEmpty (StartTime)) {SQL5+="and CONVERT (varchar (), addtime,120) >= '"+ StartTime +"'"; }            if(!string. IsNullOrEmpty (EndTime)) {SQL5+="and CONVERT (varchar (), addtime,120) <= '"+ EndTime +"'"; } DataTable ListData=datafactory.database ().            Findtablebysql (SQL5);  This. Dircsv = Server.MapPath ("~/content/uploads/"); StreamReader SR=NewStreamReader ( This. Dircsv +"\\"+ Department +"_"+ tablename +". txt");            String Line; List<string> list =Newlist<string>();  while(line = Sr. ReadLine ())! =NULL) {list. ADD (line.            ToString ()); }            stringColnames =""; string[] Chinesname = list[0]. ToString (). Trim (','). Split (','); string[] Englishname = list[1]. ToString (). Trim (','). Split (',');  for(inti =0; I < Chinesname. Length; i++) {colnames+="'"+ Chinesname[i]. ToString () +"',"; } List<Department> List1 =NewList<department>();  for(intj =0; J < Englishname. Length; J + +) {List1. ADD (NewDepartment {index = englishname[j]. ToString (). ToLower (), lable = Chinesname[j]. ToString (), name = Englishname[j]. ToString (). ToLower (), sortable ="false" }); }            varresult =New{Json=New{colnames=Chinesname, Colmodels= ( fromDeptinchList1Select New{Index=Dept.index, lable=dept.lable, name=Dept.name, sortable=false}), Data=New{Options=New{page="1", Total="1", Records="1", Costtime=Commonhelper.timerend (watch), Rows=ListData}}            }            }; returnContent (result.        ToJson ()); }

So the foreground changes how to parse the JSON generated above.

The jquery code is as follows

$.ajax ({url: "@Url. Content ("/dataswitch/gettestdata ")? department=" + $ ("#seldepartment"). Val () + "&tablename= "+ $ (" #ExcelFileId "). Val () +"&sjs= "+ new Date (). GetTime () +"&starttime= "+ $ (" #StartTime "). Val () +"&endtime= "+ $ (" #EndTime "). Val (), type: ' POST ', Cache:false, data: {}, Success:funct                Ion (Result) {result = eval (' (' +result+ ') '); var colmodels = result.                Json.colmodels; var colnames = result.                Json.colnames; var data = result.                Json.data.options; $ ("#gridTable"). Jqgrid ({datatype: ' jsonstring ', Datastr:data, C Olnames:colnames, Colmodel:colmodels, Jsonreader: {root: '                    Rows ', Repeatitems:false}, Gridview:true,                    Pager: $ (' #gridPager '), Height: $ (window). Height ()-111, Autowidth:true,                    Rownum:15, Rowlist: [A], viewrecords:true,          Rownumbers:true,          Shrinktofit:false})}, Error:function (Result) {}}); End Ajax

Now querying different tables can show different table contents in Jqgrid, but there is another problem (this problem you are very difficult to find a solution on Baidu)

The problem is only to display the first selection of the table content, and the page has no effect, the problem bothered me for three hours, and finally in the Jqgrid group asked, some people say is loaded, the loading of the data HTML is not. Then I tried to load the different tables before I re-construct the HTML.

  $grid = $ ("<id= ' gridtable '></table><  id = ' Gridpager ' ></div > ");      $ (' #grid_List '). Empty (). HTML ($grid);

This short, magical piece of code solves the problem.

The complete jquery code is as follows

//Load Table function Getgrid () {var Eid = $ ("#ExcelFileId"). Val ();            if (Eid = = "") {Tipdialog ("Please select file type First", 3,0);        return false; } $grid = $ ("<TableID= ' gridtable '></Table><DivID= ' Gridpager '></Div>");             $ (' #grid_List '). Empty (). HTML ($grid); $.ajax ({url: "@Url. Content ("/dataswitch/gettestdata ")? department=" + $ ("#seldepartment"). Val () + "&tablename= "+ $ (" #ExcelFileId "). Val () +"&sjs= "+ new Date (). GetTime () +"&starttime= "+ $ (" #StartTime "). Val () +"&endtime= "+ $ (" #EndTime "). Val (), type: ' POST ', Cache:false, data: {}, Success:funct                Ion (Result) {result = eval (' (' +result+ ') '); var colmodels = result.                Json.colmodels; var colnames = result.                Json.colnames; var data = result.                Json.data.options; $ ("#gridTable"). Jqgrid ({datatype: ' jsonstring ', Datastr:data, C Olnames:colnames, Colmodel:colmodels, Jsonreader: {root: '                    Rows ', Repeatitems:false}, Gridview:true,                    Pager: $ (' #gridPager '), Height: $ (window). Height ()-111, Autowidth:true,                    Rownum:15, Rowlist: [A], viewrecords:true,          Rownumbers:true,          Shrinktofit:false})}, Error:function (Result) {}}); End Ajax}

At this point the problem is solved perfectly.

ASP. Jqgrid the same page to query different tables, Jqgrid display headers and data for different tables and pagination

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.