easyUI 数据表格-datagrid
2015年9月28日
1 目标:数据的表格展示,网页版excel(具有所有功能)。
2 原理:table的扩展封装。
3 流程:创建table,设置为easyui-datagrid。设置属性和数据。
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<!-- easyUI library -->
<linkrel="stylesheet" type="text/css"
href="easyui/jquery-easyui-1.4.3/themes/default/easyui.css"/>
<linkrel="stylesheet" type="text/css"
href="easyui/jquery-easyui-1.4.3/themes/icon.css" />
<!-- add defaulticons.@author:sf2gis@163.com @date:2015-9-23 14:19 -->
<linkrel="stylesheet" type="text/css"
href="easyui/jquery-easyui-1.4.3/themes/color.css"/>
<scripttype="text/javascript"
src="easyui/jquery-easyui-1.4.3/jquery.min.js"></script>
<scripttype="text/javascript"
src="easyui/jquery-easyui-1.4.3/jquery.easyui.min.js"></script>
<scripttype="text/javascript"
src="easyui/jquery-easyui-1.4.3/locale/easyui-lang-zh_CN.js"></script>
<title>This is a testpage for easyUI</title>
</head>
<body>
<table class="easyui-datagrid"data-options="fitColumns:true,singleSelect:true"style="width:200px;">
<thead>
<tr>
<thdata-options="field:'code'">code</th>
<thdata-options="field:'name'">name</th>
<thdata-options="field:'price'">price</th>
</tr>
</thead>
<tbody>
<tr>
<td>01</td>
<td>02</td>
<td>03</td>
</tr>
<tr>
<td>11</td>
<td>12</td>
<td>13</td>
</tr>
</tbody>
</table>
</body>
</html>
4 方法
4.1 创建datagrid控件:填充table标签创建或使用js创建(推荐)。
由于JS创建能够设置复杂的样式,推荐使用js创建。
4.1.1直接填充table标签,生成easyui-datagrid样式
参见:流程:创建table,设置为easyui-datagrid。设置属性和数据。
4.1.2使用table标签的thead构建样式,使用JSON文件填充数据。
示例:使用table标签构建样式。
//easyUITest.html
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<!-- easyUI library -->
<linkrel="stylesheet" type="text/css"
href="easyui/jquery-easyui-1.4.3/themes/default/easyui.css"/>
<linkrel="stylesheet" type="text/css"
href="easyui/jquery-easyui-1.4.3/themes/icon.css"/>
<!-- add defaulticons.@author:sf2gis@163.com @date:2015-9-23 14:19 -->
<linkrel="stylesheet" type="text/css"
href="easyui/jquery-easyui-1.4.3/themes/color.css"/>
<scripttype="text/javascript"
src="easyui/jquery-easyui-1.4.3/jquery.min.js"></script>
<scripttype="text/javascript"
src="easyui/jquery-easyui-1.4.3/jquery.easyui.min.js"></script>
<scripttype="text/javascript"
src="easyui/jquery-easyui-1.4.3/locale/easyui-lang-zh_CN.js"></script>
<scripttype="text/javascript"src="js/highcharts.js"></script>
<scripttype="text/javascript"src="js/highcharts-3d.js"></script>
<title>This is a testpage for easyUI</title>
</head>
<body>
<table id="dg" class="easyui-datagrid"
data-options="fitColumns:true,singleSelect:true,url:'/thbd/pages/BusinessStatistics/clsxxtj_data.json'"
style="width: 100%;">
<thead>
<tr>
<thdata-options="field:'cph'" style="width: 100px;">车牌号</th>
<thdata-options="field:'cpys'" style="width: 100px;">车牌颜色</th>
<thdata-options="field:'ssqy'" style="width: 100px;">所属企业</th>
<thdata-options="field:'sxzs'" style="width: 100px;">上线总数</th>
<thdata-options="field:'xxzs'" style="width: 100px;">下线总数</th>
<thdata-options="field:'cz',formatter:czRowFormatter"
style="width:100px;">操作</th>
</tr>
</thead>
</table>
<script type="text/javascript">
//format cell to <a> with blue color and underline
function czRowFormatter(value, row, index) {
return "<a href=''style='color:blue;text-decoration:underline;'>"
+ value + "</a>";
}
</script>
</body>
</html>
//clsxxtj_data.json
{"total":4,"rows":[
{"cph":"京APB-25415","cpys":"黄色","ssqy":"天绘北斗","sxzs":"1","xxzs":"36","cz":"查看"},
{"cph":"京APB-25415","cpys":"黄色","ssqy":"天绘北斗","sxzs":"1","xxzs":"36","cz":"查看"},
{"cph":"京APB-25415","cpys":"黄色","ssqy":"天绘北斗","sxzs":"1","xxzs":"36","cz":"查看"},
{"cph":"京APB-25415","cpys":"黄色","ssqy":"天绘北斗","sxzs":"0","xxzs":"36","cz":"查看"}
]}
4.1.3使用js构建样式,填充数据(推荐)。
方法:在html中构建table,使用js设置table的样式和数据。
设置样式Columns:参见复杂表头:多栏表头设计
示例:使用js创建表格。
//easyUITest.html
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<!-- easyUI library -->
<linkrel="stylesheet" type="text/css"
href="easyui/jquery-easyui-1.4.3/themes/default/easyui.css"/>
<linkrel="stylesheet" type="text/css"
href="easyui/jquery-easyui-1.4.3/themes/icon.css"/>
<!-- add defaulticons.@author:sf2gis@163.com @date:2015-9-23 14:19 -->
<linkrel="stylesheet" type="text/css"
href="easyui/jquery-easyui-1.4.3/themes/color.css"/>
<scripttype="text/javascript"
src="easyui/jquery-easyui-1.4.3/jquery.min.js"></script>
<scripttype="text/javascript"
src="easyui/jquery-easyui-1.4.3/jquery.easyui.min.js"></script>
<scripttype="text/javascript"
src="easyui/jquery-easyui-1.4.3/locale/easyui-lang-zh_CN.js"></script>
<scripttype="text/javascript"src="js/highcharts.js"></script>
<scripttype="text/javascript"src="js/highcharts-3d.js"></script>
<title>This is a testpage for easyUI</title>
</head>
<body>
<table id="dg" class="easyui-datagrid"style="width: 100%;"></table>
<script type="text/javascript">
$(document).ready(function() {
$('#dg').datagrid({
url:'/thbd/pages/BusinessStatistics/clsxxtj_data.json',
fitColumns:true,
columns:[[
{field:'cph',title:'车牌号',width:100},
{field:'cpys',title:'车牌颜色',width:100},
{field:'ssqy',title:'所属企业',width:100},
{field:'sxzs',title:'上线总数',width:100},
{field:'xxzs',title:'下线总数',width:100},
{field:'cz',title:'车牌号',width:100,formatter:czRowFormatter}
]]
});
});