Alt*_*ept 15 sorting asp.net-mvc html-table
我想知道人们如何在asp.net mvc中对表进行排序?我听说过javascript解决方案非常适用于非分页表,例如jquery的表分类器,但我需要一个可以处理分页表的解决方案.
我正在研究的项目目前使用以下解决方案,但我发现它非常混乱.
调节器
public ActionResult Sort(string parameter)
{
IEnumerable<IProduct> list;
if (Session["Model"] != null)
list = (IEnumerable<IProduct>)Session["Model"]).ToList<IProduct>();
else
list = _service.GetAll();
if (Session["parameter"] == null && Session["sortDirection"] == null)
{
//set the parameter and set the sort to desc
Session["parameter"] = parameter;
Session["sortDirection"] = "DESC";
}
else if (Session["parameter"] != null) //already set so not the first time
{
//same parameter sent
if (Session["parameter"].ToString().Equals(parameter))
{
//check sort direction and reverse
if (Session["sortDirection"].ToString().Equals("DESC"))
Session["sortDirection"] = "ASC";
else
Session["sortDirection"] = "DESC";
}
else //different parameter sent
{
Session["sortDirection"] = "DESC";
Session["parameter"] = parameter;
}
}
if (Session["sortDirection"].CompareTo("ASC") == 0)
list = Models.ContollerHelpers.SortingHelper.OrderBy(list.AsQueryable(), column);
else
list = Models.ContollerHelpers.SortingHelper.OrderByDescending(list.AsQueryable(), column);
return View("Results", list.ToList);
}
Run Code Online (Sandbox Code Playgroud)
帮手
public class Helper()
{
private static IOrderedQueryable<T> OrderingHelper<T>(IQueryable<T> source, string propertyName, bool descending, bool anotherLevel)
{
ParameterExpression param = Expression.Parameter(typeof(T), string.Empty); // I don't care about some naming
MemberExpression property = Expression.PropertyOrField(param, propertyName);
LambdaExpression sort = Expression.Lambda(property, param);
MethodCallExpression call = Expression.Call(
typeof(Queryable),
(!anotherLevel ? "OrderBy" : "ThenBy") + (descending ? "Descending" : string.Empty),
new[] { typeof(T), property.Type },
source.Expression,
Expression.Quote(sort));
return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(call);
}
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string propertyName)
{
return OrderingHelper(source, propertyName, false, false);
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string propertyName)
{
return OrderingHelper(source, propertyName, true, false);
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string propertyName)
{
return OrderingHelper(source, propertyName, false, true);
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string propertyName)
{
return OrderingHelper(source, propertyName, true, true);
}
}
Run Code Online (Sandbox Code Playgroud)
列表显示
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Models.Interface.IProduct>>" %>
<% Session["model"] = Model; %>
<table>
<tr>
<th>
Edit Details
</th>
<th>
<%=Html.ActionLink("Id","Sort",new {parameter ="Id"}) %>
</th>
<th>
<%=Html.ActionLink("Name", "Sort", new { parameter = "Name"})%>
</th>
<th>
<%=Html.ActionLink("Status", "Sort", new { parameter = "Status" })%>
</th>
<th>
<%=Html.ActionLink("Notes", "Sort", new { parameter = "Notes"})%>
</th>
</tr>
<% foreach (var item in Model){ %>
<tr>
<td>
<%= Html.ActionLink("Edit", "Edit", new { id=item.Id }) %> |
</td>
<td>
<%= Html.Encode(item.Id) %>
</td>
<td>
<%= Html.Encode(item.Name) %>
</td>
<td>
<%= Html.Encode(item.Status) %>
</td>
<td>
<%= Html.Encode(item.Notes) %>
</td>
</tr>
<% } %>
</table>
Run Code Online (Sandbox Code Playgroud)
这是做这样事情的唯一方法吗?如果有人知道一种更好的方式,不涉及将所有记录一次性加载到页面,那么请链接到示例.
Bha*_*rat 10
查看DataTables @ DataTables这将允许您分页结果并通过简单的设置进行查询.它适用于ajax和json数据.看看样品.希望这会帮助你.
尝试以下扩展方法(从头顶):
static class OrderByExtender
{
public static IOrderedEnumerable<T> OrderBy<T>(this IEnumerable<T> collection, string key, string direction)
{
LambdaExpression sortLambda = BuildLambda<T>(key);
if(direction.ToUpper() == "ASC")
return collection.OrderBy((Func<T, object>)sortLambda.Compile());
else
return collection.OrderByDescending((Func<T, object>)sortLambda.Compile());
}
public static IOrderedEnumerable<T> ThenBy<T>(this IOrderedEnumerable<T> collection, string key, string direction)
{
LambdaExpression sortLambda = BuildLambda<T>(key);
if (direction.ToUpper() == "ASC")
return collection.ThenBy((Func<T, object>)sortLambda.Compile());
else
return collection.ThenByDescending((Func<T, object>)sortLambda.Compile());
}
private static LambdaExpression BuildLambda<T>(string key)
{
ParameterExpression TParameterExpression = Expression.Parameter(typeof(T), "p");
LambdaExpression sortLambda = Expression.Lambda(Expression.Convert(Expression.Property(TParameterExpression, key), typeof(object)), TParameterExpression);
return sortLambda;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
var products = Session["Model"] as IEnumerable<Product>() ?? _service.GetAll();
return products.OrderBy("Name", "ASC").ThenBy("Price", "DESC");
Run Code Online (Sandbox Code Playgroud)
假设您一次只使用1个orderby条件,您可以使用:
var products = Session["Model"] as IEnumerable<Product>();
var sortDirection = Session["Direction"] as string ?? "DESC";
Session["Direction"] = sortDirection == "DESC" ? "ASC" : "DESC";
sortDirection = Session["Direction"] as string;
return products.OrderBy(parameter, sortDirection);
Run Code Online (Sandbox Code Playgroud)
我更喜欢这里描述的方法: http://www.c-sharpcorner.com/UploadFile/camurphy/csharpLists03302006170209PM/csharpLists.aspx
所以对于前:
var products = new List<Products>();
products = ProductRepository.GetAll();
// Sort Results
products.Sort(
delegate(Products p1, Products p2) {
return p1.Name.CompareTo(p2.Name);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18255 次 |
| 最近记录: |