jQuery插件jqGrid動態(tài)獲取列和列字段的方法
本文實例講述了jQuery插件jqGrid動態(tài)獲取列和列字段的方法。分享給大家供大家參考,具體如下:
1、問題背景
jqGrid表格插件,利用自身方法獲取表格的表頭和表格字段;獲取列名和列字段名顯示在彈窗里,用復選框進行勾選
2、實現(xiàn)源碼
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>jqGrid動態(tài)獲取列和列字段</title> <link rel="stylesheet" href="css/ui.jqgrid.css" rel="external nofollow" /> <link rel="stylesheet" href="css/ui.jqgrid-bootstrap-ui.css" rel="external nofollow" /> <link rel="stylesheet" href="css/bootstrap.css" rel="external nofollow" /> <link rel="stylesheet" href="css/bootstrap-theme.css" rel="external nofollow" /> <link rel="stylesheet" href="css/jquery-ui.css" rel="external nofollow" /> <link rel="stylesheet" href="css/jquery-ui.theme.css" rel="external nofollow" /> <script type="text/javascript" src="js/jquery-1.11.0.min.js" ></script> <script type="text/javascript" src="js/jquery-ui.js"></script> <script type="text/javascript" src="js/jquery.jqGrid.min.js" ></script> <script type="text/javascript" src="plugins/grid.setcolumns.js"></script> <style> th{ border: 1px solid #ABABAB; line-height: 20px; vertical-align: middle; } td{ line-height: 20px; } </style> <script> $(document).ready(function(){ $("#jqTable").jqGrid({ url:"data/student.json", height:380, datatype:"json", colNames:["序號","姓名","年齡","性別","QQ號","電話","地址"], colModel:[{ name : 'id', index : 'id', label : '序號', width : 60, align:'center' },{ name : 'name', index : 'name', label : '姓名', width : 120, align:'center' },{ name : 'age', index : 'age', label : '年齡', width : 120, align:'center' },{ name : 'sex', index : 'sex', label : '性別', width : 120, edittype : "select", formatter : 'select', editoptions : { value :'0:男;1:女;' }, align:'center' },{ name : 'qq', index : 'qq', label : 'QQ號', width : 120, align:'center' },{ name : 'phone', index : 'phone', label : '電話', width : 120, align:'center' },{ name : 'address', index : 'address', label : '地址', width : 200, align:'center' }], sortname : "id", sortorder : "desc", viewrecords : true, rownumbers:true, autowidth:true, jsonReader : { repeatitems : false } }); var dialog = $("#dialog-column").dialog({ autoOpen :false, modal : true, resizable : true, height: "auto", width: 400, align:'center', buttons: { "確定": function() { $(this).dialog( "close" ); }, "關(guān)閉": function() { $(this).dialog( "close" ); } } }); $("#column").button().on("click", function() { dialog.dialog("open"); //獲取列名 var colNames=$("#jqTable").jqGrid('getGridParam','colNames'); //獲取列字段 var colModel=$("#jqTable").jqGrid('getGridParam','colModel'); var table = ""; var newColumnName = []; var newColumnValue = []; for (var i=0;i<colNames.length;i++) { var columnHidden = colModel[i].hidden; var columnName = colModel[i].name; if(columnHidden==false && columnName != "rn") { newColumnName.push(colNames[i]); newColumnValue.push(columnName); } console.info(columnName); } for(var j=0;j<newColumnName.length;j++) { if(j%5==0) { table += "<tr>"; } table += "<td><input type='checkbox' id='"+newColumnValue[j]+"' name='column' checked='checked'><label for='"+newColumnValue[j]+"'>"+newColumnName[j]+"</label></td>"; if((j+1)%5==0) { table += "</tr>"; } } $("#tableColumn").empty().append(table); }); }); </script> </head> <body> <div> <table id="jqTable" class="table"></table> </div> <div> <button id="column" type="button">顯示</button> </div> <div id="dialog-column" title="設置列"> <table id="tableColumn" style="width: 100%; height: 100px;"> </table> </div> </body> </html>
3、實現(xiàn)結(jié)果
(1)初始化
(2)單擊按鈕
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery常用插件及用法總結(jié)》、《jquery中Ajax用法總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery擴展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設計有所幫助。
- 基于jQuery和Bootstrap框架實現(xiàn)仿知乎前端動態(tài)列表效果
- jQuery動態(tài)顯示和隱藏datagrid中的某一列的方法
- jQuery實現(xiàn)表格行和列的動態(tài)添加與刪除方法【測試可用】
- jQuery EasyUI中DataGird動態(tài)生成列的方法
- 基于JQuery的動態(tài)刪除Table表格的行和列的代碼
- jQuery實現(xiàn)列表內(nèi)容的動態(tài)載入特效
- jQuery實現(xiàn)動態(tài)顯示select下拉列表數(shù)據(jù)的方法
- jQuery實現(xiàn)動態(tài)加載select下拉列表項功能示例
- jQuery動態(tài)產(chǎn)生select option下拉列表
- jQuery簡單實現(xiàn)向列表動態(tài)添加新元素的方法示例
- jQuery實現(xiàn)動態(tài)生成年月日級聯(lián)下拉列表示例
- Jquery動態(tài)列功能完整實例
相關(guān)文章
jQuery中通過ajax調(diào)用webservice傳遞數(shù)組參數(shù)的問題實例詳解
本文通過實例給大家詳細介紹jQuery中通過ajax調(diào)用webservice傳遞數(shù)組參數(shù)的相關(guān)資料,需要的朋友可以參考下2016-05-05javascript 歷史記錄 經(jīng)常用于產(chǎn)品最近歷史瀏覽
在很多購物網(wǎng)站,都有“產(chǎn)品的瀏覽歷史記錄”,這是個很貼心的功能。2009-05-05詳解有關(guān)easyUI的拖動操作中droppable,draggable用法例子
本篇文章主要介紹了有關(guān)easyUI的拖動操作中droppable,draggable用法例子,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06jquery+ajaxform+springboot控件實現(xiàn)數(shù)據(jù)更新功能
這篇文章主要介紹了jquery+ajaxform+springboot控件實現(xiàn)數(shù)據(jù)更新操作,使用jquery的ajaxform插件是一個比較不錯的選擇。具體實現(xiàn)工程大家參考下本文2018-01-01