1

我有一个不使用 loadonce: true 选项的 jqGrid。服务器在发送每个查询时动态处理排序。唯一的问题是每个页面都显示相同的数据。当我单击列标题时,数据会正确排序,但我无法显示不同的页面。

这是处理排序的函数。它与 http://www.trirand.com/jqgridwiki/doku.php?id=wiki:first_grid&s[]=sidx 基本相同,除了使用 postgres 而不是 mysql

function popGrid($conn){
    $page = $_POST['page'];
    $limit = $_POST['rows'];
    $sidx = $_POST['sidx'];
    $sord = $_POST['sord'];
    $sqlCount = $_POST['sqlCount'];
    $sqlSelect = $_POST['sqlSelect'];
    $sqlSelect2 = $_POST['sqlSelect2'];
    $sqlSelect3 = $_POST['sqlSelect3'];
    $label1 = $_POST['label1'];
    $label2 = $_POST['label2'];
    $lbl1 = $_POST['lbl1'];
    $lbl2 = $_POST['lbl2'];
    $lbl3 = $_POST['lbl3'];

    if(!$sidx){ $sidx = 1; }

    $query = pg_query($conn,$sqlCount);
    $row = pg_fetch_row($query);
    $count = $row[0];

    if($count > 0 && $limit > 0){ $total_pages = ceil($count/$limit); }
    else{ $total_pages = 0; }

    if($page > $total_pages){ $pages = $total_pages; }

    $start = $limit*page - $limit;

    if($start < 0){ $start = 0; }

    header("Content-type: text/xml;charset=utf-8");

    $s = "<?xml version='1.0' encoding='utf-8'?>
        <rows>
        <page>".$page."</page>
        <total>".$total_pages."</total>
        <records>".$count."</records>";

    $sqlSelect .= " ORDER BY $sidx $sord LIMIT $limit OFFSET $start ";
    $query = pg_query($conn,$sqlSelect);
    while($row = pg_fetch_row($query)){
        $s .= "<row id='".$row[0]."'>";
        if($lbl1){  $s .= "<cell>$lbl1</cell>"; }
        $s .=   "<cell>".$row[0]."</cell>
                <cell>".$row[1]."</cell>";
        if($label1){ $s.= "<cell>$label1</cell>"; } 
        $s .= "<cell>".$row[2]."</cell>
                <cell>".$row[3]."</cell>
                <cell>".$row[4]."</cell>
                <cell>".$row[5]."</cell>
                <cell>".$row[6]."</cell>
                <cell>".$row[7]."</cell>
                <cell>".$row[8]."</cell>
                <cell>".$row[9]."</cell>
                <cell>".$row[10]."</cell>
                <cell>".$row[11]."</cell>
                <cell>".$row[12]."</cell>
                <cell>".$row[13]."</cell>
                <cell>".$row[14]."</cell>
                <cell>".$row[15]."</cell>
                </row>";
    }
    if($sqlSelect2){
        $sqlSelect2 .= " ORDER BY $sidx $sord LIMIT $limit OFFSET $start ";
        $query = pg_query($conn,$sqlSelect2);
        while($row = pg_fetch_row($query)){
            $s .= "<row id='".$row[0]."'>";
            if($lbl2){ $s .= "<cell>$lbl2</cell>"; }
            $s .= " <cell>".$row[0]."</cell>
                    <cell>".$row[1]."</cell>";
            if($label2){ $s .= "<cell>$label2</cell>"; }
            $s .=   "<cell>".$row[2]."</cell>
                    <cell>".$row[3]."</cell>
                    <cell>".$row[4]."</cell>
                    <cell>".$row[5]."</cell>
                    <cell>".$row[6]."</cell>
                    <cell>".$row[7]."</cell>
                    <cell>".$row[8]."</cell>
                    <cell>".$row[9]."</cell>
                    </row>";
        }
    }
    if($sqlSelect3){
        $sqlSelect3 .= " ORDER BY $sidx $sord LIMIT $limit OFFSET $start ";
        $query = pg_query($conn,$sqlSelect3);
        while($row = pg_fetch_row($query)){
            $s .= "<row id='".$row[0]."'>";
            if($lbl3){ $s .= "<cell>$lbl3</cell>"; }
            $s .= " <cell>".$row[0]."</cell>
                    <cell>".$row[1]."</cell>";
            if($label2){ $s .= "<cell>$label2</cell>"; }
            $s .=   "<cell>".$row[2]."</cell>
                    <cell>".$row[3]."</cell>
                    <cell>".$row[4]."</cell>
                    <cell>".$row[5]."</cell>
                    <cell>".$row[6]."</cell>
                    <cell>".$row[7]."</cell>
                    </row>";            
        }
    }   
    $s .= "</rows>";
    echo $s;

}

