MVC4 Jqgrid设计与实现
项目本来使用的是webgrid。后台弃用改成Jqgrid插件。
首先介绍一下webgrid的用法:webgrid是mvc中HtmlHelper自带的。首先创建viewmodel用于数据的绑定,然后在页面中进行绑定一些参数的设置即可
具体如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | @model Models.SearchPageViewModel @{ WebGrid grid = new WebGrid( null , rowsPerPage: Model.PagingInfo.ItemsPerPage, selectionFieldName: "selectedRow" , ajaxUpdateContainerId: "gridContent" , canSort: false ); grid.Bind(Model.modelList, autoSortAndPage: false , rowCount: Model.PagingInfo.TotalItems); } @ if (grid.TotalRowCount > 0) { <div id= "gridContent" > @grid.GetHtml( tableStyle: "GridViewTable" , headerStyle: "HeaderStyle" , footerStyle: "FooterStyle" , rowStyle: "RowStyle" , alternatingRowStyle: "AltRowStyle" , selectedRowStyle: "SelectedRowStyle" , mode: WebGridPagerModes.All, numericLinksCount: 10, firstText: "首页" , lastText: "尾页" , previousText: "<" , nextText: ">" , columns: grid.Columns( grid.Column( "RowNum" , "序号" , format: (item) => item.RowNum), grid.Column( "XZQDM" , "行政区代码" , format: (item) => item.XZQDM, style: null , canSort: false ), grid.Column( "XZQMC" , "行政区名称" , format: (item) => item.XZQMC, canSort: false ), grid.Column( "UPTIME" , "上传时间" , format: (item) => item.UPTIME, canSort: false ), grid.Column(header: "选择" , format: @<text><input class = "check-box" id= "assignChkBx" name= "assignChkBx" type= "checkbox" value= "@item.PZWH" /></text>) )) </div> |
Jqgrid用法大同小异:
需要引用<script type="text/javascript" src="~/scripts/locales/grid.locale-cn.js"></script>
<script type="text/javascript" src="~/scripts/jqGrid/jquery.jqGrid.js"></script>
<link type="text/css" href="~/content/jquery-ui/jquery-ui-1.10.4.custom.css" media="screen" rel="Stylesheet" />
<link type="text/css" href="~/Content/jqGrid/ui.jqgrid.css" media="screen" rel="Stylesheet" />
第一步:定义一个呈现数据的表格 <table id="gridUploadProg" style=" height:100%; width:100%"></table>
第二部:在js里面进行相关设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | var grid = $( "#gridUploadProg" ); grid.jqGrid({ url: 'ResultManage/QueryData' , mtype: 'post' , datatype: 'json' , loadonce: false , data: {}, colNames: [ '项目ID' , '行政区代码' , '行政区名称' ], colModel: [ { name: 'XMID' , index: 'XMID' , sortable: false , key: true , align: 'center' , hidden: true }, { name: 'XZQDM' , index: 'XZQDM' }, { name: 'XZQMC' , index: 'XZQMC' , align: 'center' }, { name: 'XMMC' , index: 'XMMC' , align: 'center' } ], rowNum: 10, rowList: [10, 15, 20, 30], pager: '#gridUploadProgPager' , emptyrecords: "没有符合要求的数据" , gridview: true , rownumbers: true , sortname: 'ProvinceCode' , viewrecords: true , sortorder: 'asc' , multiselect: true , caption: '任务管理列表' , jsonReader: { page: 'page' , total: 'total' , records: 'records' , root: 'rows' }, height: '100%' , loadui: 'block' , autoScroll: false , loadComplete: function (data) { //完成服务器请求后,回调函数 // alert(data.records); if (data.records == 0) { //如果没有记录返回,追加提示信息 $( "p" ).appendTo($( "#gridUploadProg" )).addClass( "nodata" ).html( "找不到相关数据!" ); } else { //否则,删除提示 $( "p.nodata" ).remove(); } } }); |
第三步:在control里面进行模型的获取JSON的转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public void QueryData( string sidx = "XZQDM" , string sord = "DESC" , int rows = 10, int page = 1) { SearchConditionModel searchCondition = new SearchConditionModel(); if ( this .Session[ "SearchCondition" ] != null ) { searchCondition = (SearchConditionModel) this .Session[ "SearchCondition" ]; } int totalRecord = 0; IEnumerable<TsakManageViewModel> modelList = this .TaskManageDataContext.GetModelList( out totalRecord, provinceCode: searchCondition.XZQ, rwlx: searchCondition.RWLX, taskResult: searchCondition.SFTG, orderField: sidx, orderType: sord, pageIndex: page, pageSize: rows ); //int index = (page - 1) * rows; // 开始记录数 int totalPage = totalRecord % rows == 0 ? totalRecord / rows : totalRecord / rows + 1; // 计算总页数 int pagesize = rows; string son = Newtonsoft.Json.JsonConvert.SerializeObject(modelList); son = "{ \"page\": " + page.ToString() + ", \"total\": " + totalPage.ToString() + ", \"records\": " + totalRecord.ToString() + ", \"rows\": " + son + "}" ; Response.Write(son); } |
以后每一次分页查询都会进到这个action里面,实现分页实时查询数据。JSON数据前面一定要加记录数等信息用于分页的显示。当然还有其他方法返回json数据。理想的格式应该的返回JsonResult的。像这样 return Json(modelList, JsonRequestBehavior.AllowGet);这种方法我还没有测试,不清楚能不能返回数据到界面上。这种方法看起来明显正规一点
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构