top
Loading...
jQuery EasyUI 數據網格 – 擴展行顯示細節

jQuery EasyUI 數據網格 - 擴展行顯示細節

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

步驟 1:創建數據網格(DataGrid)

    <table id="dg" style="width:500px;height:250px"
            url="datagrid8_getdata.asp"
            pagination="true" sortName="itemid" sortOrder="desc"
            title="DataGrid - Expand Row"
            singleSelect="true" fitColumns="true">
        <thead>
            <tr>
                <th field="itemid" width="60">Item ID</th>
                <th field="productid" width="80">Product ID</th>
                <th field="listprice" align="right" width="70">List Price</th>
                <th field="unitcost" align="right" width="70">Unit Cost</th>
                <th field="status" width="50" align="center">Status</th>
            </tr>
        </thead>
    </table>

步驟 2:為數據網格(DataGrid)設置詳細視圖

為了使用詳細視圖,請記得在頁面頭部引用視圖腳本文件。

<script type="text/javascript" src="http://www.w3cschool.cc/try/jeasyui/datagrid-detailview.js"></script>
$('#dg').datagrid({
    view: detailview,
    detailFormatter:function(index,row){
        return '<div class="ddv" style="padding:5px 0"></div>';
    },
    onExpandRow: function(index,row){
        var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
        ddv.panel({
            border:false,
            cache:false,
            href:'datagrid21_getdetail.asp?itemid='+row.itemid,
            onLoad:function(){
                $('#dg').datagrid('fixDetailRowHeight',index);
            }
        });
        $('#dg').datagrid('fixDetailRowHeight',index);
    }
});

我們定義 'detailFormatter' 函數,告訴數據網格(datagrid)如何渲染詳細視圖。 在這種情況下,我們返回一個簡單的 '<div>' 元素,它將充當詳細內容的容器。 請注意,詳細信息為空。當用戶點擊展開按鈕('+')時,onExpandRow 事件將被觸發。 所以我們可以寫一些代碼來加載 ajax 詳細內容。 最後我們調用 'fixDetailRowHeight' 方法來固定當詳細內容加載時的行高度。

步驟 3:服務器端代碼

datagrid21_getdetail.asp
&lt;?php
    include_once 'conn.asp';
    $itemid = mysql_real_escape_string($_REQUEST['itemid']);
    $rs = mysql_query("select * from item where itemid='$itemid'");
    $item = mysql_fetch_array($rs);
?&gt;
<table class="dv-table" border="0" style="width:100%;">
    <tr>
        <td rowspan="3" style="width:60px">
            &lt;?php
                $aa = explode('-',$itemid);
                $serno = $aa[1];
                $img = "images/shirt$serno.gif";
                echo "<img src=\"$img\" style=\"width:60px;margin-right:20px\" />";
            ?&gt;
        </td>
        <td class="dv-label">Item ID: </td>
        <td>&lt;?php echo $item['itemid'];?&gt;</td>
        <td class="dv-label">Product ID:</td>
        <td>&lt;?php echo $item['productid'];?&gt;</td>
    </tr>
    <tr>
        <td class="dv-label">List Price: </td>
        <td>&lt;?php echo $item['listprice'];?&gt;</td>
        <td class="dv-label">Unit Cost:</td>
        <td>&lt;?php echo $item['unitcost'];?&gt;</td>
    </tr>
    <tr>
        <td class="dv-label">Attribute: </td>
        <td colspan="3">&lt;?php echo $item['attr1'];?&gt;</td>
    </tr>
</table>
北斗有巢氏 有巢氏北斗