php - JqG​​rid 服务器端查询与搜索冲突

标签 php jquery mysql jqgrid

看看我的网格; grid 最后一列从 MySQL 数据库返回tiny int 值(0 或 1)。我想要实现的目标是仅显示 reviewed = 0 的行,并且仍然能够使用搜索来执行查询和检索 的字段已审核 = 1。我想在网格最初加载时显示reviewed =0的行。我尝试使用诸如 "SELECT * FROM table WHERE review = 0" 之类的代码来执行此服务器端操作。在这种情况下,查询有效,但搜索不起作用。有没有办法在客户端上执行此操作?请帮助我已经解决这个问题好几天了。我几乎尝试了所有方法。

Here's my server side code;
//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_apptRequest"); 
$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_apptRequest".$where." ORDER BY $sidx $sord LIMIT $start , $limit"; 
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error());

这是我的客户端代码

$(function () {
               $("#list").jqGrid({
            url:"grid_apptRequest.php",
            datatype: "json",
            mtype: "GET",
            colNames:["ID","Date","referralType","patientName","patientAddress","patientDOB","referralProvider","referralReason","contactName","contactPhone","contactEmail","contactFax","preferredTime","comments","reviewed"],
            colModel: [
        { name: "id",index:'id', width: 55,search:true, formatter:'showlink',formatoptions:{baseLinkUrl:'renal_apptRequest_review.php', target:'_blank'}},
        { name: "date",index:'date',search:true, width: 90 },
        { name: "referralType",index:'referralType',search:true, width: 80},
        { name: "patientName",index:'patientName',search:true, width: 120},
        { name: "patientAddress",index:'patientAddress',search:true, width: 120},
        { name: "patientDOB",index:'patientDOB',search:true, width: 90 },
        { name: "referralProvider",index:'referralProvider',search:true, width: 90 },
        { name: "referralReason",index:'referralReason',search:true, width: 120 },
        { name: "contactName",index:'contactName',search:true, width: 100},
        { name: "contactPhone",index:'contactPhone',search:true, width: 100},
        { name: "contactEmail",index:'contactEmail',search:true, width: 100 },
        { name: "contactFax",index:'contactFax',search:true, width: 80},
        { name: "preferredTime",index:'preferredTime',search:true, width: 70 },
        { name: "comments",index:'comments',search:true, width: 100 },
        { name: "reviewed",index:'reviewed',search:true,hidedlg:true, width: 60, align: "right" }
    ],
        pager: "#pager",
        rowNum: 10,
        rowList: [10,20,30],
        autowidth:true,
        sortname: "id",
        sortorder: "asc",
        viewrecords: true,
        gridview: true,
        autoencode: true,
        caption: "Appointment Request",

        afterInsertRow: function (id, currentData, jsondata) {
     if(currentData.reviewed == 1){ 
            $('#list').jqGrid('delRowData',id);
  }

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

下面是填充我的网格的 JSON 数据的示例。这是在服务器端完成的。 $cipher->decryptThis() 是一种解密算法,用于从数据库中解密加密字段。

$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'];
    $responce->rows[$i]['cell']=array($row['id'],$row['date'],$row['referralType'],$cipher->decryptThis($row['patientName']),$cipher->decryptThis($row['patientAddress']),$cipher->decryptThis($row['patientDOB']),$row['referralProvider'], $cipher->decryptThis($row['referralReason']),$cipher->decryptThis($row['contactName'])
    ,$cipher->decryptThis($row['contactPhone']),$cipher->decryptThis($row['contactEmail']), $row['contactFax'],$row['preferredTime'],$row['comments'], 
    $row['reviewed']  );
    $i++;
}
echo json_encode($responce);

最佳答案

执行此操作的最佳方法是按我的案例已审核中感兴趣的领域对数据进行分组。这段代码就达到了目的。将其插入网格标题之前。

grouping:true,
            groupingView : {
            groupField : ['reviewed'],
            groupColumnShow : [true],
            groupText:['<b>{0} - {1} Item(s)</b>'],
            groupCollapse : true,
            groupOrder: ['desc']
            },

关于php - JqG​​rid 服务器端查询与搜索冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20982342/

相关文章:

我认为 MySQL 使用了错误的索引

mysql - MySQL 查询速度慢——可能是索引问题?

php - mysql-通过id从一个表中选择一个产品,然后从另一个表中选择它的关联图像

php - 如何在 INSERT-SELECT 查询中返回 WHERE 语句中存在的条件

javascript - 简单的 Javascript 或 jQuery clearInterval 问题

javascript - 如何为每个div追加div

php - 使用 die() 时 SQL 语句中的结果数量不同

php - 在 WooCommerce 中更新自定义订单项元

php - 使用 getopt 捕获意外选项

javascript - 使用jquery选择器计算html表中的列和行总和