※ datagrid的基本属性和方法 
※ datagrid分页在前后台的实现

最终效果: 
  与视图显示对应的view model

 
public class Book

        public string ItemId { get; set; }
        public string ProductId { get; set; }
        public decimal ListPrice { get; set; }
        public decimal UnitCost { get; set; }
        public string Attr1 { get; set; }
        public Int16 Status { get; set; }
  模拟一个从数据库拿数据,并考虑分页的服务层方法

□ 与分页有关的类

public class PageParam
        public int PageSize { get; set; } 
        public int PageIndex { get; set; } 
在实际项目中,可以把以上作为一个基类,把各个领域的各种搜索条件封装成继承PageParam的子类。

□ 分页服务层方法

using System.Linq;
using DataGridInMvc.Models;
using System.Collections.Generic;
using Microsoft.Ajax.Utilities;

namespace DataGridInMvc.Helper
{
    public class BookService
    {
        public IEnumerable<Book> LoadPageBookData(PageParam param, out int total)
        {
            //创建Book的集合
            var books = new List<Book>();
            for (int i = 0; i < 30; i++)
            {
                books.Add(new Book()
                {
                    ItemId = "EST-" + i,
                    ProductId = "AV-SB-" + i,
                    ListPrice = (i + 5) * 10m,
                    UnitCost = (i + 2) * 10m,
                    Attr1 = "Attr" + i,
                    Status = (short)0
                });
            }

total = books.Count();
            var result = books.OrderBy(b => b.ItemId)
                .Skip(param.PageSize*(param.PageIndex - 1))
                .Take(param.PageSize);
            return result;
        }
    }

Controller有显示页面和响应前台datagrid请求的Action方法

using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.Mvc;
using DataGridInMvc.Helper;
using DataGridInMvc.Models;

namespace DataGridInMvc.Controllers
{
    public class HomeController : Controller
    {

public ActionResult Index()
{
return View();
        }

public ActionResult GetData()
        {
            //接收datagrid传来的参数
            int pageIndex = int.Parse(Request["page"]);
            int pageSize = int.Parse(Request["rows"]);

//构建得到分页数据方法所需的参数
            var temp = new PageParam()
            {
                PageIndex = pageIndex,
                PageSize = pageSize
            };

//分页数据方法的输出参数
            int totalNum = 0;

var service = new BookService();
            var books = service.LoadPageBookData(temp, out totalNum);

var result = from book in books
                select new {book.ItemId, book.ProductId, book.ListPrice, book.UnitCost, book.Status, book.Attr1};

//total,rows是前台datagrid所需要的
            var jsonResult = new {total = totalNum, rows = result};

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

□ 这里需要把json对象序列化成string,使用Newtonsoft组件是不错的选择。把序列化和反序列化封装成类。

using System;
using Newtonsoft.Json;

namespace DataGridInMvc.Helper
{
public static class JsonSerializeHelper
{
/// <summary>
/// 把object对象序列化成json字符串
/// </summary>
/// <param name="obj">序列话的实例</param>
/// <returns>序列化json字符串</returns>
public static string SerializeToJson(object obj)
{
return JsonConvert.SerializeObject(obj);
}

/// <summary>
/// 把json字符串反序列化成Object对象
/// </summary>
/// <param name="json">json字符串</param>
/// <returns>对象实例</returns>
public static Object DeserializeFromJson(string json)
{
return JsonConvert.DeserializeObject(json);
}

/// <summary>
/// 把json字符串反序列化成泛型T
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="json">json字符串</param>
/// <returns>泛型T</returns>
public static T DeserializeFromJson<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
}
}

视图

<link href="~/Content/themes/default/easyui.css" rel="stylesheet" /> <link href="~/Content/themes/icon.css" rel="stylesheet" /> <script src="~/Scripts/jquery-1.10.2.js"></script> <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() { $('#tt').datagrid({ url: 'Home/GetData', width: 500, height: 350, title: 'Book列表', iconCls: 'icon-save', fitColumns: true, rownumbers: true, //是否加行号 pagination: true, //是否显式分页 pageSize: 15, //页容量,必须和pageList对应起来,否则会报错 pageNumber: 2, //默认显示第几页 pageList: [15, 30, 45],//分页中下拉选项的数值 columns: [[ //book.ItemId, book.ProductId, book.ListPrice, book.UnitCost, book.Status, book.Attr1 { field: 'ItemId', title: '主键', sortable: true }, { field: 'ProductId', title: '产品编号' }, { field: 'Attr1', title: '属性' }, { field: 'UnitCost', title: '成本价' }, { field: 'ListPrice', title: '零售价' }, { field: 'Status', title: '状态' }, ]] }); } function changeP() { var dg = $('#tt'); dg.datagrid('loadData', []); //重新加载数据 dg.datagrid({ pagePosition: $('#p-pos').val() }); //分页位置 dg.datagrid('getPager').pagination({ //分页样式、内容 layout: ['list', 'sep', 'first', 'prev', 'sep', $('#p-style').val(), 'sep', 'next', 'last', 'sep', 'refresh'] }); } </script> <p> 选择分页显示位置: <select id="p-pos" onchange="changeP()"> <option>bottom</option> <option>top</option> <option>both</option> </select> 选择分页显示样式 <select id="p-style" onchange="changeP()"> <option>manual</option> <option>links</option> </select> </p> <table id="tt"> </table>

※ datagrid的基本属性和方法 
※ datagrid分页在前后台的实现

最终效果: 
  与视图显示对应的view model

 
public class Book

        public string ItemId { get; set; }
        public string ProductId { get; set; }
        public decimal ListPrice { get; set; }
        public decimal UnitCost { get; set; }
        public string Attr1 { get; set; }
        public Int16 Status { get; set; }
  模拟一个从数据库拿数据,并考虑分页的服务层方法

□ 与分页有关的类

public class PageParam
        public int PageSize { get; set; } 
        public int PageIndex { get; set; } 
在实际项目中,可以把以上作为一个基类,把各个领域的各种搜索条件封装成继承PageParam的子类。

□ 分页服务层方法

using System.Linq;
using DataGridInMvc.Models;
using System.Collections.Generic;
using Microsoft.Ajax.Utilities;

namespace DataGridInMvc.Helper
{
    public class BookService
    {
        public IEnumerable<Book> LoadPageBookData(PageParam param, out int total)
        {
            //创建Book的集合
            var books = new List<Book>();
            for (int i = 0; i < 30; i++)
            {
                books.Add(new Book()
                {
                    ItemId = "EST-" + i,
                    ProductId = "AV-SB-" + i,
                    ListPrice = (i + 5) * 10m,
                    UnitCost = (i + 2) * 10m,
                    Attr1 = "Attr" + i,
                    Status = (short)0
                });
            }

total = books.Count();
            var result = books.OrderBy(b => b.ItemId)
                .Skip(param.PageSize*(param.PageIndex - 1))
                .Take(param.PageSize);
            return result;
        }
    }

Controller有显示页面和响应前台datagrid请求的Action方法

using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.Mvc;
using DataGridInMvc.Helper;
using DataGridInMvc.Models;

namespace DataGridInMvc.Controllers
{
    public class HomeController : Controller
    {

public ActionResult Index()
{
return View();
        }

public ActionResult GetData()
        {
            //接收datagrid传来的参数
            int pageIndex = int.Parse(Request["page"]);
            int pageSize = int.Parse(Request["rows"]);

//构建得到分页数据方法所需的参数
            var temp = new PageParam()
            {
                PageIndex = pageIndex,
                PageSize = pageSize
            };

//分页数据方法的输出参数
            int totalNum = 0;

var service = new BookService();
            var books = service.LoadPageBookData(temp, out totalNum);

var result = from book in books
                select new {book.ItemId, book.ProductId, book.ListPrice, book.UnitCost, book.Status, book.Attr1};

//total,rows是前台datagrid所需要的
            var jsonResult = new {total = totalNum, rows = result};

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

□ 这里需要把json对象序列化成string,使用Newtonsoft组件是不错的选择。把序列化和反序列化封装成类。

using System;
using Newtonsoft.Json;

namespace DataGridInMvc.Helper
{
public static class JsonSerializeHelper
{
/// <summary>
/// 把object对象序列化成json字符串
/// </summary>
/// <param name="obj">序列话的实例</param>
/// <returns>序列化json字符串</returns>
public static string SerializeToJson(object obj)
{
return JsonConvert.SerializeObject(obj);
}

/// <summary>
/// 把json字符串反序列化成Object对象
/// </summary>
/// <param name="json">json字符串</param>
/// <returns>对象实例</returns>
public static Object DeserializeFromJson(string json)
{
return JsonConvert.DeserializeObject(json);
}

/// <summary>
/// 把json字符串反序列化成泛型T
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="json">json字符串</param>
/// <returns>泛型T</returns>
public static T DeserializeFromJson<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
}
}

视图

<link href="~/Content/themes/default/easyui.css" rel="stylesheet" /> <link href="~/Content/themes/icon.css" rel="stylesheet" /> <script src="~/Scripts/jquery-1.10.2.js"></script> <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() { $('#tt').datagrid({ url: 'Home/GetData', width: 500, height: 350, title: 'Book列表', iconCls: 'icon-save', fitColumns: true, rownumbers: true, //是否加行号 pagination: true, //是否显式分页 pageSize: 15, //页容量,必须和pageList对应起来,否则会报错 pageNumber: 2, //默认显示第几页 pageList: [15, 30, 45],//分页中下拉选项的数值 columns: [[ //book.ItemId, book.ProductId, book.ListPrice, book.UnitCost, book.Status, book.Attr1 { field: 'ItemId', title: '主键', sortable: true }, { field: 'ProductId', title: '产品编号' }, { field: 'Attr1', title: '属性' }, { field: 'UnitCost', title: '成本价' }, { field: 'ListPrice', title: '零售价' }, { field: 'Status', title: '状态' }, ]] }); } function changeP() { var dg = $('#tt'); dg.datagrid('loadData', []); //重新加载数据 dg.datagrid({ pagePosition: $('#p-pos').val() }); //分页位置 dg.datagrid('getPager').pagination({ //分页样式、内容 layout: ['list', 'sep', 'first', 'prev', 'sep', $('#p-style').val(), 'sep', 'next', 'last', 'sep', 'refresh'] }); } </script> <p> 选择分页显示位置: <select id="p-pos" onchange="changeP()"> <option>bottom</option> <option>top</option> <option>both</option> </select> 选择分页显示样式 <select id="p-style" onchange="changeP()"> <option>manual</option> <option>links</option> </select> </p> <table id="tt"> </table>

jQuery EasyUI DataGrid在MVC中的运用-基本属性并实现分页的更多相关文章

  1. JQuery easyUi datagrid 中 editor 动态设置最大值最小值

    前言 近来项目中使用到 easyui 来进行页面设计,感觉挺方便的,但是网上除了api外,其他有价值的资料比较少,故在此分享一点经验,供大家参考.   问题 JQuery easyUi datagri ...

  2. JQuery easyUi datagrid 中 自定义editor作为列表操作按钮列

    转自   http://blog.csdn.net/tianlincao/article/details/7494467 前言 JQuery easyUi datagrid 中 使用datagrid生 ...

  3. datagrid在MVC中的运用01-基本属性并实现分页

    本文体验jQuery EasyUI的datagrid在MVC中的应用.主要涉及到: ※ datagrid的基本属性和方法 ※ datagrid分页在前后台的实现 最终效果: 与视图显示对应的view ...

  4. datagrid在MVC中的运用05-加入时间搜索条件,枚举填充下拉框

    本文主要来体验在搜索区域增加更多的搜索条件,主要包括: ※ 使用jQuery ui的datepicker显示时间,设置显示格式.样式. ※ 设置jQuery ui的onClose事件,使开始和结束时间 ...

  5. jQuery EasyUI DataGrid Checkbox 数据设定与取值

    纯粹做个记录,以免日后忘记该怎么设定. 这一篇将会说明两种使用 jQuery EasyUI DataGrid 的 Checkbox 设定方式,以及在既有数据下将 checked 为 true 的该笔数 ...

  6. jquery easyui datagrid使用参考

    jquery easyui datagrid使用参考   创建datagrid 在页面上添加一个div或table标签,然后用jquery获取这个标签,并初始化一个datagrid.代码如下: 页面上 ...

  7. Jquery easyui datagrid 导出Excel

    From:http://www.cnblogs.com/weiqt/articles/4022399.html datagrid的扩展方法,用于将当前的数据生成excel需要的内容. 1 <sc ...

  8. 扩展jquery easyui datagrid编辑单元格

    扩展jquery easyui datagrid编辑单元格 1.随便聊聊 这段时间由于工作上的业务需求,对jquery easyui比较感兴趣,根据比较浅薄的js知识,对jquery easyui中的 ...

  9. jQuery EasyUI datagrid列名包含特殊字符会导致表格错位

    首先申明:本文所述的Bug存在于1.3.3以及更高版本中,其它低版本,本人未测试,太老的版本不想去折腾了. 洒家在写前端的SQL执行工具时,表格用了 jQuery EasyUI datagrid,因为 ...

随机推荐

  1. RxJava 1.x 理解-2

    给RxJava 加入线程控制 -- Scheduler 在 RxJava 1.x 理解-1 中,我们说到了RxJava的简单用法,但是这还远远不够,因为这简单用法是在同一个线程中使用的.比如我们需要在 ...

  2. 扑克模拟,牌型判断java版

    Card类 package com.company; public class Card { private String color; private Integer value; public S ...

  3. Nginx流量带宽请求状态统计(ngx_req_status)

    介绍           ngx_req_status 用来展示 nginx 请求状态信息,类似于 apache 的 status, nginx 自带的模块只能显示连接数等等 信息,我们并不能知道到底 ...

  4. HTTPS.SYS怎样使用HTTPS

    HTTPS.SYS怎样使用HTTPS 参考了MORMOT的官方文档:http://blog.synopse.info/post/2013/09/04/HTTPS-communication-in-mO ...

  5. C#中使用 HttpWebRequest 向网站提交数据

    HttpWebRequest 是 .NET 基类库中的一个类,在命名空间 System.Net 里,用来使用户通过 HTTP 协议和服务器交互. HttpWebRequest 对 HTTP 协议进行了 ...

  6. iOS:Objective-c的MD5/SHA1加密算法的实现

    介绍: Objective-c实现MD5和SHA1算法相对还是比较简单的,可以直接调用系统的C/C++共享库来实现调用MD5即Message Digest Algorithm 5(信息-摘要算法 5) ...

  7. ISP图像调试工程师——边缘增强(熟悉图像预处理和后处理技术)

    http://blog.csdn.net/u013033431/article/details/50907907 http://dsqiu.iteye.com/blog/1638589 概念: 图像增 ...

  8. html DOM 的继承关系

    零散的知识聚合在一起,就会形成力量,就有了生命力. 如各种语言的开发框架, 都是右各个碎片化的功能聚合在一起,构成有机地整体,便有了强大的力量.will be powerful! 如: jquery ...

  9. phpunit与xdebug的使用

    基本说明: 1.xdebug是程序猿在调试程序过程中使用的bug调试暴露工具 windows下安装: 1)下载php对应的dll文件,下载地址:https://xdebug.org/download. ...

  10. subscription group permisson