jqgrid能前台分页么?jqgrid前端分页和排序的实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jqgrid能前台分页么?jqgrid前端分页和排序的实现相关的知识,希望对你有一定的参考价值。
jqgrid前端分页和排序的实现,不要 在后台的。知道的帮忙解决下,给个例子或提示,谢谢。
参考技术A jQuery("#list27").jqGrid(url:'localset.php',
datatype: "json",
height: 255,
width: 600,
colNames:['Index','Name', 'Code'],
colModel:[
name:'item_id',index:'item_id', width:65, sorttype:'int',
name:'item',index:'item', width:150,
name:'item_cd',index:'item_cd', width:100
],
rowNum:50,
rowTotal: 2000, ----一次加载的最大行数
rowList : [20,30,50],
loadonce:true,
----一次加载
mtype: "GET",
rownumbers: true,
rownumWidth: 40,
gridview: true,
pager: '#pager27',
sortname: 'item_id',
viewrecords: true,
sortorder: "asc",
caption: "Loading data from server at once"
);追问
你这个是前端分页么?
你这个是前端分页么?
追答我手头没有代码测试,你试试看,一次从后台获取2000条数据,每次只显示20行。
参考技术B html代码举例<html>
<head>
<title>jqGrid 实例</title>
</head>
<body>
···代码省略···
<table id="s2list"></table>
<div id="s2pager"></div>
<div id="filter" style="margin-left:30%;display:none">Search Invoices</div>
···代码省略···
</body>
</html>
javascript代码举例
$(function()
pageInit();
);
function pageInit()
jQuery("#s2list").jqGrid(
url:ctx+'/JSONData',
datatype: "json",
colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'],
colModel:[
name:'id',index:'id', width:65,
name:'invdate',index:'invdate', width:90,
name:'name',index:'name', width:100,
name:'amount',index:'amount', width:80, align:"right",
name:'tax',index:'tax', width:80, align:"right", edittype:'select', editoptions:value:":All;0.00:0.00;12:12.00;20:20.00;40:40.00;60:60.00;120:120.00",
name:'total',index:'total', width:80,align:"right",
name:'note',index:'note', width:150, sortable:false
],
rowNum:10,
mtype: "POST",
rowList:[10,20,30],
pager: '#s2pager',
sortname: 'id',
viewrecords: true,
sortorder: "desc",
caption:"Multiple Form Search Example",
onHeaderClick: function (stat)
if(stat == 'visible' )
jQuery("#filter").css("display","none");
)
jQuery("#s2list").jqGrid('navGrid','#s2pager',edit:false,add:false,del:false,search:false,refresh:false);
jQuery("#s2list").jqGrid('navButtonAdd',"#s2pager",caption:"Search",title:"Toggle Search",
onClickButton:function()
if(jQuery("#filter").css("display")=="none")
jQuery(".HeaderButton","#gbox_s2list").trigger("click");
jQuery("#filter").show();
);
jQuery("#s2list").jqGrid('navButtonAdd',"#s2pager",caption:"Clear",title:"Clear Search",buttonicon:'ui-icon-refresh',
onClickButton:function()
var stat = jQuery("#s2list").getGridParam('search');
if(stat)
var cs = jQuery("#filter")[0];
cs.clearSearch();
);
jQuery("#filter").jqGrid('filterGrid',"s2list",
gridModel:true,
gridNames:true,
formtype:"vertical",
enableSearch:true,
enableClear:false,
autosearch: false,
afterSearch : function()
jQuery(".HeaderButton","#gbox_s2list").trigger("click");
jQuery("#filter").css("display","none");
);
java servlet代码举例
package net.mn886.blog.jqgrid.loadding_data;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class JSONData
*/
public class JSONData extends HttpServlet
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public JSONData()
super();
// TODO Auto-generated constructor stub
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
// TODO Auto-generated method stub
doPost(req,resp);
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
// TODO Auto-generated method stub
String jsondata = "\"page\":\"1\"," +
" \"total\":2," +
" \"records\":\"13\"," +
" \"rows\":" +
" [" +
" " +
" \"id\":\"13\"," +
" \"cell\":" +
" [\"13\",\"2007-10-06\",\"Client 3\",\"1000.00\",\"0.00\",\"1000.00\",null]" +
" ," +
" " +
" \"id\":\"12\"," +
" \"cell\":" +
" [\"12\",\"2007-10-06\",\"Client 2\",\"700.00\",\"140.00\",\"840.00\",null]" +
" ," +
" " +
" \"id\":\"11\"," +
" \"cell\":" +
" [\"11\",\"2007-10-06\",\"Client 1\",\"600.00\",\"120.00\",\"720.00\",null]" +
" ," +
" " +
" \"id\":\"10\"," +
" \"cell\":" +
" [\"10\",\"2007-10-06\",\"Client 2\",\"100.00\",\"20.00\",\"120.00\",null]" +
" ," +
" " +
" \"id\":\"9\"," +
" \"cell\":" +
" [\"9\",\"2007-10-06\",\"Client 1\",\"200.00\",\"40.00\",\"240.00\",null]" +
" ," +
" " +
" \"id\":\"8\"," +
" \"cell\":" +
" [\"8\",\"2007-10-06\",\"Client 3\",\"200.00\",\"0.00\",\"200.00\",null]" +
" ," +
" " +
" \"id\":\"7\"," +
" \"cell\":" +
" [\"7\",\"2007-10-05\",\"Client 2\",\"120.00\",\"12.00\",\"134.00\",null]" +
" ," +
" " +
" \"id\":\"6\"," +
" \"cell\":" +
" [\"6\",\"2007-10-05\",\"Client 1\",\"50.00\",\"10.00\",\"60.00\",\"\"]" +
" ," +
" " +
" \"id\":\"5\"," +
" \"cell\":" +
" [\"5\",\"2007-10-05\",\"Client 3\",\"100.00\",\"0.00\",\"100.00\",\"no tax at all\"]" +
" ," +
" " +
" \"id\":\"4\"," +
" \"cell\":" +
" [\"4\",\"2007-10-04\",\"Client 3\",\"150.00\",\"0.00\",\"150.00\",\"no tax\"]" +
" " +
" ]," +
" \"userdata\":\"amount\":3220,\"tax\":342,\"total\":3564,\"name\":\"Totals:\"" +
" ";
response.getWriter().write(jsondata);
jqGrid 不显示数据,但显示分页和列名/工作正常
【中文标题】jqGrid 不显示数据,但显示分页和列名/工作正常【英文标题】:jqGrid not displaying data but paging and column names are displayed/working fine 【发布时间】:2013-12-27 23:40:58 【问题描述】:我一直在关注https://tpeczek.codeplex.com/ 的教程以使 jqGrid 正常工作,并在更新我的 GetData() actionresult 以启用分页和排序之后,现在我的网格不再显示数据,但我不确定为什么没有出现错误抛出。曾经工作的代码:
public ActionResult GetData()
try
var model = (from s in db.Sections
select new
s.ID,
s.RouteName,
s.Title
).ToList();
return Json(model, JsonRequestBehavior.AllowGet);
catch (Exception ex)
ErrorSignal.FromCurrentContext().Raise(ex);
return Json(null, JsonRequestBehavior.AllowGet);
我的新代码尝试添加分页和排序。
public ActionResult GetData(string sidx, string sord, int page, int rows) 尝试 int RowCount = db.Sections.Count(); int SkipCount = (page * rows);
string OrderBy = (sidx + " " + sord);
var SectionData = new
total = (int)Math.Ceiling((float)RowCount / (float)rows),
page = page,
records = RowCount,
rows = (from s in db.Sections
select new
id = s.ID,
cell = new string[]
SqlFunctions.StringConvert((double)s.ID).Trim(),
s.RouteName,
s.Title
.OrderBy(x => sidx)
.Skip(SkipCount)
.Take(rows)
).ToArray()
;
return Json(SectionData, JsonRequestBehavior.AllowGet);
catch (Exception ex)
ErrorSignal.FromCurrentContext().Raise(ex);
return Json(null, JsonRequestBehavior.AllowGet);
编辑: jqGrid代码:
<script type="text/javascript">
$( document ).ready( function ()
$( '#Sections' ).jqGrid(
url: '/Admin/Section/GetData',
datatype: 'json',
mtype: 'GET',
colNames: ['ID', 'RouteName', 'Title'],
colModel: [
name: 'ID', index: 'ID', width: '10' ,
name: 'RouteName', index: 'RouteName', width: '50' ,
name: 'Title', index: 'Title'
],
autowidth: true,
height: '100%',
pager: $( '#SectionsPager' ),
rowNum: 10,
sortname: 'ID',
sortorder: 'asc',
viewrecords: true
).navGrid(
'#SectionsPager',
//enabling buttons
add: true, del: false, edit: false, search: false ,
//edit options
width: 'auto' ,
//add options
width: 'auto', url: '/Admin/Section/Add' ,
//delete options
);
);
【问题讨论】:
@Eagle..请显示 jqgrid 的代码..可能有错误 @Avinash 很抱歉,因为匆忙出去工作而忘记了。我现在已经添加了。 【参考方案1】:所以我最终将命令 loadonce: true
添加到 jqGrid 配置中以启用客户端排序,并删除了服务器端处理排序的所有代码。我的网格现在可以正常显示数据和排序和分页。
【讨论】:
@Matthew..首先,jqgrid有两种排序方式。服务器端和客户端。您没有提到您的任何要求,即您想要客户端还是服务器端。其次,由于您的代码类似于服务器端分页,因此我添加了答案。因此,请在投反对票之前三思,在问题中正确提及您的要求。您提出的问题,以及您为此回答的答案,肯定会误导许多 SO 用户。这是因为,您的问题涉及服务器端分页,而答案是客户端分页。 @Avinash 我对您投了反对票,因为您添加datatype: "json"
的建议已经在我的代码中,并且您建议在我的代码不存在时从我的代码中删除 loadOnce: true
。您的回答没有提供对问题或解决方案的深入了解。我的问题中确实有服务器端代码,但不需要在服务器端或客户端上都有它,所以我的回答确实解决了让分页工作的问题。关于 SO 有很多问题,人们试图以一种方式做事,而最好的答案是另一种方式,这将是其中之一。【参考方案2】:
您需要设置:datatype: "json"
,如果您使用的是loadOnce:true
,请将其删除..
【讨论】:
根据 jqGrid 文档,loadOnce 的默认设置是 false。以上是关于jqgrid能前台分页么?jqgrid前端分页和排序的实现的主要内容,如果未能解决你的问题,请参考以下文章
jqgrid 如何获取分页的信息:当前页和总页数,作为参数传递到url