简体   繁体   English

如何使用ASP.NET MVC中的Web网格通过表标题的onclick对表的顺序进行排序?

[英]How to sort the order of the table by the onclick of the heading of the table using web grid in ASP.NET MVC?

I would like to sort the order of the table by the onclick on the heading of the column (Link). 我想按列标题(链接)上的onclick排序表的顺序。 The code is as below, 代码如下

enter image description here 在此处输入图片说明

Controller Code 控制器代码

public ActionResult StockStatus(int? id)
        {
            List data = new List();
            if (id.HasValue)
            {
                data = db.Items.Where(d => d.InStock.Value  d.ItemName).ToList();
                ViewBag.LesserThan = id;
            }
            else
            {
                data = db.Items.OrderBy(d => d.ItemName).ToList();
            }

            return View(data);
        }
        public ActionResult StockStatus(int id)
        {
            var sold = db.Sales.Include(d => d.Items).Include(d => d.Customers).Where(d => d.ItemId == id).OrderByDescending(d => d.InvoiceId).ThenBy(d => d.Items.ItemName).ToList();
            return View(sold);
        }


Can any one of you please help me in this!! 你们任何人都可以在这方面帮助我!

You have next options: 您还有下一个选择:

  1. Use client-side (Javascript) filtering\\sorting. 使用客户端(JavaScript)过滤\\排序。 In this case use some controls like jqgrid, jgrid, etc which has this functionality(preferable) or implement your own. 在这种情况下,请使用具有此功能(首选)的jqgrid,jgrid等控件或实现您自己的控件。
  2. Server side. 服务器端。 Extend your controllers, to take additional string "order_by" and string "order_field" parameters and refactor your db queries considering this parameters. 扩展控制器,以获取其他字符串“ order_by”和字符串“ order_field”参数,并考虑该参数来重构数据库查询。 Add ajax calls on clicking headers for calling actions in controllers.( If you have webapi in your project you can just simply replace grid with new one, returned in ajax response) 在单击标题上添加ajax调用,以在控制器中调用操作。(如果您的项目中有webapi,则只需将网格替换为新的,在ajax响应中返回)

Hope this helps! 希望这可以帮助!

you can call action method using action link below 您可以使用下面的操作链接调用操作方法

grid.Column(
  header: "Qty In stock",
  columnName: "qty",
  format: (item) => new HtmlString(Html.ActionLink(Html.ActionLink("Sort", "Sortstock", new { Id = item.idAddress,sort="fieldname"})
  ) 

if u get the column name inside the action method you can sort the list 如果您在action方法中获得列名,则可以对列表进行排序

   public ActionResult Sortstock(string sort= "", string sortdir="")
   {
       List<Item> stocks = Student.getStock();
       IQueryable<Item> stud = SortIQueryable<Item>(Student.getStock().AsQueryable(), sort, sortdir);

       return View(stud);
   }


    public static IQueryable<T> SortIQueryable<T>(IQueryable<T> data, string fieldName, string sortOrder)
    {
        if (string.IsNullOrWhiteSpace(fieldName)) return data;
        if (string.IsNullOrWhiteSpace(sortOrder)) return data;

        var param = Expression.Parameter(typeof(T), "i");
        Expression conversion = Expression.Convert(Expression.Property(param, fieldName), typeof(object));
        var mySortExpression = Expression.Lambda<Func<T, object>>(conversion, param);

        return (sortOrder == "desc") ? data.OrderByDescending(mySortExpression) : data.OrderBy(mySortExpression);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM