Jqgrid table data export instance

Source: Internet
Author: User
Tags jqgrid

First, a javascript script:
Copy codeThe Code is as follows:
/**
*
*
* @ Param table_id id of the table
* @ Param container_id id of the container
* @ Param form_id id of the submitted Form
* @ Param title file name
* @ Param rownumbers
*/
Function getXlsFromTbl (table_id, container_id, form_id, title, rownumbers ){
Try {
Var content = "";


If (table_id! = Null & table_id! = "" & Table_id! = "Null "){
<SPAN style = "WHITE-SPACE: pre"> </SPAN> content = getTblData ($ ('#' + table_id), $ ('#' + container_id ), rownumbers );
}
If (content = ""){
Alert ("table does not exist ");
Return;
}
Var fileName = getExcelFileName (title );

DoFileExport ($ ('#' + form_id), fileName, content );
}
Catch (e ){
Alert ("Export exception:" + e. name + "->" + e. description + "! ");
}
}
Function getTblData (tableobj, containerobj, rownumbers ){


Var outStr = "";
If (tableobj! = Null ){
Var rowdata = tableobj. getRowData ();
Var Lenr = 1;


For (I = 0; I <Lenr; I ++ ){
// Var Lenc = curTbl. rows (I). cells. length;
Var th;
If (rownumbers = false ){
Th = containerobj. find ('th: not (: first-child )');
}
Else {
Th = containerobj. find ('th ');
}
Th. each (function (index, element ){
Var j = index + 1;
Var content = $ (element). text ();
Content = content. replace (/(^ \ s *) | (\ s * $)/g, ""); // remove Spaces
OutStr + = content + ",";
});
OutStr + = "+ nl + ";
}
Var tmp = "";
For (I = 0; I <rowdata. length; I ++ ){
Var row = eval (rowdata [I]);
For (each in row ){
<SPAN style = "WHITE-SPACE: pre"> </SPAN> var temp = $ (row [each]). text ();
<SPAN style = "WHITE-SPACE: pre"> </SPAN> if ($ (row [each]). text () = null | $ (row [each]). text () = ""){
<SPAN style = "WHITE-SPACE: pre"> </SPAN> if (row [each]. charAt (0 )! = '<')
<SPAN style = "WHITE-SPACE: pre"> </SPAN> outStr + = row [each] + ",";
<SPAN style = "WHITE-SPACE: pre"> </SPAN>
<SPAN style = "WHITE-SPACE: pre"> </SPAN>}
<SPAN style = "WHITE-SPACE: pre"> </SPAN> else
<SPAN style = "WHITE-SPACE: pre"> </SPAN> outStr + = $ (row [each]). text () + ",";
}
OutStr + = "+ nl + ";
}
}
Else {
OutStr = null;
Alert (inTbl + "null! ");
}
Return outStr;
}
Function getExcelFileName (title ){
Var d = new Date ();
Var curYear = d. getYear ();
Var curMonth = "" + (d. getMonth () + 1 );
Var curDate = "" + d. getDate ();
Var curHour = "" + d. getHours ();
Var curMinute = "" + d. getMinutes ();
Var curSecond = "" + d. getSeconds ();
If (curMonth. length = 1 ){
CurMonth = "0" + curMonth;
}
If (curDate. length = 1 ){
CurDate = "0" + curDate;
}
If (curHour. length = 1 ){
CurHour = "0" + curHour;
}
If (curMinute. length = 1 ){
CurMinute = "0" + curMinute;
}
If (curSecond. length = 1 ){
CurSecond = "0" + curSecond;
}
Var fileName = title + "_" + curYear + curMonth + curDate + "_"
+ CurHour + curMinute + curSecond + ". csv ";


Return fileName;
}
Function doFileExport (formobj, filename, content ){
<SPAN style = "WHITE-SPACE: pre "> </SPAN> formobj.html (" <input id = 'filename' name = 'filename' type = 'text' style = 'display: none '> <input id = 'content' name = 'content' type = 'text' style = 'display: none'> ");
$ ("# Filename"). val (filename );
$ ("# Content"). val (content );
Formobj. submit ();
}

The page calls javascript:
Copy codeThe Code is as follows:
<Form id = "download_form" method = "post" target = "_ blank" action = "downLoadTableDataAction">

Copy codeThe Code is as follows:
<Div id = "table_container">
<SPAN style = "WHITE-SPACE: pre"> </SPAN> <table id = "keyword_detail"> </table>
<SPAN style = "WHITE-SPACE: pre"> </SPAN> <div id = "footer"> </div>
</Div>

Copy codeThe Code is as follows:
// Download
$ ('# Download_file'). click (function (){
GetXlsFromTbl ('keyword _ detail', 'table _ container', 'Download _ Form', 'keyword details data', true)
});

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.