※ 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. flask 开发环境搭建

    window下: 1)安装python 2)安装pip 3)使用pip install flask 如果成功安装使用pip list 既可以查看到flask的版本 ubuntu下的环境搭建 同样地使用 ...

  2. 【开源类库学习】MBProgressHUD(提示框)

    新博客: http://www.liuchendi.com MBProgressHUD是一个开源类库,实现了各种样式的提示框, 下载地址:https://github.com/jdg/MBProgre ...

  3. android 电话薄先10位匹配,若是无法匹配,则换成7位匹配

    案例 1: 假设您保存的有:A:04165191666. B:5191666.  来电号码是:04165191666   由于是7位匹配,所以A和B都能够匹配到.可是最佳匹配还是A,最后显示A: 来电 ...

  4. s3c6410 Linux 驱动开发环境搭建

    s3c6410 Linux 驱动开发环境搭建 -- 既然你是做Linux开发的,你还用虚拟机? 非常多人都在win下做开发,于是SD_writer.exe之类的烧写工具"大行其道" ...

  5. sqls

    ALTER TABLE `shh_data`.`topic_floor` ADD COLUMN `updated_date` DATETIME NULL AFTER `publish_date`,AD ...

  6. RocketMQ性能压测分析(转)

    原创文章,转载请注明出处:http://jameswxx.iteye.com/blog/2093785 一   机器部署 1.1  机器组成 1台nameserver 1台broker  异步刷盘 2 ...

  7. mysql 5.7.13 安装配置方法(linux)-后期部分运维

    mysql 5.7.13 安装配置方法图文教程(linux) 学习了:https://www.cnblogs.com/zhao1949/p/5947938.html /usr/local/mysql是 ...

  8. junit4单元測试总结

    junit4单元測试总结 本文开发环境为myeclipse10.7 1.  准备工作 1.1. 选择须要单元測试的文件 创建mavenproject.右击须要单元測试的文件,选择New->oth ...

  9. Mac电脑下配置maven环境变量

    Mac电脑下配置maven环境变量 打开终端,使用 touch 命令创建 .bash_profile 文件 touch .bash_profile 编辑刚刚创建的文件 .bash_profile vi ...

  10. Win7如何查看自己得Win7版本号

    如何查看Windows 7详细系统版本号? --Windows 7系统知识100问之七十一 责任编辑:姜惠田作者:IT168 老姜   2009-08-05 前言:微软新一代操作系统Windows 7 ...