只是用一个例子来完成。
大家好,我遇到了同样的问题,但我看不到任何例子。所以,我解决了我的问题,我会给你一个例子。
我有一个名为“estado”的表(id、nome、sigla、codigo_ibge)。我实现了 jqGrid Guriddo Responsive 来进行所有的 CRUD 操作。
HTML
<!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";
}
}
我希望通过一个真实的例子有所帮助。享受。