今天說下基於datagrid架構的查詢,後續會有刪除、添加與修改:
這一系列筆記以easyui的架構使用為主,後台代碼為輔,如有需要,我會盡量抽時間寫後台;
無需多言,直接代碼(希望不要再因為我把注釋大部分寫到代碼中而給我踢出首頁):
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="jquery-easyui-1.3.2/themes/default/easyui.css" rel="stylesheet" />
<link href="jquery-easyui-1.3.2/themes/icon.css" rel="stylesheet" />
<script src="jquery-easyui-1.3.2/jquery-1.8.0.min.js"></script>
<script src="JavaScript.js"></script><!--這個是擴充Jquery的方法-->
<script src="jquery-easyui-1.3.2/jquery.easyui.min.js"></script>
<script src="jquery-easyui-1.3.2/locale/easyui-lang-zh_CN.js"></script>
</head>
<body id="layoutbody" class="easyui-layout">
<div data-options="region:'north',title:'North Title',split:true" style="height: 100px;">
</div>
<div data-options="region:'south',title:'South Title',split:true" style="height: 100px;">
</div>
<div data-options="region:'east',iconCls:'icon-reload',title:'East',split:true" style="width: 100px;">
</div>
<div data-options="region:'west',title:'West',split:true" style="width: 100px;">
</div>
<div data-options="region:'center',title:'center title'" href="CenterPage.html" style="background: #eee; overflow: hidden;">
</div>
</body>
然後是CenterPage.html架構中的代碼,也就是tabs架構中的代碼:
<script type="text/javascript" charst="utf-8">
//因為layout架構指向href時,只取html頁面body中間的部分,所以該頁面這樣寫即可
//有datagrid包含屬性較多,所以盡量使用js的方式初始化datagrid架構
$(function () {
$("#dg").datagrid({
url: "GetJson.ashx", //指向一個一般處理常式或者一個控制器,返回資料要求是Json格式,直接賦值Json格式資料也可,我以demo中內建的Json資料為例,就不寫後台代碼了,但是我會說下後台返回的注意事項
iconCls: "icon-add",
fitColumns: false, //設定為true將自動使列適應表格寬度以防止出現水平滾動,false則自動匹配大小
//toolbar設定表格頂部的工具列,以數組形式設定
idField: 'id', //識別欄位,一般設為id,可能會區分大小寫,大家注意一下
loadMsg: "正在努力為您載入資料", //載入資料時向使用者展示的語句
pagination: true, //顯示最下端的分頁工具列
rownumbers: true, //顯示行數 1,2,3,4...
pageSize: 10, //讀取分頁條數,即向後台讀取資料時傳過去的值
pageList: [10, 20, 30], //可以調整每頁顯示的資料,即調整pageSize每次向後台請求資料時的資料
//由於datagrid的屬性過多,我就不每個都介紹了,如有需要,可以看它的API
sortName: "name", //初始化表格時依據的排序 欄位 必須和資料庫中的欄位名稱相同
sortOrder: "asc", //正序
columns: [[
{ field: 'code', title: 'Code', width: 100 },
{ field: 'name', title: 'Name', width: 100, sortable: true },//sortable:true點擊該列的時候可以改變升降序
{ field: 'addr', title: 'addr', width: 100 }
]],//這裡之所以有兩個方括弧,是因為可以做成水晶報表形式,具體可看demo
toolbar: [{//在dategrid表單的頭部添加按鈕
text: "添加",
iconCls: "icon-add",
handler: function () {
}
}, '-', {//'-'就是在兩個按鈕的中間加一個豎線分割,看著舒服
text: "刪除",
iconCls: "icon-remove",
handler: function () {
}
}, '-', {
text: "修改",
iconCls: "icon-edit",
handler: function () {
}
}, '-']
});
});
//點擊尋找按鈕出發事件
function searchFunc() {
alert("123");
$("#dg").datagrid("load", sy.serializeObject($("#searchForm").form()));//將searchForm表單內的元素序列為對象傳遞到後台
}
//點擊清空按鈕出發事件
function clearSearch() {
$("#dg").datagrid("load", {});//重新載入資料,無填寫資料,向後台傳遞值則為空白
$("#searchForm").find("input").val("");//找到form表單下的所有input標籤並清空
}
</script>
<div class="easyui-tabs" fit="true" border="false">
<div title="資料展示表格" border="false" fit="true">
<div class="easyui-layout" fit="true" border="false">
<!--由於查詢需要輸入條件,但是以toolbar的形式不好,所以我們在Layout架構的頭部north中書寫查詢的相關資訊-->
<!-- 這裡我們盡量使其展示的樣式與toolbar的樣式相似,所以我們先尋找toolbar的樣式,並複製過來-->
<div data-options="region:'north',title:'進階查詢'" style="height: 100px; background: #F4F4F4;">
<form id="searchForm">
<table>
<tr>
<th>使用者姓名:</th>
<td>
<input name="name" /></td>
</tr>
<tr>
<th>建立開始時間</th>
<td>
<input class="easyui-datetimebox" editable="false" name="subStartTime" /></td>
<!--由於datebox架構上面的資料必須是時間格式的,所以我們用editable="false"來禁止使用者手動輸入,以免報錯-->
<th>建立結束時間</th>
<td>
<input class="easyui-datetimebox" editable="false" name="nsubEndTimeame" /></td>
<td><a class="easyui-linkbutton" href="javascript:void(0);" onclick="searchFunc();">尋找</a></td>
<td><a class="easyui-linkbutton" href="javascript:void(0);" onclick="clearSearch();">清空</a></td>
</tr>
</table>
</form>
</div>
<div data-options="region:'center',split:false">
<table id="dg">
</table>
</div>
</div>
</div>
</div>
Jquery的擴充代碼:
var sy = $.extend({}, sy);/*定義一個全域變數*/
sy.serializeObject = function (form) { /*將form表單內的元素序列化為對象,擴充Jquery的一個方法*/
var o = {};
$.each(form.serializeArray(), function (index) {
if (o[this['name']]) {
o[this['name']] = o[this['name']] + "," + this['value'];
} else {
o[this['name']] = this['value'];
}
});
return o;
};
圖示: