Creating expandable master-details table (JQuery DataTables and ASP.NET MVC integration - Part IV)
Introduction
Creating fully functional tables with pagination, sorting, filtering features is an easy task if you use JQuery DataTables plugin. This plugin enables you to add all of these functionalities to the plain HTML table using a single line of code shown in the following example:
("#myTable").dataTables();
This line of code finds a HTML table with an id “myTable” and adds pagination, sorting, and filtering by keyword functionalities. A plain HTML table is converted into the fully functional table shown on the figure below:
In this article will be shown how the standard DataTable can be extended to become expandable master-details table. Goal of the article is to present how can be shown list of companies with expand button that will show list of employees for the selected company as it is shown in the figure below:
In this exampe each record in the table (companies in this example) has expand button and when this button is clicked, details are opened. Details can be either some child records (employees that works for this company as in this example), additional details about the company (e.g. logo, description, and other information that are not shown in table row) or combination of these two.
Background
Expandable tables are common requirement in the situations where you have related entities. If you have companies that have employees, teachers that have students, sales order with sales order items probably you will have some requirement to implement list of master records (companies, teachers, sales orders) with ability to show related records for each master row when the master row is expanded. Other situation where you might need expandable tables is when just minimal set of columns is shown in table and there is a requirement that when user clicks on any row more details should be shown in the expanded row. These requirement can be easily implemented using the JQuery DataTables plugin – this article describes how it should be configured, what client-side logic should be added and what should be implemented on the server-side to support expandable table on the client-side.
When expandable table is implemented, the following scenario is used:
- One additional column is added to the table, containing the expand/collapse buttons that enables user to expand selected table row,
- When user click on the expand button, AJAX call is sent to the server and HTML for details will be returned,
- HTML that is returned is injected as a sub-row using DataTables fnOpen function and expand button is converted to collapse button,
- When user click on the collapse button sub-row is closed using the DataTables fnClose function and collapse button is converted back to the expand button.
Using the code
In this example, it is shown how expandable master-details table can be implemented in ASP.NET MVC using JQuery DataTables plugin. Three parts of the project need to be implemented:
- Model where are defined structures of data that will be shown,
- Controller – class that reacts on the requests sent by the DataTables plugin,
- View – rendering engine and client side logic that need to be implemented.
In the following sections are described these parts.
Model
The model is represented as a two classes related with one-to-many relationship. In this example are used companies as a master information and employees as details. Source code of the model classes is shown in the listing below:
public class Company
{
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
public class Employee
{
public int EmployeeID { get; set; }
public string Name { get; set; }
public string Position { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public int CompanyID { get; set; }
}
Relationship between employees and their companies is established via CompanyID property in the employee class.
Controller
Controller class responds to the user actions and return responses. From the client-side will be sent request for list of employees by company id that is sent when the expand button is presses. This request will contain id of the company for whom the list of emplyees shoudl be shown in the expanded row. Action method in the controller that return employees by company id is shown in the listing below:
public class HomeController : Controller
{
public ActionResult CompanyEmployees(int? CompanyID)
{
var employees = DataRepository.GetEmployees();
var companyEmployees = (from e in employees
where (CompanyID == null || e.CompanyID == CompanyID)
select e).ToList();
return View(companyEmployees);
}
}
This action method takes id of the company and finds all employees with that company id. There is a one utility partial view that format returned list of companies – this view is shown below:
@model IEnumerable<JQueryDataTables.Models.Employee>
<table cellpadding="4" cellspacing="0" border="0" style="padding-left:50px;">
<tr>
<th>Employee</th>
<th>Position</th>
<th>Phone</th>
<th>Email</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>@item.Name</td>
<td>@item.Position</td>
<td>@item.Phone</td>
<td>@item.Email</td>
</tr>
}
</table>
In this view is generated a HTML table, using a list of employees that are filtered by the controller. This HTML code is sent back to the client-side as a AJAX response. In the following figure is shown AJAX response of the Home/CompanyEmployees call traced in the FireBug:
View
View pages are used to show tables and contain client-side JavaScripts. There are three view pages used in this example:
- Static view page where table of companies is static HTML table,
- Server side view where table of companies is generated on the server-side,
- Ajax view page where table of companies is loaded via AJAX call.
However, all these view uses the same client-side logic for showing details (list of employees) – ajax call is sent to the controller action /Home/CompanyEmployees described above and HTML response returned by the controller is injected in the table.
All three views uses the same layout page where are included common elements required by all of them. This layout page is shown in the following listing:
<html>
<head>
<title>Implementation of Master-Details tables using a JQuery DataTables plugin</title>
<link href="@Url.Content("~/Content/dataTables/demo_page.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/dataTables/demo_table.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/dataTables/demo_table_jui.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" media="all" />
<link href="@Url.Content("~/Content/themes/smoothness/jquery-ui-1.7.2.custom.css")"rel="stylesheet" type="text/css" media="all" />
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.dataTables.min.js")" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
@RenderSection("onReady", required: false)
});
</script>
</head>
<body id="dt_example">
<div id="container">
<a href="/Home/Index">Static table</a> |
<a href="/Home/ServerSide">Server-side generated table</a> |
<a href="/Home/Ajax">Ajax-loaded table</a>
@RenderBody()
</div>
</body>
</html>
On the layout page are included all necessary CSS/JavaScript files, defined common structure of the page and left two sections that will be defined on the particular pages. These sections are:
- onReady section used to include custom JavaScript that will be executed on document ready event,
- Body section where HTML code for each page will be placed.
In the following examples are described three different cases of usage of expandable table.
Static view
In the static view page, primary company table is generated as a plain HTML table. HTML source of the static table is shown in the following listing:
<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>
...
</tbody>
</table>
</div>
This code is placed in the body section of the view. In the first column is placed image with rel attribute equal to the id of the company that is shown. When user clicks on this image, AJAX call will be sent to the server-side controller and HTML that is returned will be shown as an expanded row. JavaScript code that performs this action is placed in the head section of the view and it is shown below:
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');
});
}
});
In the JavaScript shown above, click handler is added to the each image in the first column. When a user clicks on the image, this code checks whether the image is in closed state or not. If image is in the closed state AJAX call is sent to the server, returned HTML is injected using a fnOpen function, and image is changed, otherwise, details row is closed using a fnClose function. oTable variable is a refernece to the DataTable object initialized in the foowing code.
You will need to add DataTables initialization code that will initialize company table. This is required in order to use fnOpen and fnClose functions. Example of DataTable initialization code is shown below. Only specific settings is usage of JQueryUI for styles and making first column (containing image) non-sortable and non-searchable.
/* Initialize table and make first column non-sortable*/
oTable = $('#companies').dataTable({ "bJQueryUI": true,
"aoColumns": [
{ "bSortable": false,
"bSearchable": false },
null,
null,
null
]
});
Server-side generated view
In the second example in this article, company table is generated on the server-side dynamically. Body of the view page is shown in the following listing:
<div id="demo">
<table id="companies" class="display">
<thead>
<tr>
<th></th>
<th>Company Name</th>
<th>Address</th>
<th>Town</th>
</tr>
</thead>
@foreach (var item in Model) {
<tr>
<td><img src="/Content/images/details_open.png" alt="expand/collapse" rel="@item.ID"/></td>
<td>@item.Name</td>
<td>@item.Address</td>
<td>@item.Town</td>
</tr>
}
</table>
</div>
As you can see there are no big differences - view dynamically generates the same structure as in the previous example. Therefore, on ready section is same as in the previous view.
Note that this view uses list of companies for generating table, therefore action method should pass list of all companies in the application. Example of the action method that is used in this example to pass model to the view is shown in the listing below:
public class HomeController : Controller
{
public ActionResult ServerSide()
{
return View(DataRepository.GetCompanies());
}
}
Ajax view page
The last example in this article is a view page where companies are dynamically loaded via AJAX call. This is performance improvement of the previous case that should be used if you expect that there will be large number of items in the table, and that loading of of them at once could cause perfrmance issue. The body of the view page just represents a structure of the table as shown in the listing below:
<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>
</tbody>
</table>
</div>
On ready section that contains initialization code is slightly modified as shown in the following listing:
var oTable;
$('#companies tbody td img').live('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');
});
}
});
Difference is that ‘live’ function is used instead of the direct click handler because event should be added to the elements that are dynamically added in each reload. Without live function, you will need to apply click handler each time table is reloaded from the server side.
DataTables initialization call is also changed - you will need to set bServerSide
parameter to true indicating that companies will be loaded from AJAX source, and server side page that will provide table data. Initialization call is shown in the following listing:
/* Initialize table and make first column non-sortable*/
oTable = $('#companies').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": 'AjaxHandler',
"bJQueryUI": true,
"aoColumns": [
{ "bSortable": false,
"bSearchable": false,
"fnRender": function (oObj) {
return '/Content/images/details_open.png" alt="expand/collapse" rel="' + oObj.aData[0] + '"/>';
}
},
null,
null,
null
]
});
Note that in the first column is used fnRender
function to generate image and place ID of the company in the rel attribute of the expand/collapse image. Setting server side processing in ASP.NET MVC is out of scope of this article so I recommend you to take a look at the Integrating DataTables into the ASP.NET MVC application article (this is a first article in this group of articles).
Conclusion
In this article is explained how you can create expandable master details table using JQuery DataTables plugin and ASP.NET MVC. This is a fourth article in the group of article I wrote about the integration of JQuery DataTables plugin into the ASP.NET MVC application. Few other articles that might interest you are:
- Integration JQuery DataTables plugin into the ASP.NET MVC application where is described how you can implement server-side processing in ASP.NET in order to implement high performances server side table using JQuery DataTables plugin,
- Creating editable DataTables in ASP.NET MVC where is described how you can implement add, edit and delete functionalities in data table,
- Creating parent-child relationships between the tables in ASP.NET MVC – and article similar to this one where is explained how you could connect two tables in the parent child manner.
Articles in this group might enable you to create functional and effective tables in ASP.NET MVC using the JQuery DataTables plugin.
发表评论
really appreciate your content. Please let me know.
Very good info. Lucky me I found your website by accident (stumbleupon). I ave bookmarked it for later!
Wow, fantastic blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is great, as well as the content!
very couple of websites that happen to be in depth beneath, from our point of view are undoubtedly nicely worth checking out
Is it possible to change A Menu Items Type
Usually I don at read post on blogs, but I would like to say that this write-up very forced me to try and do so! Your writing taste has been amazed me. Thank you, very nice article.
cJXeV9 Wordpress or go for a paid option? There are so many options
It as laborious to search out knowledgeable people on this matter, but you sound like you realize what you are speaking about! Thanks
I truly appreciate this blog article.Thanks Again. Fantastic.
This can be a really very good study for me, Should admit which you are a single of the best bloggers I ever saw.Thanks for posting this informative write-up.
in the evening as the sun sets. The study was undertaken by
Pretty! This has been an extremely wonderful post. Thank you for providing these details.
You made some really good points there. I checked on the internet for more info about the issue and found most people will go along with your views on this web site.
Looking forward to reading more. Great blog article.Thanks Again. Keep writing.
Wow, superb weblog format! How lengthy have you been blogging for? you made running a blog glance easy. The overall glance of your website is fantastic, let alone the content material!
Wow, incredible blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is wonderful, as well as the content!
The electronic cigarette makes use of a battery and a small heating aspect the vaporize the e-liquid. This vapor can then be inhaled and exhaled
This is one awesome blog post.Really thank you! Really Great.
This blog was how do I say it? Relevant!! Finally I have found something which helped me. Kudos!
wow, awesome blog.Much thanks again. Really Great.
Merely a smiling visitant here to share the love (:, btw great design and style.
You have noted very interesting details! ps nice internet site.
I relish, cause I discovered exactly what I used to be having a look for. You have ended my four day long hunt! God Bless you man. Have a great day. Bye
Say, you got a nice article post. Great.
I think this is a real great article post.Much thanks again. Really Great.
on some general things, The site style is ideal, the articles is really
Yes, you are right buddy, daily updating web site is genuinely needed in favor of Web optimization. Good argument keeps it up.
Wow, marvelous weblog format! How lengthy have you been running a blog for? you make blogging look easy. The entire look of your site is wonderful, as well as the content!
I visited a lot of website but I believe this one holds something special in it in it
Very informative blog post.Really looking forward to read more. Much obliged.
Im obliged for the blog post.Really thank you! Great.
Muchos Gracias for your blog.Really looking forward to read more. Much obliged.
with hackers and I am looking at alternatives for another platform.
Wow! Thank you! I constantly needed to write on my website something like that. Can I implement a part of your post to my site?
I'аve read several excellent stuff here. Definitely worth bookmarking for revisiting. I wonder how so much attempt you put to make any such excellent informative web site.
Im grateful for the blog article.Thanks Again. Much obliged.
This is one awesome blog article.Much thanks again. Keep writing.
Thanks for the blog article.Really thank you! Great.
You have made some good points there. I checked on the web to find out more about the issue and found most individuals will go along with your views on this website.
Whenever you hear the consensus of scientists agrees on something or other, reach for your wallet, because you are being had.
Wow, fantastic blog layout! How long have you ever been running a blog for? you made running a blog look easy. The overall look of your site is fantastic, let alone the content material!
You generated some decent points there. I looked on-line for that challenge and identified most people will go coupled with with all of your website.
Souls in the Waves Fantastic Early morning, I just stopped in to go to your internet site and assumed I would say I experienced myself.
Spot on with this write-up, I truly suppose this website wants way more consideration. I all in all probability be again to learn much more, thanks for that info.
YES! I finally found this web page! I ave been looking just for this article for so long!!
My brother suggested I might like this web site. He was totally right. This post truly made my day. You cann at imagine just how much time I had spent for this information! Thanks!
Yay google is my world beater aided me to find this outstanding site!.
You made some good points there. I did a search on the subject and found most individuals will go along with with your site.
Thank you, I have just been looking for information about
Thanks for sharing this great post. Very interesting ideas! (as always, btw)