MVC: Custom sorting for HTML table

  • Thread starter Tommy Holm Jakobsen
  • Start date
T

Tommy Holm Jakobsen

Hi,

I was wondering how I can sort a HTML table the smartest way. I've got
the following table:

<table id="customersTable" class="dataTable">
<thead>
<tr>
<th>Kundenavn</th>
<th>POB kunde nr.</th>
<th>SAP kontrakt nr.</th>
<th>Start dato</th>
<th>Brugere</th>
</tr>
</thead>
<tbody>
<% foreach (var c in ViewData["Customers"] as List<Customer>)
{ %>
<tr>
<td><%= Html.ActionLink(c.Name, c.ID.ToString(),
"Customers") %></td>
<td class="alignRight"><%= c.POBNo %></td>
<td class="alignRight"><%= c.SAPContractNo %></td>
<td class="alignRight"><%=
c.StartDate.ToString("dd-MM-yy") %></td>
<td class="alignRight"><%= c.Users.Count %></td>
</tr>
<% } %>
</tbody>
</table>

Where each header cell should be clickable to sort the choosen column.

The table datasource is a List<Customer>, published using LINQ to SQL.
Heres the controller:

AdvoforumDataContext advoforum = new AdvoforumDataContext();
List<Customer> customers = advoforum.GetCustomers();
ViewData["Customers"] = customers;
return View();

Could you give me a hint how to do this the "best" way?
 
B

bruce barker

you have two options. sort client side using javascript (which is how i'd do
it), or add an anchor on the column heads and sort server side:

<th><a href="list/sort/1">Kundenavn</a></th>
<th><a href="list/sort/2">POB kunde nr.</a></th>

where you add a sort method to the list controller.

-- bruce (sqlwork.com)
 
T

Tommy Holm Jakobsen

you have two options. sort client side using javascript (which is how i'd do
it), or add an anchor on the column heads and sort server side:

<th><a href="list/sort/1">Kundenavn</a></th>
<th><a href="list/sort/2">POB kunde nr.</a></th>

where you add a sort method to the list controller.

-- bruce (sqlwork.com)


Tommy Holm Jakobsen said:
Hi,

I was wondering how I can sort a HTML table the smartest way. I've got
the following table:

<table id="customersTable" class="dataTable">
<thead>
<tr>
<th>Kundenavn</th>
<th>POB kunde nr.</th>
<th>SAP kontrakt nr.</th>
<th>Start dato</th>
<th>Brugere</th>
</tr>
</thead>
<tbody>
<% foreach (var c in ViewData["Customers"] as List<Customer>)
{ %>
<tr>
<td><%= Html.ActionLink(c.Name, c.ID.ToString(),
"Customers") %></td>
<td class="alignRight"><%= c.POBNo %></td>
<td class="alignRight"><%= c.SAPContractNo %></td>
<td class="alignRight"><%=
c.StartDate.ToString("dd-MM-yy") %></td>
<td class="alignRight"><%= c.Users.Count %></td>
</tr>
<% } %>
</tbody>
</table>

Where each header cell should be clickable to sort the choosen column.

The table datasource is a List<Customer>, published using LINQ to SQL.
Heres the controller:

AdvoforumDataContext advoforum = new AdvoforumDataContext();
List<Customer> customers = advoforum.GetCustomers();
ViewData["Customers"] = customers;
return View();

Could you give me a hint how to do this the "best" way?
Thank you. I've found a way to do it client side using Javascript.

- Tommy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top