JqGrid Primary Key other than Id
Can any Body tell me how to configure primary key in JqGrid. Since Jqgrid uses id as primary key But I am using fid as primary key.
Example of 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",
开发者_如何学Python 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);
?>
You can either use
jsonReader: {repeatitems: false, id: "fid" }
or use
jsonReader: {repeatitems: false }
and define additional key:true
property for the 'fid' column definition:
{ name:'fid', index:'fid', key:true, width:55 }
The key:true
property overwrite id
property of jsonReader
.
One more remark: If you want use closeAfterEdit: true
and closeAfterAdd: true
setings of form editing you should define there on another place: as a part of navGrid
parameters:
jQuery("#jsonmap").jqGrid({
// ... jqGrid parameters
}).navGrid('#pager',
{/*navGrid options*/},
{closeAfterEdit: true}, // Edit form options
{closeAfterAdd: true} // Add form options
)
.navButtonAdd('#pjmap',{
// navButtonAdd options
})
精彩评论