第一個jqGrid範例:Local array

jqGrid是一個jQuery的擴充外掛,透過它能很快速的撰寫資料Grid展示處理。目前正在評估是否將jqGrid整合到專案裡,因此這幾天做了一些測試,趁記憶猶新趕緊記錄下來。

首先由http://www.trirand.com/blog/?page_id=6下載jqGrid最新的示範壓縮檔jqgrid_demo36.zip,再將檔案解壓縮到測試用的Webapp裡。

jqGrid的資料區塊要存放在一個<table>裡,頁面導覽面板(Navigation Pager)則使用一個<div>,因此要顯示jqGrid的資料只要有下列兩組標籤,在執行jQuery("#grid_id").jqGrid()後,jqGrid就會自動設置這兩個標籤並填入資料:

<table id="grid_id"></table>
<div id="pager"></div>

jqGrid最基本的使用格式就是:

jQuery("#grid_id").jqGrid(options);

不同的設定選項就是透過options來處理。options的詳細說明要參考這裡的列表

jqGrid能處理的資料類型有XML、JSON、陣列等,我們必須在options裡用datatype來指定要使用的是那一種資料,並提供對應的資料給jqGrid處理。在這個範例裡使用最簡單datatype="local"並提供陣列資料給jqGrid(程式取自上述範例資料夾裡的localex.html)。

options用colNames與colModel來定義資料欄位的描述與欄位設定,colNames指定欄位標題字串,而colModel則要分別設定每個欄位的定義,細節可以參考ColModel API

範例程式如下(程式的顯示有些問題,請盡量使用文末的檔案連結來檢視):

<html>
<head>
<link rel="stylesheet" type="text/css" href="themes/redmond/jquery-ui-1.7.1.custom.css" media="screen" />
<link rel="stylesheet" type="text/css" href="themes/ui.jqgrid.css" media="screen" />
<link rel="stylesheet" type="text/css" href="themes/ui.multiselect.css" media="screen" />
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script>
<script src="js/jquery.layout.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script type="text/javascript">
  $.jgrid.no_legacy_api = true;
  $.jgrid.useJSON = true;
</script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script src="js/jquery.tablednd.js" type="text/javascript"></script>
<script src="js/jquery.contextmenu.js" type="text/javascript"></script>
<script src="js/ui.multiselect.js" type="text/javascript"></script>
</head>
<body>
<table id="grid_id"><tbody></tbody></table>
</body>
</html>

<script>
  $(document).ready(function() {
    jQuery("#grid_id").jqGrid({
      datatype: "local",
      height: 250,
      colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'],
      colModel:[
        {name:'id',index:'id', width:60, sorttype:"int"},
        {name:'invdate',index:'invdate', width:90, sorttype:"date"},
        {name:'name',index:'name', width:100},
        {name:'amount',index:'amount', width:80, align:"right",sorttype:"float"},
        {name:'tax',index:'tax', width:80, align:"right",sorttype:"float"},
        {name:'total',index:'total', width:80,align:"right",sorttype:"float"},
        {name:'note',index:'note', width:150, sortable:false}
      ],
      multiselect: true,
      caption: "Manipulating Array Data"
    });  // jqGrid

    var mydata = [
      {id:"1", invdate:"2007-10-01", name:"test", note:"note", amount:"200.00", tax:"10.00", total:"210.00"},
      {id:"2", invdate:"2007-10-02", name:"test2", note:"note2", amount:"300.00", tax:"20.00", total:"320.00"},
      {id:"3", invdate:"2007-09-01", name:"test3", note:"note3", amount:"400.00", tax:"30.00", total:"430.00"},
      {id:"4", invdate:"2007-10-04", name:"test", note:"note", amount:"200.00", tax:"10.00", total:"210.00"},
      {id:"5", invdate:"2007-10-05", name:"test2", note:"note2", amount:"300.00", tax:"20.00", total:"320.00"},
      {id:"6", invdate:"2007-09-06", name:"test3", note:"note3", amount:"400.00", tax:"30.00", total:"430.00"},
      {id:"7", invdate:"2007-10-04", name:"test", note:"note", amount:"200.00", tax:"10.00", total:"210.00"},
      {id:"8", invdate:"2007-10-03", name:"test2", note:"note2", amount:"300.00", tax:"20.00", total:"320.00"},
      {id:"9", invdate:"2007-09-01", name:"test3", note:"note3", amount:"400.00", tax:"30.00", total:"430.00"}
    ];

    for(var i=0;i< =mydata.length;i++)
      jQuery("#grid_id").jqGrid('addRowData',i+1,mydata[i]);
    });
</script>

執行後的網頁顯示如下:

jqGrid Array screen

範例檔案下載(請用右鍵另存目標)

範例1 Local array: my_localex.html
範例2 Local array 2: my_localex2.jsp
範例3
  1. XML類型: my_xmlex.jsp
  2. XML字串類型: my_xmlstringex.jsp
  3. JSP含入檔: jq_inc.jspf
  4. 產生資料的程式範例:test1_xml.jsp
範例4 資料列處理程式: my_manipex.jsp

##

您可能也會有興趣的類似文章

1則留言 »第一個jqGrid範例:Local array

  • hero Windows XP Internet Explorer 8.0

    請問如果每row最前面是click,然後第二個要用超連結選項(如 編緝),之後才是真正的資料格,要用什麼語法呢? 感謝您。

填寫回應

 

 

 

您可使用這些HTML標籤

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

隨機文章

  1. [ADS] 在Linux上執行Aqua Data Studio 
  2. [WordPress] 在插頁寫PHP的外掛:EXEC-PHP,讓行動版網頁顯示訪客統計 
  3. 增加Spb Mobile Shell 3.0生活化桌面頁數的小技巧 (7)
  4. Windows 7的常用快速鍵速查表 
  5. Windows Mobile使用的完美聯絡人軟體:Inesoft Phone [修訂] (1)
  6. 變化你的噗浪暱稱:Unicode我♥你-啦啦啦♪☺〠 (2)
  7. 超級起手式-兼具效率與彈性的程式啟動器FARR 
  8. TiddlyWiki: 增加第二組編輯區工具列 
  9. [轉貼] 昇陽新授權要強化Java與Linux相容性 
  10. [Tools] 另一套Free的剪貼簿工具:ClipX (1)

歷史熱門文章

  1. 如何手動輸入Plurk的表情圖示? (352,914 點擊/2008-09-29)
  2. 簡單放大Plurk輸入區的方法[修訂] (86,912 點擊/2008-09-29)
  3. 啟用&關閉中華電信ZyXEL P874的無線網路連線功能的步驟[修訂] (83,726 點擊/2011-06-01)
  4. Plurk CSS樣式自訂修改全攻略 (83,045 點擊/2009-07-04)
  5. 網路升級光世代50M/3M與設定ZyXEL P874 (53,128 點擊/2011-05-28)
  6. 輕鬆學會彈指神功-揭露AutoHotkey絕技 (43,114 點擊/2008-04-10)
  7. iPad新手入門技巧 (40,345 點擊/2011-02-05)
  8. 分享世博台灣館主題曲『台灣的心跳聲』MV(附歌詞+下載連結+新歌詞) (38,131 點擊/2010-04-27)
  9. 把HTC Touch HD操作介面換成Spb Mobile Shell,美觀又便利! (37,943 點擊/2009-02-18)
  10. 另一套適用手機的影片轉檔工具:WinAVI 3GP/MP4/PSP/iPod Video Converter (35,894 點擊/2009-04-18)