一、利用datagrid load方法重新载入并传递查询参数.
load | param | 载入并显示第一页的记录,如果传递了'param'参数,它将会覆盖查询参数属性的值。 |
$('#dg').datagrid('load', { "searchKey": name, "searchValue": value });
$('#dg').datagrid('load', { "参数名": 值, "参数名2": 参数值});
当点击查询按钮send时触发
$(function(){
$("#send").click(function(){
$('#Dear').datagrid('load', { "searchKey":"cnname", "searchValue": $('#cnname).val()});
})
});
服务器端代码
include 'conn.php';
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$itemid = isset($_POST['searchKey']) ? mysql_real_escape_string($_POST['searchKey']) : '';
$offset = ($page-1)*$rows;
$result = array();
$where = "itemid like '$itemid%'";
$rs = mysql_query("select count(*) from item where " . $where);
$row = mysql_fetch_row($rs);
$result["total"] = $row[0];
$rs = mysql_query("select * from item where " . $where . " limit $offset,$rows");
$items = array();
while($row = mysql_fetch_object($rs)){
array_push($items, $row);
}
$result["rows"] = $items;
echo json_encode($result);
后台代码分析
1. 首先判断是否要生成查询数据,条件是传递参数$_POST['searchKey']存在且不为空 。
if(isset($_POST['searchValue']) and $_POST['searchValue']!="")
2. 采用like查询语言扩大查询范围,$map[$_POST['searchKey']]=array('like',array('%'.$_POST['searchValue'].'%'));生成的查询代码是:$_POST['searchKey']
like % $_POST['searchValue'] %
3. 生成的查询记录要符合datagrid的json数据格式。其中json数据的总记录用count()生成,需位于where条件之后。
注意:
reload | param | 重载记录,跟'load'方法一样但是重载的是当前页的记录而非第一页。 |
$('#Dear').datagrid('reload', { "searchKey": name, "searchValue": value });