jQuery EasyUI データグリッド -拡張行の詳細表示

データグリッド(datagrid)は、そのビュー(view)を変更することで異なる効果を表示できます。詳細ビューを使用すると、データグリッド(datagrid)はデータ行の左側に展開ボタン(「+」または「-」)を表示できます。ユーザーは行を展開して、追加の詳細情報を表示できます。

ステップ 1:データグリッド(DataGrid)を作成する

    <table id="dg" style="width:500px;height:250px"
            url="datagrid8_getdata.php"
            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.php?itemid='+row.itemid,
            onLoad:function(){
                $('#dg').datagrid('fixDetailRowHeight',index);
            }
        });
        $('#dg').datagrid('fixDetailRowHeight',index);
    }
});

私たちは「detailFormatter」関数を定義し、データグリッド(datagrid)に詳細ビューのレンダリング方法を指示します。この場合、詳細コンテンツのコンテナとして機能する単純な「<div>」要素を返します。詳細情報は空であることに注意してください。ユーザーが展開ボタン(「+」)をクリックすると、onExpandRow イベントがトリガーされます。そこで、ajax の詳細コンテンツをロードするコードを書くことができます。最後に、「fixDetailRowHeight」メソッドを呼び出して、詳細コンテンツのロード時の行の高さを固定します。

ステップ 3:サーバーサイドコード

datagrid21_getdetail.php
&lt;?php
    include_once 'conn.php';

    $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>

jQuery EasyUI インスタンスをダウンロード

jeasyui-datagrid-datagrid21.zip

その他の拡張