2011-06-02 93 views
1

任何機構都可以告訴我如何在JqGrid中配置主鍵。由於Jqgrid使用id作爲主鍵但是我使用fid作爲主鍵。的jqGrid的JqGrid主鍵除Id以外

實施例:

<!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); 
?> 

回答

6

既可以使用

jsonReader: {repeatitems: false, id: "fid" } 

,或者使用

jsonReader: {repeatitems: false } 

並定義附加key:true屬性爲 'FID' 列定義:

{ name:'fid', index:'fid', key:true, width:55 } 

jsonReaderkey:true屬性覆蓋id財產。

還有一個備註:如果你想使用形式的closeAfterEdit: truecloseAfterAdd: true setings編輯,你應該在另一個地方定義有:爲navGrid參數的一部分:

jQuery("#jsonmap").jqGrid({ 
    // ... jqGrid parameters 
}).navGrid('#pager', 
      {/*navGrid options*/}, 
      {closeAfterEdit: true}, // Edit form options 
      {closeAfterAdd: true} // Add form options 
      ) 
    .navButtonAdd('#pjmap',{ 
       // navButtonAdd options 
      }) 
+0

主席先生,我已經試過這一個,但是這是不工作。 – user776022 2011-06-03 10:11:00

+0

@ user776022:你試過了什麼?您的演示是否可以從互聯網訪問,以便我可以調試它? – Oleg 2011-06-03 10:19:00