【问题标题】:JQ Grid, Sorting does not work as expectedJQGrid,排序不按预期工作
【发布时间】:2014-02-11 22:23:42
【问题描述】:

我有一个应该在单击列标题时排序的网格。某些列(例如 ID)按预期排序,但某些列(例如单击日期)虽然向服务器发送请求并排序,但行排序不佳。我已经附上了一张图片来描述这个场景 从图片来看,时间列没有按预期排序,最后一行应该比排序时更早(JAN 24 before 28)...

这是我的服务器端代码..

//Get the requested page
$page = $_GET['page'];

//Get how many rows we want to have into the grid
$limit = $_GET['rows'];

// get index row - i.e. user click to sort. At first time sortname parameter -
// after that the index from colModel 
$sidx = $_GET['sidx']; 

// sorting order - at first time sortorder 
$sord = $_GET['sord']; 

// if we not pass at first time index use the first column for the index or what you want
if(!$sidx) $sidx =1;

//array to translate the search type
$ops = array(
    'eq'=>'=', //equal
    'ne'=>'<>',//not equal
    'lt'=>'<', //less than
    'le'=>'<=',//less than or equal
    'gt'=>'>', //greater than
    'ge'=>'>=',//greater than or equal
    'bw'=>'LIKE', //begins with
    'bn'=>'NOT LIKE', //doesn't begin with
    'in'=>'LIKE', //is in
    'ni'=>'NOT LIKE', //is not in
    'ew'=>'LIKE', //ends with
    'en'=>'NOT LIKE', //doesn't end with
    'cn'=>'LIKE', // contains
    'nc'=>'NOT LIKE'  //doesn't contain
);
function getWhereClause($col, $oper, $val){
    global $ops;
    if($oper == 'bw' || $oper == 'bn') $val .= '%';
    if($oper == 'ew' || $oper == 'en' ) $val = '%'.$val;
    if($oper == 'cn' || $oper == 'nc' || $oper == 'in' || $oper == 'ni') $val = '%'.$val.'%';
    return " WHERE $col {$ops[$oper]} '$val' ";
}

$where = ""; //if there is no search request sent by jqgrid, $where should be empty
$searchField = isset($_GET['searchField']) ? $_GET['searchField'] : false;
$searchOper = isset($_GET['searchOper']) ? $_GET['searchOper']: false;
$searchString = isset($_GET['searchString']) ? $_GET['searchString'] : false;
if ($_GET['_search'] == 'true') {
    $where = getWhereClause($searchField,$searchOper,$searchString);
}

mysql_query("SET NAMES 'utf8'");

// calculate the number of rows for the query. We need this for paging the result 
$result = mysql_query("SELECT COUNT(*) AS count FROM renal_accessLog"); 
$row = mysql_fetch_array($result,MYSQL_ASSOC); 
$count = $row['count']; 

// calculate the total pages for the query 
if( $count > 0 && $limit > 0) { 
              $total_pages = ceil($count/$limit); 
} else { 
              $total_pages = 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 renal_accessLog ".$where." ORDER BY $sidx $sord LIMIT $start , $limit"; 
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error()); 


 $responce = new stdClass();
$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'];

这是我的客户端代码

 $(function () {
            $("#list").jqGrid({
                url:"grid_accessLog.php",
                datatype: "json",
                mtype: "GET",
        colNames:["ID","Ip Address","User Info","Time","Page","Referrer","Search","User Agent","Notes"],
                colModel: [
            { name: "id",index:"id", width: 55,search:true},
            { name: "ip_address",index:"ip_address",search:true, width: 90 },
            { name: "user_info",index:"user_info",search:true, width: 100},
            { name: "time",index:"time",search:true,sorttype:"text", width: 90},
            { name: "page",index:"page",search:true, width: 120},
            { name: "referrer",index:"referrer",search:true, width: 90 },
            { name: "search",index:"search",search:true, width: 90 },
            { name: "user_agent",index:"user_agent",search:true, width: 120 },
        { name: "notes",index:"notes",search:true, width: 120 }
        ],

         pager: "#pager",
            rowNum: 30,
            rowList: [10,20,30],
            autowidth:true,
            sortname: "id",
            sortorder: "desc",
            viewrecords: true,
            autoencode: true,
            caption: "Access Log",
            height: 'auto'

                            }).navGrid("#pager", {search:true, edit:false,add:false,del:false,searchtext:"Search"});
                                             });//end ready

谢谢!

【问题讨论】:

    标签: jquery sorting jqgrid


    【解决方案1】:

    在您提到的代码中,默认 sorcolumn 是 id。 将sortname: "id" 更改为sortname: "time"

    【讨论】:

    • 我之前尝试过此选项,但问题仍然存在。感谢您的意见。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多