Click here to Skip to main content
15,884,095 members
Articles / Web Development / HTML

Creating an Expandable Master-Details Table (jQuery DataTables and ASP.NET MVC Integration - Part IV)

Rate me:
Please Sign up or sign in to vote.
4.87/5 (55 votes)
2 May 2011CPOL9 min read 391.6K   15.9K   124  
Implement an expandable master-details table in ASP.NET MVC using jQuery DataTables plug-in
This article shows how an expandable master-details table can be implemented in ASP.NET MVC using the jQuery DataTables plug-in.
@{
    Layout = "~/Views/Home/MasterDetailsLayoutPage.cshtml";
}


@section onReady{

    var oTable;
    $('#companies tbody td img').click(function () {
        var nTr = this.parentNode.parentNode;
        if (this.src.match('details_close')) {
            /* This row is already open - close it */
            this.src = "/Content/images/details_open.png";
            oTable.fnClose(nTr);
        }
        else {
            /* Open this row */
            this.src = "/Content/images/details_close.png";
            var companyid = $(this).attr("rel");
            $.get("CompanyEmployees?CompanyID=" + companyid, function (employees) {
                oTable.fnOpen(nTr, employees, 'details');
            });
        }
    });

        /* Initialize table and make first column non-sortable*/
   oTable = $('#companies').dataTable({  "bJQueryUI": true,
                                         "aoColumns": [
			                                { "bSortable": false, "bSearchable": false },
			                                null,
			                                null,
			                                null
		                            ]
        });

}

    <div id="demo">
        <table id="companies" class="display">
            <thead>
                <tr>
                    <th></th>
                    <th>Company name</th>
                    <th>Address</th>
                    <th>Town</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><img src="/Content/images/details_open.png" rel="0" alt="expand/collapse"></td>
                    <td>Emkay Entertainments</td>
                    <td>Nobel House, Regent Centre</td>
                    <td>Lothian</td>
                </tr>
                <tr>
                    <td><img src="/Content/images/details_open.png" rel="0" alt="expand/collapse"></td>
                    <td>The Empire</td>
                    <td>Milton Keynes Leisure Plaza</td>
                    <td>Buckinghamshire</td>
                </tr>
                <tr>
                    <td><img src="/Content/images/details_open.png" rel="2" alt="expand/collapse"></td>
                    <td>Asadul Ltd</td>
                    <td>Hophouse</td>
                    <td>Essex</td>
                </tr>
                <tr>
                    <td><img src="/Content/images/details_open.png" rel="3" alt="expand/collapse"></td>
                    <td>Ashley Mark Publishing Company</td>
                    <td>1-2 Vance Court</td>
                    <td>Tyne &amp; Wear</td>
                </tr>
                <tr>
                    <td><img src="/Content/images/details_open.png" rel="4" alt="expand/collapse"></td>
                    <td>MuchMoreMusic Studios</td>
                    <td>Unit 29</td>
                    <td>London</td>
                </tr>
                <tr>
                    <td><img src="/Content/images/details_open.png" rel="5" alt="expand/collapse"></td>
                    <td>Audio Records Studios</td>
                    <td>Oxford Street</td>
                    <td>London</td>
                </tr>
            </tbody>
        </table>


    </div>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Program Manager Microsoft
Serbia Serbia
Graduated from Faculty of Electrical Engineering, Department of Computer Techniques and Informatics, University of Belgrade, Serbia.
Currently working in Microsoft as Program Manager on SQL Server product.
Member of JQuery community - created few popular plugins (four popular JQuery DataTables add-ins and loadJSON template engine).
Interests: Web and databases, Software engineering process(estimation and standardization), mobile and business intelligence platforms.

Comments and Discussions