標籤:style blog http ar io os sp for java
項目總結—jQuery EasyUI-DataGrid動態載入表頭
目錄(?)[-]
- 概要
- 實現
- 總結
概要
在前面兩篇文章中,我們已經介紹了在jQuery EasyUI-DataGrid中有參數和無參數的情況下將資料庫中資料顯示到前台,但是對於前面兩篇文章顯示的資料表頭是固定的,如果我們顯示到前台的資料來自資料庫不同的表,那麼表頭也需要動態載入,這篇文章我們就來看下在EasyUI-DataGrid中動態載入表頭和資料.
實現
我們要實現的功能是根據我們的需要,讓DataGrid顯示不同的資料,表頭也同樣在變,我們可以先看下我們要實現功能的頁面.
我們點擊左側不同的節點,右側將會顯示不同DataGrid的資料.
現在我們來看下前台html頁的代碼,因為表頭是變的,所以我們的前台代碼跟前面兩篇部落格就有點不一樣了.
[html] view plaincopyprint?
- <divdata-optionsdivdata-options="region:‘center‘,title:‘考核對象資料錄入情況‘,iconCls:‘icon-ok‘">
- <divclassdivclass="demo-info"data-options="fit:true,border:false,plain:true">
-
- <table id="dg"title="考核對象資料計算得分" class="easyui-datagrid"style="width: 900px; height: 400px; padding-left: 200px;"data-options="rownumbers:true,pageSize:5,pageList:[5,10,15,20],method:‘get‘,"
- pagination="true" rownumbers="true"fitcolumns="false" striped="true"singleselect="true">
- <thead>
- </thead>
-
- </table>
- </div>
-
- </div>
<divdata-options="region:‘center‘,title:‘考核對象資料錄入情況‘,iconCls:‘icon-ok‘"> <divclass="demo-info"data-options="fit:true,border:false,plain:true"> <table id="dg"title="考核對象資料計算得分" class="easyui-datagrid"style="width: 900px; height: 400px; padding-left: 200px;"data-options="rownumbers:true,pageSize:5,pageList:[5,10,15,20],method:‘get‘," pagination="true" rownumbers="true"fitcolumns="false" striped="true"singleselect="true"> <thead> </thead> </table> </div> </div>
接下來我們就看下js代碼產生表頭的代碼,的縣市區定量的表頭.
[javascript] view plaincopyprint?
- //縣市區定量指標計算情況查詢
- function reloadgridCityQuanty() {
- var test =document.getElementById("NodeChild").innerHTML;
-
- //於一般處理常式進行互動,同時產生縣市區定量表頭
- $(‘#dg‘).datagrid({
- url:"CalculateScoreFrameCity.ashx?test=" + test,
- columns: [[
- { field: ‘TargerName‘,title: ‘指標名稱‘, width: 300 },
- { field: ‘ConditionInput‘,title: ‘資料錄入情況‘, width: 300 },
- { field: ‘Operate‘, title:‘操作‘, width: 300 }
- ]]
-
- })
- }
//縣市區定量指標計算情況查詢 function reloadgridCityQuanty() { var test =document.getElementById("NodeChild").innerHTML; //於一般處理常式進行互動,同時產生縣市區定量表頭 $(‘#dg‘).datagrid({ url:"CalculateScoreFrameCity.ashx?test=" + test, columns: [[ { field: ‘TargerName‘,title: ‘指標名稱‘, width: 300 }, { field: ‘ConditionInput‘,title: ‘資料錄入情況‘, width: 300 }, { field: ‘Operate‘, title:‘操作‘, width: 300 } ]] }) }
在這裡我們需要說下的是,我們需要將我們所有要顯示的資料的表頭都在這裡提前建立好,然後利用js語句進行動態載入.
我們再來看個表頭的js代碼,和上面的本質上是一樣的.
[javascript] view plaincopyprint?
- //縣市區定性計算情況查詢
- functionreloadgridCharacterizationCity() {
- var test =document.getElementById("NodeChild").innerHTML;
-
- //於一般處理常式進行互動,同時產生縣市區定性表頭
- $(‘#dg‘).datagrid({
- url:"CalculateScoreFrameCity.ashx?test=" + test,
- columns: [[
- { field: ‘Name‘, title:‘指標名稱‘, width: 225 },
- { field: ‘ConditionInput‘,title: ‘資料錄入情況‘, width: 225 },
- { field: ‘CalculateInput‘,title: ‘計算情況‘, width: 225 },
- { field: ‘Operate‘, title:‘操作‘, width: 225 }
- ]]
-
- })
-
- }
//縣市區定性計算情況查詢 functionreloadgridCharacterizationCity() { var test =document.getElementById("NodeChild").innerHTML; //於一般處理常式進行互動,同時產生縣市區定性表頭 $(‘#dg‘).datagrid({ url:"CalculateScoreFrameCity.ashx?test=" + test, columns: [[ { field: ‘Name‘, title:‘指標名稱‘, width: 225 }, { field: ‘ConditionInput‘,title: ‘資料錄入情況‘, width: 225 }, { field: ‘CalculateInput‘,title: ‘計算情況‘, width: 225 }, { field: ‘Operate‘, title:‘操作‘, width: 225 } ]] }) }
關於一般處理常式的書寫與前面兩篇文章是一樣的,大家可以參考前面一篇的文章.
我們也來看下我們建立的另一個表頭的效果
總結
通過本篇文章我們就可以在前台根據我們的需要,來建立我們需要的表頭,以此在DataGrid中顯示不同的資料.有心的同學可能注意到我們這篇文章顯示的資料與前面兩篇文章顯示的資料不一樣,前面文章DataGrid表格中顯示的資料是全部來自資料庫表,而我們這篇文章中顯示的資料不是直接來自資料庫的,是我們手動拼接成的,至於具體的實現我們在下一篇文章中進行介紹.
jQuery EasyUI-DataGrid動態載入表頭