本文体验datagrid的排序。

□ 思路

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

相关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集合,并返回一个输出总记录数

 

在进行分类的时候,用到了针对 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>
}

最终效果:

作者:darrenji

【第十篇】easyui-datagrid排序 (转)的更多相关文章

  1. easyUI datagrid 排序

    easyUI datagrid 排序 1.首先设置datagrid属性sortName 2.设置sortOrder 3.设置remoteSort(注:此属性默认为true,如果如果是对本地数据排序必须 ...

  2. easyui datagrid client搜索、分页、排序

    easyui datagrid的排序默认是server端排序.能够用sorter实现client排序[2].client分页可用filter实现[3].client搜索相同能够用filter实现. 不 ...

  3. easyui datagrid导出excel

    [第十四篇]easyui datagrid导出excel   <a class="btn btn-app" onclick="exportExcel()" ...

  4. easyui datagrid的列编辑

    [第十五篇]easyui datagrid的列编辑,同时插入两张表的数据进去   看图说话. 需求:插入两张表,上面的表单是第一张表的内容,下面的两个表格是第二张详情表的内容,跟第一张表的id关联 第 ...

  5. 反射实体自动生成EasyUi DataGrid模板 第二版--附项目源码

    之前写过一篇文章,地址 http://www.cnblogs.com/Bond/p/3469798.html   大概说了下怎么通过反射来自动生成对应EasyUi datagrid的模板,然后贴了很多 ...

  6. DataTables VS EasyUI DataGrid 基础应用 转

    DataTables中文网推出了 第一篇 关于DataTables和其他表格插件比较后,为了把让这个比较更有意义,更能帮助到大家,DataTables中文网 做了问卷调查,根据小伙伴们的填写我归纳了一 ...

  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(8)-MVC与EasyUI DataGrid 分页

    系列目录 前言 为了符合后面更新后的重构系统,文章于2016-11-1日重写 EasyUI Datagrid在加载的时候会提交一些分页的信息到后台,我们需要根据这些信息来进行数据分页再次返回到前台 实 ...

  8. jquery easyui datagrid翻页后再查询始终从第一页开始

    在查询之前将datagrid的属性pageNumber重新设置为1 var opts = grid.datagrid('options'); opts.pageNumber = 1; easyui d ...

  9. EasyUI datagrid优化

    easyui datagrid 在IE上加载速度慢, 150行数据就无法忍受了. firefox加载速度还可以. jquery easyui datagrid使用参考 http://www.cnblo ...

随机推荐

  1. Spring.Net 依赖注入

    一.Spring.Net概念 编程模型(Ioc,DI方式) IoC:控制反转 原来创建对象的权利由程序来控制就是new实例,IoC就是改由容器来创建,相当于一个工厂, DI:依赖注入 没有IoC就没有 ...

  2. python 闭包,装饰器,random,os,sys,shutil,shelve,ConfigParser,hashlib模块

    闭包 def make_arerage(): l1 = [] def average(price): l1.append(price) total = sum(l1) return total/len ...

  3. javaScript基础-04 对象

    一.对象的基本概念 对象是JS的基本数据类型,对象是一种复合值,它将很多值(原始值或者对象)聚合在一起,可通过名字访问这些值,对象也可看做是属性的无序集合,每个属性都是一个名/值对.对象不仅仅是字符串 ...

  4. intellij idea 2019 安装使用教程

    一.安装 idea   2019.2   链接:https://pan.baidu.com/s/1acx_P23W463it9PGAYUIBw 提取码:4bky 双击运行idea.exe 点击Next ...

  5. Elasticsearch由浅入深(一)

    什么是Elasticsearch 什么是搜索 百度:我们比如说想找寻任何的信息的时候,就会上百度去搜索一下,比如说找一部自己喜欢的电影,或者说找一本喜欢的书,或者找一条感兴趣的新闻(提到搜索的第一印象 ...

  6. 移动开发-UI设计

        UI:手机的用户界面 UI物理版:手机实际的屏幕像素 UI设计版:我们截屏的手机界面在ps中去量,发现的尺寸 UI放大版:手机的尺寸等比放大1.5倍得出的分辨率   响应式布局 原由:窗体缩小 ...

  7. HBase 系列(六)——HBase Java API 的基本使用

    一.简述 截至到目前 (2019.04),HBase 有两个主要的版本,分别是 1.x 和 2.x ,两个版本的 Java API 有所不同,1.x 中某些方法在 2.x 中被标识为 @depreca ...

  8. python实例:自动保存百度盘资源到百度盘中

    本实例的实现逻辑是,应用selenium UI自动化登录百度盘,读取存储百度分享地址和提取码的txt文档,打开百度盘分享地址,填入提取码,然后保存到指定的目录中 全部代码如下: # -*-coding ...

  9. Linux shell 获得字符串所在行数及位置

    shell 获得字符串所在行数及位置 01 获取字符串所在的行数 方式一:用grep -n [root@root]# cat test apple bit create delect exe flow ...

  10. 王某人从0开始学习lorawan的笔记_0

    最近老板想做lorawan的项目,交给我了,我也应承下来了,但是!!!我TM连lorawan是啥子我都不知道啊啊啊啊啊! 真希望我女朋友可以看穿我的倔强,给我1千万,让我专心当舔狗,等等,我的女朋友? ...