datagrid在MVC中的运用09-实现排序

本文体验datagrid的排序。

□ 思路

当点击datagrid的标题,视图传递给Controller的Form Data类似这样:page=1&rows=10&sort=CustomerID&order=asc。为了应对变化,把关于分页的封装成基类,其他关于排序或搜索的封装成继承该基类的子类。再把这些子类对象实例传递给服务层方法。

datagrid参数

  相关Model

展开    //显示表相关
    public class Customer
    {
        public int CustomerID { get; set; }
        public string CompanyName { get; set; }
        public string ContactName { get; set; }
        public string ContactTitle { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string Region { get; set; }
        public string Country { get; set; }
        public string Phone { get; set; }
        public string Fax { get; set; } 
    }

    //关于分页封装成基类
    public class PageParam
    {
        public int PageSize { get; set; }
        public int PageIndex { get; set; } 
    }    

    //关于搜索或排序封装成子类
    public class CustomerParam : PageParam
    {
        public string SortProperty { get; set; }
        public bool Order { get; set; }
    }    

  服务层根据CustomerParam返回Customer集合,并返回一个输出总记录数

展开using System;
using System.Collections;
using System.Linq;
using DataGridInMVC2.Models;
using System.Collections.Generic;

namespace DataGridInMVC2.Helpers
{
    public class Service
    {
        //获取Customer列表
        public IEnumerable<Customer> LoadPageCustomers(CustomerParam param, out int total)
        {
            var customers = InitializeCustomers();
            total = customers.Count();

            //考虑排序
            //CustomerID,CompanyName,ContactName,ContactTitle,Address,Fax,Phone,City
            switch (param.SortProperty)
            {
                case "CustomerID":
                    return customers.OrderByWithDirection(c => c.CustomerID, param.Order)
                    .Skip(param.PageSize * (param.PageIndex - 1))
                    .Take(param.PageSize);
                    break;
                    
                case "CompanyName":
                    return customers.OrderByWithDirection(c => c.CompanyName, param.Order)
                    .Skip(param.PageSize * (param.PageIndex - 1))
                    .Take(param.PageSize);
                    break;

                case "ContactName":
                    return customers.OrderByWithDirection(c => c.ContactName, param.Order)
                    .Skip(param.PageSize * (param.PageIndex - 1))
                    .Take(param.PageSize);
                    break;

                case "ContactTitle":
                    return customers.OrderByWithDirection(c => c.ContactTitle, param.Order)
                    .Skip(param.PageSize * (param.PageIndex - 1))
                    .Take(param.PageSize);
                    break;

                case "Address":
                    return customers.OrderByWithDirection(c => c.Address, param.Order)
                    .Skip(param.PageSize * (param.PageIndex - 1))
                    .Take(param.PageSize);
                    break;

                case "Fax":
                    return customers.OrderByWithDirection(c => c.Fax, param.Order)
                    .Skip(param.PageSize * (param.PageIndex - 1))
                    .Take(param.PageSize);
                    break;

                case "Phone":
                    return customers.OrderByWithDirection(c => c.Phone, param.Order)
                    .Skip(param.PageSize * (param.PageIndex - 1))
                    .Take(param.PageSize);
                    break;

                case "City":
                    return customers.OrderByWithDirection(c => c.City, param.Order)
                    .Skip(param.PageSize * (param.PageIndex - 1))
                    .Take(param.PageSize);
                    break;

                default:
                    return customers.OrderByWithDirection(c => c.CustomerID, param.Order)
                    .Skip(param.PageSize * (param.PageIndex - 1))
                    .Take(param.PageSize);
                    break;
            }
            
        }


        //初始化Customer
        public IEnumerable<Customer> InitializeCustomers()
        {
            var customers = new List<Customer>();
            for (int i = 0; i < 35; i++)
            {
                customers.Add(new Customer()
                {
                    CustomerID = i + 1,
                    CompanyName = "Company" + Convert.ToString(i + 1),
                    ContactName = "ContactName" + Convert.ToString(i + 1),
                    ContactTitle = "ContactTitle" + Convert.ToString(i + 1),
                    Address = "Address" + Convert.ToString(i + 1),
                    City = "City" + Convert.ToString(i + 1),
                    Country = "China",
                    Fax = "010-878789" + Convert.ToString(i + 1),
                    Phone = "010-989876" + Convert.ToString(i + 1),
                    Region = "Qingdao"
                });
            }
            return customers;
        }
    }
} 

在进行分类的时候,用到了针对 IEnumerable<Customer>扩展方法OrderByWithDirection,如下:

using System.Linq;
 
namespace DataGridInMVC2.Helpers
{
    public static class SortExtension
    {
 
        public static IOrderedEnumerable<TSource> OrderByWithDirection<TSource, TKey>(
            this IEnumerable<TSource> source,
            System.Func<TSource, TKey> keySelector,
            bool descending)
        {
            return descending ? source.OrderByDescending(keySelector) : source.OrderBy(keySelector);
        }
    }
}
 

 

  CustomerController

展开using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataGridInMVC2.Helpers;
using DataGridInMVC2.Models;

namespace DataGridInMVC2.Controllers
{
    public class CustomerController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetData()
        {
            //接收datagrid传来的参数
            //page=1&rows=10&sort=CustomerID&order=asc
            int pageIndex = int.Parse(Request["page"]);
            int pageSize = int.Parse(Request["rows"]);
            string sortProperty = Request["sort"];
            bool order = Request["order"] == "asc" ? false : true;
            

            //构建服务类方法所需要的参数实例
            var temp = new CustomerParam()
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                SortProperty = sortProperty,
                Order = order
            };

            var service = new Service();
            int totalNum = 0;
            var customers = service.LoadPageCustomers(temp, out totalNum);

            var result = from customer in customers
                select new
                {
                    customer.CustomerID,
                    customer.CompanyName,
                    customer.ContactName,
                    customer.ContactTitle,
                    customer.Country,
                    customer.Region,
                    customer.Address,
                    customer.Fax,
                    customer.Phone,
                    customer.City
                };

            var jsonResult = new {total = totalNum, rows = result};

            //序列化成json字符串
            string str = JsonSerializeHelper.SerializeToJson(jsonResult);
            return Content(str);
        }
    }
}

 

  Customer/Index 视图

