任何Body都可以告诉我如何在JqGrid中配置主键。由于Jqgrid使用id作为主键但我使用fid作为主键。
Jqgrid示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP jqGrid</title>
<link rel="stylesheet" type="text/css" media="screen" href="themes/redmond/jquery-ui-1.8.2.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="themes/ui.jqgrid.css" />
<script src="js/jquery-1.5.2.min.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script type="text/javascript">
// Here we set a globally the altRows option
//jQuery.extend(jQuery.jgrid.defaults, { altRows:true });
</script>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#jsonmap").jqGrid({
url:'example.php?q=4',
datatype: "json",
colNames:['FId','Id', 'Colname', 'Index','width','Form Name','Fiel Name','Editable','Show','Extra Field'],
colModel:[
{name:'fid',index:'fid', width:55,size:10,editable:false},
{name:'id',index:'id', width:90,editable:true,size:5}, /*name : database field name*/
{name:'col_name',index:'col_name', width:100,editable:true},
{name:'ind_ex',index:'ind_ex', width:80, align:"right",editable:true},
{name:'width',index:'width', width:80, align:"right",editable:true},
{name:'form_name',index:'form_name', width:80, align:"right",editable:true},
{name:'field_name',index:'field_name', width:80,align:"right",editable:true},
{name:'editable',index:'editable', width:150, sortable:false,editable:true},
{name:'s_how',index:'s_how', width:150, sortable:false,editable:true},
{name:'extra_field',index:'extra_field', width:150, sortable:false,editable:true}
/*{name:'id',index:'id', width:55,editable:false,editoptions:{readonly:true,size:10}},
{name:'invdate',index:'invdate', width:80,editable:true,editoptions:{size:10}},
{name:'name',index:'name', width:90,editable:true,editoptions:{size:25}},
{name:'amount',index:'amount', width:60, align:"right",editable:true,editoptions:{size:10}},
{name:'tax',index:'tax', width:60, align:"right",editable:true,editoptions:{size:10}},
{name:'total',index:'total', width:60,align:"right",editable:true,editoptions:{size:10}},
{name:'closed',index:'closed',width:55,align:'center',editable:true,edittype:"checkbox",editoptions:{value:"Yes:No"}},
{name:'ship_via',index:'ship_via',width:70, editable: true,edittype:"select",editoptions:{value:"FE:FedEx;TN:TNT"}},
{name:'note',index:'note', width:100, sortable:false,editable: true,edittype:"textarea", editoptions:{rows:"2",cols:"20"}}
*/ ],
rowNum:10,
rowList:[10,20,30],
pager: '#pjmap',
sortname: 'id',
viewrecords: true,
sortorder: "desc",
gridview: true,
editurl:'edit.php',
closeAfterEdit: true,
closeAfterAdd: true,
jsonReader: {
// root: "Rows",
//page: "Page",
//total: "Total",
//records: "Records",
repeatitems: false,
**fid: "PrimaryKey"**
//repeatitems : false,
//id: "5"
},
caption: "ADD DELETE UPDATE",
height: '100%'
}).navGrid('#pager',{edit:false,add:true,del:true,search:false}).navButtonAdd('#pjmap',{
caption:"Add",
buttonicon:"ui-icon-add",
closeAfterAdd:true,
onClickButton: function(){
jQuery("#jsonmap").editGridRow("new",{height:400,scroll:true,reloadAfterSubmit:false});
//alert("Adding Row");
},
position:"last"
})
/* .navButtonAdd('#pjmap',{
caption:"Del",
buttonicon:"ui-icon-del",
onClickButton: function(rp_ge,postdata){
alert(getRowData(postdata.id));
},
position:"last"
});
*/
});
</script>
</head>
<body>
<table id="jsonmap"></table>
<div id="pjmap"></div>
<!--<input type="BUTTON" id="bedata" value="Edit Selected" />-->
</body>
</html>
server2.php
<?php
include_once('../../../../wp-load.php');
include('dbconfig.php');
$page = $_GET['page']; // get the requested page
$limit = $_GET['rows']; // get how many rows we want to have into the grid
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
$sord = $_GET['sord']; // get the direction
if(!$sidx) $sidx =1; // connect to the database
$result = @mysql_query("SELECT COUNT(*) AS count FROM $table");
$row = @mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];
if ( $count >0 )
{ $total_pages = ceil($count/1); }
else
{ $total_pages = 0; }
if ($page > $total_pages)
$page=$total_pages;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
$SQL = "SELECT * from $table Order By id ";
$SQL = "SELECT * from $table";
$result = @mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
$responce->page = $page; $responce->total = $total_pages; $responce->records = $count; $i=0;
while($row = @mysql_fetch_array($result,MYSQL_ASSOC))
{
$responce->rows[$i]['id']=$row[id];
$responce->rows[$i]['cell']=array($row[id],$row[business_name],$row[title],$row[firstname],$row[lastname],$row[address],$row[city],$row[state],$row[postcode],$row[email]);
$i++;
}
echo json_encode($responce);
?>
答案 0 :(得分:6)
您可以使用
jsonReader: {repeatitems: false, id: "fid" }
或使用
jsonReader: {repeatitems: false }
并为'fid'列定义定义其他key:true
属性:
{ name:'fid', index:'fid', key:true, width:55 }
key:true
属性覆盖id
的{{1}}属性。
还有一句话:如果您想使用表单编辑的jsonReader
和closeAfterEdit: true
设置,您应该在另一个地方定义:作为closeAfterAdd: true
参数的一部分:
navGrid