开发者

How to add to POST when loading a subgrid in jqGrid?

开发者 https://www.devze.com 2023-01-27 05:51 出处:网络
How do I add a parameter to the POST variable for the subgrid? All I want to do is add a variable called \'q\' to the $_POST for the subgrid.

How do I add a parameter to the POST variable for the subgrid? All I want to do is add a variable called 'q' to the $_POST for the subgrid.

MY JS code:

$(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"

});
});

MY PHP code:

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-开发者_如何学JAVA>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);


I don't use PHP myself, but I can say that the client part works correct. I verified that during the loading of the grid will be POSTed the following data

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

So the data are exactly like you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消