数据操作类:
<?php
class DBHelper
{
public $conn;
/**
* 得到数据库连接 成功返回true,失败返回false
* Enter description here ...
* @param unknown_type $user
* @param unknown_type $pwd
* @param unknown_type $dbname
* @param unknown_type $host
*/
function getConn($user,$pwd,$dbname,$host = "localhost")
{
$this->conn = mysql_connect($host,$user,$pwd);
if (!$this->conn)
{
return false;
}
mysql_select_db($dbname,$this->conn);
return true;
}
/**
* 返回查询的result
* @param unknown_type $table 待查询的表
* @param unknown_type $cols 待查询的列的数组 如array("col1","col2");
* 如果想给列 重命名 则使用 array("newcol1"=>"col1","newcol2"=>"col2") ;
* null 表示查询所有
* @param unknown_type $conditions where后的条件数组按如此格式来 array("col"=>array("> xx"=>"AND","< yy"=>"")) 表示 col 列 大于 xx
* 并且小于yy的结果 。array("col"=>array("LIKE 'xx%'"=>"")) 模糊搜索
* 注意xx,yy 为字符串时要用''扩住,中间的条件才能用AND 或者OR,末尾条件用""表示
* null 表示无条件
* @param unknown_type $order 排序数组 如 array("col1"=>"asc","col2"=>"desc"); null 表示默认排序
* @param unknown_type $limit 数据条目,如 array(1=>10) 表示limit 1,10; null 表示默认
*/
function getRow($table,$cols,$conditions,$order,$limit)
{
$sql = "SELECT ";
$tmp = "";
if($cols != null){
while((list($k,$v) = each($cols)) !== false)
{
$tmp =$tmp.$v.",";
if(!is_numeric($k)){
$tmp = substr($tmp, 0,strlen($tmp)-1)." as {$k},";
}
}
$tmp = substr($tmp, 0,strlen($tmp)-1)." FROM {$table}";
}
else {
$tmp = "* FROM {$table}";
}
$sql = $sql.$tmp;
if($conditions != null)
{
$tmp = " WHERE ";
while((list($k,$v) = each($conditions)) !== false)
{
if((list($k1,$v1) = each($v)) !== false){
$tmp = $tmp."{$k} {$k1} {$v1}";
}
}
$tmp = rtrim($tmp);
$sql = $sql.$tmp;
}
if($order != null){
$tmp = " ORDER BY ";
while((list($k,$v) = each($order)) !== false)
{
$tmp = $tmp."{$k} {$v},";
}
$tmp = substr($tmp, 0,strlen($tmp)-1);
$sql = $sql.$tmp;
}
if($limit != null){
$tmp = " limit ";
if((list($k,$v) = each($limit)) !== false){
$tmp = $tmp."{$k},{$v}";
}
$sql = $sql.$tmp;
}
//echo $sql;
$result = mysql_query($sql);
return $result;
}
function closeDb(){
if($this->conn != null)
mysql_close($this->conn);
}
/**
*
* 返回mysql标准时间字符串
* 注意这个date的获得和php的配置有关系,而php默认返回的UTC
* 如果不符合你的标准,需要在php.ini中将配置做相应的更改
*/
function getDateStr()
{
return date("Y-m-d H:i:s");
}
}
?>
response封装:
<?php
include 'dbhelper.php';
class Response{
public $total = 0;
public $records = 0;
public $page; // get the requested page
public $rows =array();
private $limit; // get how many rows we want to have into the grid
private $sidx; // get index row - i.e. user click to sort
private $sord; // get the direction
function Response($page,$rows,$sidx,$sord){
$this->page = $page;
$this->limit = $rows;
$this->sidx = $sidx;
$this->sord = $sord;
if(!$this->sidx)
$this->sidx = 1;
}
/**
* 通过指定条件查询将结果填入response中
* @param unknown_type $table 表名
* @param unknown_type $cols 待查询的列的数组 如array("col1","col2");
* 如果想给列 重命名 则使用 array("newcol1"=>"col1","newcol2"=>"col2") 为null 表示查询所有
* @param unknown_type $conditions where后的条件数组按如此格式来 array("col"=>array("> xx"=>"AND","< yy"=>"")) 表示 col 列 大于 xx
* 并且小于yy的结果。array("col"=>array("LIKE 'xx%'"=>"")) 模糊搜索
* 注意xx,yy 为字符串时要用''扩住,中间的条件才能用AND 或者OR,末尾条件用""表示
* null 表示无条件
* ;null 表示无条件
*/
public function fillResponse($table,$cols,$conditions){
$helper = new DBHelper();
if($helper->getConn("root", "root", "dbcd")){
$result = $helper->getRow($table, null, $conditions,null,null);
if($result){
$count = mysql_num_rows($result);
$this->records = $count;
if($count > 0){
$this->total = ceil($count/$this->limit);
}
else{
$this->total = 0;
}
if($this->page > $this->total)
$this->page = $this->total;
$start = $this->limit*$this->page - $this->limit;
if($start < 0) $start = 0;
$result = $helper->getRow($table, $cols, $conditions, array($this->sidx=>$this->sord), array($start=>$this->limit));
$i = 0;
while(($row=mysql_fetch_array($result,MYSQL_ASSOC))!=false){
$this->rows[$i] = $row;
$i++;
}
}
else{
// 查询失败
}
$helper->closeDb();
}
else {
// 未成功连接数据库
}
}
}
?>
使用:
include 'php/response.php';
$conditon = null;
$response = new Response($_GET['page'], $_GET['rows'], $_GET['sidx'], $_GET['sord']);
if(filter_has_var(INPUT_GET, "searchField")){
$conditon = array($_GET['searchField']=>array("LIKE '{$_GET['searchString']}%'"=>""));
}
$response->fillResponse("factory",null, $conditon);
print_r(json_encode($response));
As mentioned above posting search data differs from custom and toolbar searching. When the find button is clicked, jqGrid adds three parameters to the url (again with _search=true), in name=value pairs:
-
sField: the 'searchField', the value comes from the index in colModel
-
sValue: the 'searchString', the value is the entered value
-
sOper: the 'searchOper', the value is the type of search - see sopt array
For example if the field index is invid, operation is equal, and the searched value is 123, then the string that is posted to the server look like:
http://localhost/demo35/server.php?...&searchField=invid&searchString=123&searchOper=eq
详见:http://www.trirand.com/jqgridwiki/doku.php?id=wiki:singe_searching
分享到:
相关推荐
jqGrid表格数据修改删除代码.zip
代码简介:jqGrid表格内容查询读取代码是一款列表详细页表格布局,生成表格内容,表格查询代码等。
jqGrid表格内容查询读取代码是一款列表详细页表格布局,生成表格内容,表格查询代码等。
jqGrid表格数据展示插件,可以做出很多很炫酷的效果,你值得拥有
前端页面点击导出jqgrid表格为excel表格代码,有表格样式设置的代码,可以参考一下,这是我刚开始工作的时候写的,希望可以帮助像我一样的菜鸟。
jqGrid表格数据修改删除代码是一款jQuery基于jqGrid表格插件制作项目名称序号表格数据操作。
jqgrid表格,单元格合并,并显示,demo版本
jqGrid表格数据修改删除代码是一款jQuery基于jqGrid表格插件制作项目名称序号表格数据操作。
jqGrid表格插件——参数配置,使用举例
MVC4网站中集成jqGrid表格插件-示例源代码,包含基础basecontroller,以及5个controller
无刷新增删改查,网上都有免费下载,所以就不设置积分了。
; charset=utf-8"> <title>Insert title here ...<link type="text/css" rel="stylesheet" href="css/ui.jqgrid.css"> <script type="text/javascript" src="js/jquery-1.11.0.min.js" ></script> ...
jqgrid 表格的增删改查4种操作,以及表格分页,还有修改以及添加时候的modal弹出框。代码是个project项目文件,需要自己建个解决方案将项目包含进去。
本Demo利用Jquery的jqgrid插件利用本地资源数据,实现分页。
MVC4网站中集成jqGrid表格插件-示例源代码
之前网上没找到现成的jqgrid表格前台导出插件,都或多或少带后台,也不想换表格插件,所以参照extjs自己改了个前台导出插件,支持多表头和分组table带表头导出,引用方法文件有详细注释,直接能用,如有不足还请见谅...
jqGrid【表格】表格属性大全 Table表格对象 Table对象:表示HTML文档中的表,对于文档中每个标记,浏览器都创建一个Table对象.