@songlaf
2017-11-26T15:22:00.000000Z
字数 5224
阅读 693
一直很想找一个比较合适的DataTable控件,用过EasyUI、JqGrid等等,发现需要功能都不完全,最后觉得还是自己写一个吧,这样想要什么功能自己扩充就成了, 于是就写了一个BhTable,不想在界面上浪费太多时间,所以选择了在免费的AdminLTE主题下面进行开发。
打开可以加入我QQ群(552596594),进行讨论,有任何需求可以提出来,希望将来能做的更好。
<!--boostrap的css-->
<link href="<c:url value="/resources/plugins/bootstrap/css/bootstrap.min.css" />" rel="stylesheet">
<!--font-awesome-->
<link href="<c:url value="/resources/plugins/font-awesome/css/font-awesome.min.css" />" rel="stylesheet">
<!--adminlet-->
<link href="<c:url value="/resources/dist/css/AdminLTE.min.css" />" rel="stylesheet">
<link href="<c:url value="/resources/dist/css/skins/_all-skins.css" />" rel="stylesheet">
<link href="<c:url value="/resources/dist/css/bh.css" />" rel="stylesheet">
引入js
<script src="<c:url value="/resources/plugins/jquery/jquery-3.2.1.js" />"></script>
<script src="<c:url value="/resources/plugins/bootstrap/js/bootstrap.min.js" />"></script>
<script src="<c:url value="/resources/dist/js/adminlte.min.js" />"></script>
<script src="<c:url value="/resources/dist/js/bhCheck.js" />"></script>
<script src="<c:url value="/resources/dist/js/bh.js" />"></script>
在html页面写一个
<div id="bhTable"></div>
javascript
$("#bhTable").bhTable({
url:"listByPage",
width:1100,
columns: [
{name: 'id',title: "序号",width: 50,key: true, sotrable:true},
{name: 'code', title: "编码",width: 100,sotrable:true},
{name: 'name', title: "名称", width: 200, sotrable:true},
{name: 'categoryName', title: "类别",sortField:'category_code',sotrable:true, width: 100},
{name: 'price', title: "单价",width: 50, sotrable:true},
{name: 'qty', title: "库存数量", width: 100, sotrable:true},
{name: 'description', title: "备注", width: 400, sotrable: false}
],
searchDiv:$("#gridSearch"),
});
属性说明:
1. url,后台的查询路径
2. width,显示的宽度
3. columns显示的列
列的内容:
name:字段名称
titile:列的标题
width:列的宽度
sortable:是否支持点击排序
sortField:排序的字段,当排序的字段名称和列的名称不一样的时候使用,比如显示的字段是CategoryName,但是排序的字段是cateGoryCode
4.searchDiv:查询条件的div,如果不需要查询条件可以不写,
searchdiv的内容如下:
<div id="gridSearch">
<form class="form-inline">
<div class="form-group">
<label for="txtQueryCode">编码</label>
<input type="text" class="form-control input-sm" id="txtQueryCode" name="code">
</div>
<div class="form-group">
<label for="txtQueryName">名称</label>
<input type="text" class="form-control input-sm" id="txtQueryName" name="name">
</div>
<button type="button" class="btn btn-default btn-sm bg-blue bh-table-btn-query"><i class="fa fa-search"></i></button>
</form>
</div>
传递到后台的查询json数组如下
{
pageSize:10,//默认的分页大小
pageNow:1,//当前页面
name:"ddddd",//serachaDiv输入的查询条件
code:"aaaa",//serachaDiv输入的查询条件,
sortString:'code asc,name desc'//排序条件
}
参考我的Demo程序里面的Mapper文件的listByPage,
<select id="listByPage" parameterType="com.bh.demo.model.Product" resultMap="BaseResultMap">
SELECT product.id,product.code,product.category_code,product_category.name as category_name,product.name,
product.price,product.qty,product.description
from
product
join
product_category on product_category.code = product.category_code
<where>
<if test="code!=null && code != ''">
and product.code=#{code}
</if>
<if test="name!=null && name != ''">
and product.name like concat('%',#{name},'%')
</if>
</where>
<if test="sortString!=null && sortString != ''">
order by ${sortString}
</if>
</select>
后台返回的数据如下:
返回结果是根据pagehelper插件来定义的
pages:100, //页数
firstPage:true, //当前是否第一页,
hasNextPage:true, //是否有有下一页
hasPreviousPage:false,//是否有前一页
isFirstPage:true,//是否第一页
isLastPage: false,//是否最后一页
lastPage: 100,//最有页数
nextPage: 2,//下一页的页数
pageNow: 1,//当前页
startRow: 1,//开始行
endRow:100, //最后行
total: 10000,//所有行
list:[ //数据
{id:1,name:"abc"},
{id:2,name:"def"}
]
$("#bhTable").bhTable({
url:"listByPage",
height: 400,
width:1100,
columns: [
{name: 'id',title: "序号",width: 50,key: true, sotrable:true},
{name: 'code', title: "编码",width: 100,sotrable:true},
{name: 'name', title: "名称", width: 200, sotrable:true},
{name: 'categoryName', title: "类别", index: 'categoryName',sortField:'category_code',sotrable:true, width: 100},
{name: 'price', title: "单价",width: 50, sotrable:true},
{name: 'qty', title: "库存数量", width: 100, sotrable:true},
{name: 'description', title: "备注", width: 400, sotrable: false}
],
rowEditColumn:true,
checkColumn:true,
searchDiv:$("#gridSearch"),
buttons:[{tag:"add",show:true,click:function () {
layer.alert('你点击了增加操作',{icon:1});
}},
{tag:"edit",show:true,click:function (rows) {
layer.alert('你点击了编辑操作,选择的记录查看rows数组',{icon:2});
console.log(rows);
}},
{tag:"delete",show:true,click:function (rows) {
layer.alert('你点击了删除操作,选择的记录查看rows数组',{icon:3});
console.log(rows);
}},
{tag:"refresh",show:true}],
rowButtons:[{tag:"edit",show:true,click:function (row,index) {
layer.alert('你点击了edit操作,选择的记录查看rows数组,点击的记录是参数的row,序号是参数的index',{icon:4});
console.log(row);
console.log(index);
}},{tag:"delete",show:true,click:function (row,index) {
layer.alert('你点击了delete操作,选择的记录查看rows数组,点击的记录是参数的row,序号是参数的index',{icon:4});
console.log(row);
console.log(index);
}}]
});
1.1)底部的导航条的代码
buttons:[{tag:"add",show:true,click:function () {
layer.alert('你点击了增加操作',{icon:1});
}},
{tag:"edit",show:true,click:function (rows) {
layer.alert('你点击了编辑操作,选择的记录查看rows数组',{icon:2});
console.log(rows);
}},
{tag:"delete",show:true,click:function (rows) {
layer.alert('你点击了删除操作,选择的记录查看rows数组',{icon:3});
console.log(rows);
}},
{tag:"refresh",show:true}],
tag:表示按钮的标志位,默认有四个标志位add,delete,update,refresh,可以自定义,具体的请看高级功能
show:表示这个功能是否显示
click:当按钮点击的时候执行的方法
注意这个方法的使用,带一个rows的参数,rows是当前grid选择的数据。
例如下列的代码
{tag:"delete",show:true,click:function (rows) {
layer.alert('你点击了删除操作,选择的记录查看rows数组',{icon:3});
console.log(rows);
}},
layer我用的layer的插件,大家可以下看看,国人写的,很不错,我的目标,哈哈。