Jquery ui loadonce:true导致jqGrid的分页出现问题
我有一个jqGrid,如下所述: UsersList.aspxJquery ui loadonce:true导致jqGrid的分页出现问题,jquery-ui,jqgrid,Jquery Ui,Jqgrid,我有一个jqGrid,如下所述: UsersList.aspx 使用者 userlistgroup.js 函数createUsersGrid(getUsersForClientGroupUrl){ $(“#客户端组用户列表”).jqGrid({ 高度:“自动”, 宽度:“590”, url:getUsersForClientGroupUrl+“?random=“+Math.random(), rowNum:10, mtype:“POST”, viewrecords:是的, postData:
使用者
userlistgroup.js
函数createUsersGrid(getUsersForClientGroupUrl){
$(“#客户端组用户列表”).jqGrid({
高度:“自动”,
宽度:“590”,
url:getUsersForClientGroupUrl+“?random=“+Math.random(),
rowNum:10,
mtype:“POST”,
viewrecords:是的,
postData:{clientGroup:getClientGroupId(),活动:true},
数据类型:“json”,
colNames:['ClientGroupID','Login','Role'],
//PG按钮:错误,
//pgtext:null,
//viewrecords:false,
gridview:没错,
colModel:[
{name:'ClientGroupID',index:'ClientGroupID',hidden:true},
{name:'Login',index:'Login',search:false,width:130,sorttype:'string},
{name:'Role',index:'Role',search:false,width:100}
],
标题:“客户端组的用户:”+getClientGroupName(),
寻呼机:“#客户端组用户列表寻呼机”,
ondblClickRow:函数(用户ID){
编辑用户(userId);
},
sortname:'登录',
排序器:“asc”,
loadui:“启用”,
loadError:函数(xhr,状态){
location.reload();
}
})
.jqGrid('navGrid','#客户端组用户列表寻呼机',
{add:false,del:false,edit:false,search:false,view:false})
}
Login
列上的排序不起作用。但是,如果我放置loadonce:true
,则它开始工作,但记录总数没有反映出来,分页功能停止工作
有谁能帮我处理排序和分页,与datatype:'json'
一起工作吗
谢谢和问候,
Santosh Kumar Patro您似乎在理解jqGrid如何与服务器交互方面存在典型的问题 如果使用
datatype:'json'
而不使用loadonce:true
选项,则服务器负责对数据进行排序、分页和选择性搜索/过滤。在这种情况下,对服务器的每个请求都包含重要的输入参数,默认值为:页面
,行
,sidx
和sord
。还有其他参数\u search
、nd
和其他一些参数,但在您的案例中没有那么重要。因此,在第一次加载网格内容时,jqGrid发布到URLgetUsersForClientGroupUrl
page=1&rows=10&sidx=Login&sord=asc
行
、sidx
和sord
参数的值对应于您使用的rowNum
、sortname
和sortorder
选项的值。服务器应提供请求的数据页(第一页数据,页面大小为10行)。可以使用网格的prmNames
选项重命名页面
,行
,sidx
和sord
参数
重要的是要了解,当用户单击“下一页”/“上一页”按钮时,或者如果用户单击列标题以更改排序,jqGrid会向服务器发出新请求,服务器应根据输入参数提供数据页
另一方面如果使用loadonce:true
选项,则服务器应返回所有数据(所有页面)。加载的数据应仅与sidx
和sord
选项相对应。加载数据后jqGrid更改数据类型
“至本地”
。所有下一步的排序、分页和过滤(请参阅方法)或排序(请参阅和)将由jqGrid内部(在客户端)实现
如果需要从服务器重新加载数据,则应将datatype
的值重置为“json”
(请参阅)。通常在navGrid
的回调中执行此操作。因此,如果用户单击导航栏的“刷新”按钮,网格将从服务器重新加载
我假设您只是没有更改服务器代码,当您使用loadonce:true
选项时,服务器仍然返回一页数据。这是错误的。不要忘记对服务器上的初始数据进行排序
您发布的代码中的一些常见问题。我建议您从URL中删除“?random=“+Math.random()
部分,因为它没有意义。这种方法通常用于防止在使用mtype:“GET”
(HTTP GET动词)时缓存服务器响应。您可以使用mtype:“POST”
。因此,反应根本不会改变。此外,如果要使用mtype:“GET”
,那么jqGrid会使用nd
参数附加已经存在的URL,该参数的作用与随机
参数相同。最好的方法是将服务器响应的Cache Control
HTTP头设置为private,max age=0
,并使用prmNames:{nd:null}
jqGrid选项删除nd
。有关缓存的更多信息,请参阅和
另一个问题:您应该了解,您只能调用createUsersGrid
函数一次。之后,将修改带有id=“客户端组用户列表”
的
。下一次调用createUsersGrid
函数将什么也不做。人们应该考虑是否需要将JavaScript片段放在函数中,如果它只能被调用一次。或者,可以使用GridUnload
方法在下次使用之前销毁所有现有的jqGrid结构
如果需要将发送到客户端的clientGroup:getClientGroupId()
参数
rows = model.Select(x => new { x.POID,
x.HA1.HA1,
x.PONumber,
status = x.POStatu.Name,
vendor=x.Vendor.Name,
x.POCreatedDate,
x.POSenttoVendor
}
).ToArray().ToPagedList(pageIndex,pageSize).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- We support more than 40 localizations -->
<script type="text/ecmascript" src="../../resources/jqGrid/js/i18n/grid.locale-pt-br.js"></script>
<!-- This is the Javascript file of jqGrid -->
<script type="text/ecmascript" src="../../resources/jqGrid/js/jquery.jqGrid.min.js"></script>
<!-- A link to a Boostrap and jqGrid Bootstrap CSS siles-->
<link rel="stylesheet" type="text/css" media="screen" href="../../resources/jqGrid/css/ui.jqgrid-bootstrap.css" />
<style type="text/css">
.ui-jqgrid .ui-jqgrid-bdiv {
position: relative;
margin: 0em;
padding:0;
/*overflow: auto;*/
overflow-x:hidden;
overflow-y:auto;
text-align:left;
}
.customelement .radio-inline input {
margin-left: -40px;
padding-right: 10px;
}
.typeahead {
z-index: 10051;
}
</style>
<script type="text/javascript">
$.jgrid.defaults.width = 850;
$.jgrid.defaults.responsive = true;
$.jgrid.defaults.styleUI = 'Bootstrap';
$.jgrid.styleUI.Bootstrap.base.rowTable = "table table-bordered table-striped";
$(document).ready(function () {
$("#jqGrid").jqGrid({
url: '../ctrl_gerenciamento/gerenciaEstadoJqGridLoad.php',
editurl: '../ctrl_gerenciamento/gerenciaEstadoJqGridEdit.php',
datatype: "json",
mtype: "GET",
page: 1,
jsonReader: {
page: "obj.currpage",
total: "obj.totalpages",
records: "obj.totalrecords",
repeatitems: false,
root: "obj.rows",
cell: "cell",
id: "0"
},
colModel: [
{
label: 'ID',
name: 'id',
width: 20,
key: true,
editable: false
},
{
label: 'Nome',
name: 'nome',
width: 145,
editable: true,
editrules : { required: true },
search: true,
stype: 'text'
},
{
label: 'Sigla',
name: 'sigla',
width: 35,
editable: true,
editrules : { required: true },
search: true,
stype: 'text'
},
{
label: 'Código IBGE',
name: 'codigo_ibge',
width: 60,
editable: true,
editrules : { required: true },
search: true,
stype: 'text'
}
],
sortname: 'nome',
sortorder : 'asc',
loadonce: false,
viewrecords: true,
width: 785,
height: 370,
rowNum: 10,
pager: "#jqGridPager"
});
$('#jqGrid').jqGrid('filterToolbar', {defaultSearch:'cn'});
$('#jqGrid').jqGrid('navGrid',"#jqGridPager",
{
edit: true,
add: true,
del: true,
search: false,
refresh: false,
view: false,
position: "left"
},
{
editCaption: "Editar",
width: "500",
height: "auto",
top: "120",
left: "500",
recreateForm: true,
closeAfterEdit: true,
errorTextFormat: function (data) {
return 'Error: ' + data.responseText
}
},
{
editCaption: "Adicionar",
width: "500",
height: "auto",
top: "120",
left: "500",
closeAfterAdd: true,
recreateForm: true,
errorTextFormat: function (data) {
return 'Error: ' + data.responseText
}
},
{
editCaption: "Apagar",
width: "500",
height: "auto",
top: "120",
left: "500",
errorTextFormat: function (data) {
return 'Error: ' + data.responseText
}
});
});
</script>
</head>
<body>
<br />
<h4> Gerenciamento de Estados </h4>
<br />
<div style="margin-left:10px">
<table id="jqGrid"></table>
<div id="jqGridPager"></div>
</div>
</body>
</html>
<?php
header('Content-Type: application/json; charset=utf-8');
include_once '../../classes/start.php';
$json = new JSON();
$page = $_GET['page'];
$limit = $_GET['rows'];
$sidx = $_GET['sidx'];
$sord = $_GET['sord'];
$_search = $_GET['_search'];
$filtro = array();
$filtro["nome"] = isset($_GET["nome"]) ? $_GET["nome"] : null;
$filtro["sigla"] = isset($_GET["sigla"]) ? $_GET["sigla"] : null;
$filtro["id"] = isset($_GET["id"]) ? $_GET["id"] : null;
$filtro["codigo_ibge"] = isset($_GET["codigo_ibge"]) ? $_GET["codigo_ibge"] : null;
try
{
if (!$sidx) $sidx = 1;
$result = Estado::contarTodosRegistros();
$qtdRegistros = $result[0]['count'];
if ($qtdRegistros > 0 && $limit > 0) {
$total_pages = ceil($qtdRegistros / $limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages) $page = $total_pages;
$start = $limit*$page - $limit;
if ($start < 0) $start = 0;
$result = null;
$saida = null;
if ($_search) {
$result = Estado::consultarTodosRegistros($sidx, $sord, $start, $limit, $filtro);
}else{
$result = Estado::consultarTodosRegistros($sidx, $sord, $start, $limit);
}
$saida["rows"] = $result;
$saida["totalrecords"] = "$qtdRegistros";
$saida["currpage"] = "$page";
$saida["totalpages"] = "$total_pages";
$json->setStatus("ok");
$json->setObjeto( $saida );
}
catch (Exception $ex)
{
$json->setStatus("erro");
$json->setMensagem($ex->getMessage());
}
$json->imprimirJSON();
public static function consultarTodosRegistros($sidx, $sord, $start, $limit, $filtro='')
{
$db = Database::conexao();
$temp = 'where 1=1';
if (!empty($filtro)){
if (!empty($filtro["nome"])) {
$temp .= " and estado.nome ilike '%". $filtro["nome"]."%'";
}
if (!empty($filtro["sigla"])) {
$temp .= " and estado.sigla ilike '%". $filtro["sigla"]."%'";
}
if (!empty($filtro["id"])) {
$temp .= " and estado.id = ". $filtro["id"];
}
if (!empty($filtro["codigo_ibge"])) {
$temp .= " and estado.codigo_ibge ilike '%". $filtro["codigo_ibge"]."%'";
}
}
$sql = "select *
from updrs.estado
$temp
order by $sidx $sord
limit $limit offset $start";
$result = $db->query($sql);
$result = $result->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
<?php
include_once '../../classes/start.php';
$operacao = $_POST["oper"];
$id = !empty($_POST['id']) ? $_POST['id'] : null;
$codigoIbge = isset($_POST["codigo_ibge"]) ? $_POST["codigo_ibge"] : null;
$sigla = isset($_POST["sigla"]) ? $_POST["sigla"] : null;
$nome = isset($_POST["nome"]) ? $_POST["nome"] : null;
if ($operacao == 'add')
{
try
{
$idResult = Estado::insert($codigoIbge, $sigla, $nome);
echo "registro atualizado: " . $idResult;
}
catch (Exception $e)
{
echo $e->getMessage();
}
}
if ($operacao == 'edit')
{
try
{
$count = Estado::update($codigoIbge, $sigla, $nome, $id);
echo $count . " registro atualizado.";
}
catch (Exception $e)
{
echo "Erro: Dados informados são inválidos";
}
}
if ($operacao == 'del')
{
try
{
$count = Estado::delete($id);
echo $count . " registro apagado.";
}
catch (Exception $e)
{
echo "Erro: Remoção do registro não permitida";
}
}