这是网格,以防出现问题:

$('#lst_users').jqGrid({
    url: 'hours_func.php',
    dataType: 'xml',
    mtype: 'POST',
    postData: {
        action: 'popGrid',
        sqlCount: sqlCount,
        sqlSelect: sqlSelect
    },
    colNames:['ID','LOGIN ID','PASSWORD','FIRST NAME','LAST NAME','EMAIL','ADMIN'],
    colModel:[
        {   name: 'id',
            required: false,
            editable: false,
        },
        {   name: 'login_id',
            editoptions: {
                maxlength: 4
            }
        },
        {   name: 'password',
            editoptions: {
                maxlength: 30
            }
        },
        {   name: 'fname',
            editoptions: {  
                maxlength: 20
            }
        },
        {   name: 'lname',
            editoptions: {
                maxlength: 30
            }
        },
        {   name: 'email',
            required: false,
            editoptions:{
                maxlength: 30
            }
        },
        {   name: 'admin',
            editoptions: {
                dataInit: function(elem){
                    $(elem).mask("~");
                }
            }
        },      
    ],
    cmTemplate:{ 
        align: 'center',
        colwidth: 80,
        editable: true,
        required: true,
    },
    caption: 'Users',
    pager:'#pgr_users',
    editurl: 'hours_func.php',
    loadonce: false,
}).navGrid('#pgr_users',
    //include all buttons
    {   search: false,
    },  
    //edit options
    {   bSubmit: "Edit",
        width: 350,
        recreateForm: true,
        recreateFilter: true,
        closeOnEscape: true,
        editData: { 
            action: 'grdUsers',
            id: function(){
                var id = $('#lst_users').jqGrid('getGridParam','selrow');
                var val = $('#lst_users').jqGrid('getCell',id,'id');
                return val;
            }
        },
    },  
    //add options
    {   bSubmit: "Add",
        width: 350,
        recreateForm: true,
        recreateFilter: false,
        closeOnEscape: true,
        editData: { action: 'grdUsers' }
    },  
    //delete options
    {   recreateForm: false,
        recreateFilter: false,
        closeOnEscape: true,
        delData: { 
            action: 'grdUsers',
            id: function() {
                var sel_id = $('#lst_users').jqGrid('getGridParam','selrow');
                var value = $('#lst_users').jqGrid('getCell', sel_id, 'id');
                return value;
            }
        }
    },
    //search options
    {},
    //view options
    {}
);
4

1 回答 1

2

您设置分页的方式将在服务器端处理。如果您使用 Firebug 或类似的东西,您可以看到 jqGrid 将向您的控制器发送信息,允许您过滤到正确的页面。您的控制器已经在处理数据的排序,所以现在您只需要利用分页信息来获取正确的数据页面。

下面的代码适用于 C#,但它将作为示例:

您的 Controller 将从页面请求上 jqGrid 的帖子中收到以下信息

GetGridData(string sidx, string sord, int page, int rows, bool _search, string filters)
{
 ....

然后,您可以使用此信息来过滤 jqGrid 请求的数据页面,就像我在本示例中所做的那样。

...
var pagedQuery = DataSet.OrderBy(sidx + " " + sord).Skip((page - 1) * rows).Take(rows).ToList();
...
于 2013-05-01T13:51:23.403 回答