Jquery DataTable控制顯示列,導出EXCEL

1.初始化javascript

var table = $('#table').DataTable({ "data": data[0].DATA, "columns": data[0].COLUMNS })

後臺返回前臺data的JSON格式java

[{ "COLUMNS": [{ "title": "1" }, { "title": "2" }], "DATA": [ ["1", "8758"], ["2", "8758"] ] }]

將DataTable加表頭轉換爲JSON(flag是一個狀態字段)(header是字符串數組:"標題1,標題2,標題3".Split(','))node

private void DataTableToJSON(DataTable dt, string[] header, int flag, ref StringBuilder sb) { int total = dt.Rows.Count; string tempcol = null; string kong = null; for (int i = 0; i < header.Length; i++) { tempcol = tempcol + "{ \"title\": \"" + header[i] + "\"},"; kong = kong + ","; } tempcol = "\"COLUMNS\":[" + tempcol.TrimEnd(',') + "],"; kong = kong.Substring(1, kong.Length - 1); sb.Append("[{"); sb.Append("\"FLAG\":\"" + flag + "\","); sb.Append(tempcol); if (total == 0) { sb.Append("\"DATA\":[]}]"); } else { sb.Append("\"DATA\":["); //轉化爲Json格式
                foreach (DataRow row in dt.Rows) { sb.Append("["); foreach (DataColumn column in dt.Columns) { sb.Append("\"" + row[column.ColumnName].ToString().Replace("\r\n", "<BR/>").Replace("\\", "\\\\").Replace("\t", "\\t").Replace("\\r", "\\r").Replace("\n", "<BR/>").Replace("\"", "&quot;") + "\","); } sb.Remove(sb.Length - 1, 1); sb.Append("],"); } sb.Remove(sb.Length - 1, 1); sb.Append("]}]"); } }

 

2.控制DataTable列數組

targets爲列索引,能夠爲[0,1,2]數組方式,[-1,-2]是列索引倒序函數

"columnDefs": [
                            {
                                targets: -1,
                                render: function (data, type, row, meta) {
                                    return "<a href=\"taskIndicatorScore.aspx?id=" + data + "&type=LEADER\">領導審覈</a>";
                                },
                                visible: l_v
                            }
]

3.導出EXCELui

buttons: [
{
                             extend: 'excel',
                             text: '<i class="fa fa-file-excel-o bigger-110 green"></i>',
                             title: 'EXCEL模板',
                             className: "btn btn-white btn-primary btn-bold",
                             titleAttr: '導出EXCEL模板',

                             exportOptions: { //從DataTable中選擇要收集的數據。這包括列、行、排序和搜索的選項。
                                 "columns": [0, 1, 3, 4, 5, 6, 7, 8],//設置須要導出的列索引
                                 'format': { //用於導出將使用的單元格格式化函數的容器對象 format有三個子標籤,header,body和foot
                                     'header': function (data, columnIdx) {
                                         return data;
                                     },
                                     "body": function (data, columnIndex, rowIndex, node) { //body區域的function,能夠操做須要導出excel的數據格式
                                         if (columnIndex > 2 && (data == "" || data == null)) {
                                             return "3";
                                         } else {
                                             return data;
                                         }
                                     }
                                 }
                             }
                         }
]
相關文章
相關標籤/搜索