easyUI 展開DataGrid里面的行顯示詳細信息


http://blog.csdn.net/yanghongchang_/article/details/7854156原著

datagrid 可以改變它的view(視圖)去顯示不同的效果.使用詳細視圖,datagrid可以顯示展開按鈕("+" 或者 "-")在數據行的左邊,用戶可以展開一個行去顯示一個附加的詳細信息.

查看 Demo

 

步驟 1: 創建 DataGrid

[html]  view plain  copy
 
  1. <table id="dg" style="width:500px;height:250px" url="data/datagrid_data.json" title="DataGrid - Expand Row" singleselect="true" fitcolumns="true">    
  2.     <thead>    
  3.         <tr>    
  4.             <th field="itemid" width="60">Item ID</th>    
  5.             <th field="productid" width="80">Product ID</th>    
  6.             <th field="listprice" align="right" width="70">List Price</th>    
  7.             <th field="unitcost" align="right" width="70">Unit Cost</th>    
  8.             <th field="status" width="50" align="center">Status</th>    
  9.         </tr>    
  10.     </thead>    
  11. </table>    

步驟 2: 為DataGrid設置詳細視圖

使用詳細視圖,切記:引入視圖script文件在你的頁面的頭部.

 

 

[html]  view plain  copy
 
  1. <script type="text/javascript" src="http://www.jeasyui.com/easyui/datagrid-detailview.js"></script>    
[javascript]  view plain  copy
 
  1. $('#dg').datagrid({    
  2.     view: detailview,    
  3.     detailFormatter:function(index,row){    
  4.         return '<div id="ddv-' + index + '" style="padding:5px 0"></div>';    
  5.     },    
  6.     onExpandRow: function(index,row){    
  7.         $('#ddv-'+index).panel({    
  8.             border:false,    
  9.             cache:false,    
  10.             href:'datagrid21_getdetail.php?itemid='+row.itemid,    
  11.             onLoad:function(){    
  12.                 $('#dg').datagrid('fixDetailRowHeight',index);    
  13.             }    
  14.         });    
  15.         $('#dg').datagrid('fixDetailRowHeight',index);    
  16.     }    
  17. });    

我們定義detailFormatter函數告訴datagrid 如何渲染詳細視圖,在這種情況下,我們返回一個簡單的 '<div>'元素,它將充當最為一個詳細內容的容器,

 

注意:詳細信息為空,當用戶點擊展開按鈕('+'),onExpandRow事件將被觸發,所以我們可以寫一些代碼去加載ajax詳細內容,最后我們調用fixDetailRowHeight方法去固定行高度,當詳細內容加載之后.

 

步驟 3: 服務器端代碼

 

datagrid21_getdetail.php

 

[php]  view plain  copy
 
  1. <?php    
  2. $itemid = $_REQUEST['itemid'];    
  3.     
  4. $content = file_get_contents('data/datagrid_data.json');    
  5. $data = json_decode($content,true);    
  6. foreach($data['rows'] as $item){    
  7.     if ($item['itemid'] == $itemid){    
  8.         break;    
  9.     }    
  10. }    
  11.     
  12. ?>    
  13.     
  14. <table class="dv-table" border="0" style="width:100%;">    
  15.     <tr>    
  16.         <td rowspan="3" style="width:60px">    
  17.             <?php    
  18.                 echo "<img src=\"images/$itemid.gif\" style=\"height:50px\"/>";    
  19.             ?>    
  20.         </td>    
  21.         <td class="dv-label">Item ID: </td>    
  22.         <td><?php echo $item['itemid'];?></td>    
  23.         <td class="dv-label">Product ID:</td>    
  24.         <td><?php echo $item['productid'];?></td>    
  25.     </tr>    
  26.     <tr>    
  27.         <td class="dv-label">List Price: </td>    
  28.         <td><?php echo $item['listprice'];?></td>    
  29.         <td class="dv-label">Unit Cost:</td>    
  30.         <td><?php echo $item['unitcost'];?></td>    
  31.     </tr>    
  32.     <tr>    
  33.         <td class="dv-label">Attribute: </td>    
  34.         <td colspan="3"><?php echo $item['attr1'];?></td>    
  35.     </tr>    
  36. </table>    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM