Easyui Expand the rows in the DataGrid display details

Source: Internet
Author: User

@author YHC

The DataGrid can change its view to show different effects. Using the detailed view, the DataGrid can display the expand button ("+" or "-") to the left of the data row, and the user can expand a row to display an additional detail.


View Demo

Step 1: Create a DataGrid

<table id= "DG" style= "width:500px;height:250px" url= "Data/datagrid_data.json" title= "Datagrid-expand Row" Singleselect= "true" fitcolumns= "true" >  
    <thead>  
        <tr>  
            <th field= "itemid" width= "60" >item id</th>  
            <th field= "ProductID" Width= "no" >product id</th> <th field=  
            "ListPrice" "Align=" right "width=" >list price</th> <th field= "UnitCost" align= "right" width= "," >unit cost  
            </th>  
            <th field= "status" width= "M" align= "center" >Status</th>  
        </tr>  
    </ Thead>  
</table>  
Step 2: Set up a detailed view for the DataGridUsing the Detailed view, remember: the introduction of the view script file on the head of your page.

<script type= "Text/javascript" src= "Http://www.jeasyui.com/easyui/datagrid-detailview.js" ></script>  
$ (' #dg '). DataGrid ({  
    View:detailview,  
    detailformatter:function (index,row) {return  
        ' <div id= ' ddv-' + Index + ' "style=" padding:5px 0 "></div>";  
    },  
    onexpandrow:function (index,row) {  
        $ (' #ddv-' +index ). Panel ({  
            border:false,  
            cache:false,  
            href: ' datagrid21_getdetail.php?itemid= ' +row.itemid,  
            Onload:function () {  
                $ (' #dg '). DataGrid (' Fixdetailrowheight ', index);  
            }  
        });  
        $ (' #dg '). DataGrid (' Fixdetailrowheight ', index);  
    }  
});  
We define the Detailformatter function to tell the DataGrid how to render the detailed view, in which case we return a simple ' <div> ' element that will act as a container for the most detailed content.

Note: The details are empty, when the user clicks on the expand button (' + '), the Onexpandrow event will be triggered, so we can write some code to load the AJAX details, and finally we call the Fixdetailrowheight method to fix the row height when the details are loaded.

Step 3: Server-side code

datagrid21_getdetail.php

<?php $itemid = $_request[' itemid '];  
$content = file_get_contents (' Data/datagrid_data.json ');  
$data = Json_decode ($content, true);  
    foreach ($data [' Rows '] as $item) {if ($item [' itemid '] = = $itemid) {break; }?> <table class= "dv-table" border= "0" style= "width:100%;" > <tr> <td rowspan= "3" style= "width:60px" > <?php echo "&  
            Lt;img src=\ "images/$itemid. gif\" style=\ "height:50px\"/> "; ?> </td> <td class= "Dv-label" >item ID: </td> <td><?php echo $i tem[' itemid '];? ></td> <td class= "Dv-label" >product id:</td> <td><?php echo $item [' Produ Ctid '];?  
        ></td> </tr> <tr> <td class= "Dv-label" >list Price: </td> <td><?php echo $item [' ListPrice '];? ></td> <td class= "Dv-label" &Gt Unit cost:</td> <td><?php echo $item [' unitcost '];? ></td> </tr> <tr> <td class= "Dv-label" >attribute: </td> & LT;TD colspan= "3" ><?php echo $item [' attr1 '];?   ></td> </tr> </table>
Download Easyui Sample code:Easyui-datagrid-demo.zip








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.