2010-11-29 259 views
0

如何向子网格的POST变量添加参数?我想要做的就是将一个名为'q'的变量添加到子网格的$ _POST中。如何在jqGrid中加载子网格时添加到POST?

我的JS代码:

$(function(){ 
var lastsel; 
$("#list").jqGrid({ 
url:'example.php', 
postData:{q:1}, 
datatype: 'json', 
mtype: 'POST', 
colNames:['Anchor','Description','Email','Url','In Today','Out Today','In Total','Out Total','Credits','Ratio','Status'], 
colModel :[ 
    {name : 'anchor' , index : 'anchor', width : 55, 'editable':true, 'editoptions':{'size':30}}, 
    {'name' : 'description' , 'index' : 'description', 'width' : 55, 'editable':true, 'edittype':'textarea', 'editoptions':{'rows':'3','cols':'30'}}, 
    {'name' : 'email' , 'index' : 'email', 'width' : 55, 'editable':true, 'editoptions':{'size':30}}, 
    {'name' : 'url' , 'index' : 'url', 'width' : 55, 'editable':true, 'editoptions':{'size':30}}, 
    {'name' : 'in_today' , 'index' : 'in_today', 'width' : 55, 'align' : 'right'}, 
    {'name' : 'out_today' , 'index' : 'out_today', 'width' : 55, 'align' : 'right'}, 
    {'name' : 'in_total' , 'index' : 'in_total', 'width' : 55, 'align' : 'right'}, 
    {'name' : 'out_total' , 'index' : 'out_total', 'width' : 55, 'align' : 'right'}, 
    {'name' : 'credits' , 'index' : 'credits', 'width' : 55, 'align' : 'right', 'editable':true, 'editoptions':{'size':30}}, 
    {'name' : 'ratio' , 'index' : 'ratio', 'width' : 55, 'align' : 'right', 'editable':true, 'editoptions':{'size':30}}, 
    {'name' : 'status' , 'index' : 'status', 'width' : 55,'align' : 'center', 'editable':true, 'edittype':'checkbox', 'editoptions':{'value':"On:Off"}} 
], 
pager: '#pager', 
rowNum:10, 
rowList:[10,20,30], 
sortname: 'anchor', 
sortorder: 'desc', 
viewrecords: true, 
caption: 'My first grid', 
subGrid: true, 
subGridUrl: 'example.php', 
subGridModel: [{ name : ['Game','URL'],width : [200,300],params:['email'] }], 
onSelectRow: function(id){ 
    if(id && id!=lastsel){ 
     jQuery('#list').jqGrid('restoreRow',lastsel); 
     jQuery('#list').jqGrid('editRow',id,true); 
     lastsel=id; 
    } 
}, 
editurl: "server.php" 

}); 
}); 

我的PHP代码:

mysql_connect('****', '****', '****'); 
mysql_select_db('********'); 

switch ($_POST['q']) { 
case 1: 
    $page = $_POST['page']; 
    $limit = $_POST['rows']; 
    $sidx = $_POST['sidx']; 
    $sord = $_POST['sord']; 
    if(!$sidx) $sidx =1; 

    $sql2 = "SELECT COUNT(id) AS count FROM trades"; 
    $total = array_shift(mysql_fetch_row(mysql_query($sql2))); 

    // calculate the total pages for the query 
    $total_pages = ($total > 0 && $limit > 0) ? ceil($total/$limit) : 0; 

    // if for some reasons the requested page is greater than the total 
    // set the requested page to total page 
    if ($page > $total_pages) $page=$total_pages; 

    // calculate the starting position of the rows 
    $start = $limit*$page - $limit; 

    // if for some reasons start position is negative set it to 0 
    // typical case is that the user type 0 for the requested page 
    if($start <0) $start = 0; 

    // the actual query for the grid data 
    $SQL = "SELECT * FROM trades ORDER BY $sidx $sord LIMIT $start , $limit"; 
    $result = mysql_query($SQL) or die("Couldn't execute query.".mysql_error()); 

    $output->page = $page; 
    $output->total = $total_pages; 
    $output->records = $total; 


    while ($row = mysql_fetch_assoc($result)) { 
     switch ($row['status']) { 
      case 0: 
       $status = 'Off'; 
       break; 
      case 1: 
       $status = 'On'; 
       break; 
      case 2: 
       $status = 'Approval'; 
       break; 
     } 

     $rows[] = array(
       "id" => $row['id'], 
       "cell" => array(
        $row['anchor'] 
        ,$row['description'] 
        ,$row['email'] 
        ,$row['url'] 
        ,$row['in_today'] 
        ,$row['out_today'] 
        ,$row['in_total'] 
        ,$row['out_total'] 
        ,$row['credits'] 
        ,$row['ratio'] 
        ,$status 
       ) 
     ); 
    } 
    $output->rows = $rows; 
    break; 

case 2: 
    $id = $_POST['id']; 
    $output->id = $id; 
    // connect to the database 
    $SQL = "SELECT g.title, tp.id, tp.url FROM games AS g, trades_plugs AS tp WHERE tp.trader_id='{$id}' AND g.id = tp.game_id ORDER BY tp.game_id"; 
    $result = mysql_query($SQL) or die("Couldn't execute query.".mysql_error()); 
    while ($row = mysql_fetch_row($result)) { 
     $rows[] = array(
       "id" => $row[1], 
       "cell" => array(
        $row[0] 
        ,$row[2] 
       ) 
     ); 
    } 
    $output->rows = $rows; 

    break; 
} 


     echo json_encode($output); 

回答

0

我不使用PHP自己,但我可以说,客户端部分的工作是正确的。我证实,在网格加载过程中将发布以下数据

q=1&_search=false&nd=1291068269374&rows=10&page=1&sidx=anchor&sord=desc 

所以数据完全符合你的要求。

+0

它适用于主网格的第一次调用,但是如何编辑子网格的POST。 – WAC0020 2010-11-29 23:17:57