browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need ASP.NET help?

Get answers from our community of ASP.NET experts on BYTES! It's free.

MVC: Custom sorting for HTML table

Tommy Holm Jakobsen
Guest
 
Posts: n/a
#1: Jul 8 '08
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<Customercustomers = advoforum.GetCustomers();
ViewData["Customers"] = customers;
return View();

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



=?Utf-8?B?YnJ1Y2UgYmFya2Vy?=
Guest
 
Posts: n/a
#2: Jul 8 '08

re: MVC: Custom sorting for HTML table


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" wrote:
Quote:
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<Customercustomers = advoforum.GetCustomers();
ViewData["Customers"] = customers;
return View();
>
Could you give me a hint how to do this the "best" way?
>
Tommy Holm Jakobsen
Guest
 
Posts: n/a
#3: Jul 8 '08

re: MVC: Custom sorting for HTML table


On Tue, 8 Jul 2008 09:02:10 -0700, bruce barker
<brucebarker@discussions.microsoft.comwrote:
Quote:
>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" wrote:
>
Quote:
>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<Customercustomers = 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
Closed Thread