jQuery EasyUI datagrid動態查詢數據
該插件組小巧使用方便,以下是一個從前台提交查詢條件,從MSSQL返回json數據的一個事例
HTML前端代碼
<?php
include_once("auth.php");
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="/inc/js/EasyUI/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="/inc/js/EasyUI/themes/icon.css">
<script type="text/javascript" src="/inc/js/EasyUI/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="/inc/js/EasyUI/jquery.easyui.min.js"></script>
<script>
function FindData(){
$('#mytable').datagrid('load',{
PersonCode:$('#PersonCode').val(),
KQYM:$('#KQYM').val()}
);
}
</script>
</head>
<body>
<table id='mytable' class="easyui-datagrid"
url="loadgriddata_get.php" title="請輸入查詢條件"
rownumbers="true" toolbar="#searchtool" loadMsg="正在查詢...">
<thead>
<tr>
<th field="PersonCode" Width="80">工號</th>
<th field="MyName" width="80">姓名</th>
<th field="KQDate" width="100">考勤日期</th>
<th field="MyWeek" width="80">星期</th>
<th field="KQMemo" width="200">打卡時間</th>
</tr>
</thead>
</table>
<div id="searchtool" >
<span>工號:</span><input type="text" id="PersonCode" value="" size=10 />
<span>考勤年月:</span><input type="text" id="KQYM" value="" size=10 />
<a href="javascript:FindData()" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查詢</a>
<div>
</body>
</html>
以下是取數據集,並將數據組裝成json對象返回給前台的php代碼
<?php
include_once("auth.php");
include_once("inc/ms_conn.php");
include_once("inc/comm_function.php");
$PersonCode=$_POST["PersonCode"]; //前端傳來的參數
$KQYM=$_POST["KQYM"];
$sqlstr="Exec dbo.HR_Prg_GetPersonYMKQ2 '$KQYM','$PersonCode'";
$rs =mssqlquery($sqlstr); //自定義的mssql方法,類擬mssql_query方法
$row = mssql_num_rows($rs); //取行總行數
$result["total"] = $row;
$items =array();
while ($row = mssql_fetch_array($rs)){
foreach($row as $key=>$value){
//這裡很重要,php的json_encode只支持utf-8,否則含漢字字段值會被置為null
$row[$key]=iconv('gb2312','UTF-8',$row[$key]); }
array_push($items, $row); }
$result["rows"] =$items;
echo json_encode($result);
?>
以下為效果圖