展开@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<link href="~/Content/themes/default/easyui.css" rel="stylesheet" />
<link href="~/Content/themes/icon.css" rel="stylesheet" />

<table id="tt"></table>

@section scripts
{
    <script src="~/Scripts/jquery.easyui.min.js"></script>
    <script src="~/Scripts/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript">
        $(function () {
            initData();
        });

        function initData(params) {
            $('#tt').datagrid({
                url: '@Url.Action("GetData","Customer")',
                width: 730,
                height: 400,
                title: 'Customer列表',
                fitColumns: true,
                rownumbers: true, //是否加行号
                pagination: true, //是否显式分页
                pageSize: 15, //页容量,必须和pageList对应起来,否则会报错
                pageNumber: 2, //默认显示第几页
                pageList: [15, 30, 45],//分页中下拉选项的数值
                columns: [[
                    //CustomerID,CompanyName,ContactName,ContactTitle,Country,Region,Address,Fax,Phone,City
                    { field: 'CustomerID', title: '编号',sortable: true },
                    { field: 'CompanyName', title: '客户名称', sortable: true },
                    { field: 'ContactName', title: '联系人名称', sortable: true },
                    { field: 'ContactTitle', title: '职位', sortable: true },
                    { field: 'Address', title: '地址', sortable: true },
                    { field: 'City', title: '城市名称', sortable: true },
                    { field: 'Region', title: '区域' },
                    { field: 'Country', title: '国家' },
                    { field: 'Phone', title: '电话', sortable: true },
                    { field: 'Fax', title: '传真', sortable: true }
                ]],
                queryParams: params, //搜索json对象
                sortName: 'CustomerID', //初始排序字段
                sortOrder: 'asc' //初始排序方式
            });
        }
    </script>
}

最终效果:
sorting
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<div style=";text-align:center;;height:auto;" class="datagrid-cell datagrid-cell-c1-checkId">已通过</div></td><td field="button"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-button"><a href="#" style="color: red" onclick="fileManager(0)">图片管理</a></div></td><td field="truckNo"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-truckNo">辽PD6885</div></td><td field="truckCardColor"><div style=";text-align:center;;height:auto;" class="datagrid-cell datagrid-cell-c1-truckCardColor">黄牌</div></td><td field="vtNam"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-vtNam">秦皇岛九福物流有限公司</div></td><td field="driverNam"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-driverNam">叶红建</div></td><td field="linkTel"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-linkTel">13842929049</div></td><td field="workCompanyCod"><div style=";text-align:center;;height:auto;" class="datagrid-cell datagrid-cell-c1-workCompanyCod">金海粮油</div></td><td field="cargoNam" style="display:none;"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-cargoNam"></div></td><td field="consignCod" style="display:none;"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-consignCod"></div></td><td field="planDte"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-planDte">2023-05-01</div></td><td field="validTyp"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-validTyp">当天有效</div></td><td field="ifEnd"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-ifEnd">x</div></td><td field="individualId" style="display:none;"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-individualId">0</div></td><td field="rejectReason"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-rejectReason"></div></td><td field="checkNam"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-checkNam">jhly</div></td><td field="checkTim"><div style=";height:auto;" class="datagrid-cell datagrid-cell-c1-checkTim">2023-04-29 21:09</div></td>以上代码为网页源码,帮我写一段python程序从以上代码找出drivernam和checkTim并保存数据库
06-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值