【问题标题】:How to add to POST when loading a subgrid in jqGrid?在 jqGrid 中加载子网格时如何添加到 POST?
【发布时间】:2010-11-29 16:31:58
【问题描述】:

如何为子网格的 POST 变量添加参数?我要做的就是将一个名为“q”的变量添加到子网格的 $_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);

【问题讨论】:

    标签: javascript ajax variables post jqgrid


    【解决方案1】:

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

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

    所以数据和你想要的完全一样。

    【讨论】:

    • 它适用于主网格的第一次调用,但我如何编辑子网格的 POST。
    • @WAC0020:好的。现在我明白了问题所在。看来您必须使用serializeSubGridData 函数添加将要发送的其他数据(请参阅trirand.com/jqgridwiki/doku.php?id=wiki:subgrid#events)。
    • @WAC0020:顺便说一句,你可以在源代码中看到github.com/tonytomov/jqGrid/blob/master/js/grid.subgrid.js#L153 有一个小错误 ig jqGrid:将使用ts.p.serializeSubGridData.call(ts,dp) 代替ts.p.serializeSubGridData(ts, dp)。因此该函数将有两个参数而不是一个,并且不会设置this。考虑到serializeSubGridData 的第二个参数是应该发布的数据。您可以将q 属性添加到数据中,并使用return 返回修改后的数据。
    • @WAC0020:我在 jqGrid 主论坛上发布了这个错误:trirand.com/blog/?page_id=393/bugs/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    相关资源
    最近更新 更多