1、需要指定datatables的ID

 <button class="btn  btn-primary" id="newAttribute">新增证照属性</button>
<table class="table table-striped table-bordered table-hover dataTable myTable" id="newAttributeTable" width="100%" role="grid" aria-describedby="editable_info">
</table>

2、初始化datatables

<script type="text/javascript">
$(function() {
//初始化证照属性设置表
initnewAttributeTable();
}); //证照属性设置表 function initnewAttributeTable() {
var table = $('#newAttributeTable').dataTable({
"paging": false, //翻页
//"bPaginate" : true, //显示分页器
//"iDisplayLength":28,
"ordering": false,
"searching": false, //搜索框
//"bLengthChange":true,//改变每行显示数据数量
//"aLengthMenu": [[10, 30, 50, -1], [10, 30, 50, "All"]],
"bInfo": false, //页脚
'bStateSave': true,
"bAutoWidth":false,
"bSort": true,
"processing": false,
"serverSide": false,
"sServerMethod": "get", //post
"bDestroy": true,
"ajax": {
"url": encodeURI("${pageContext.request.contextPath}/ajax/certificate/getCertificateTypeList"),
//"dataSrc": "data",//默认为data
"type": "get",
"error": function () {
}
},
"columns": [{
"data": null,
"title": "序号",
"sWidth":"30px",
"sClass": "text-tables-center",
"createdCell": function (nTd, sData, oData, iRow, iCol) {
var startnum = this.api().page() * (this.api().page.info().length);
$(nTd).html(iRow + 1 + startnum); //分页行号累加:$(nTd).html(iRow+1);
}
}, {
"data": "typeName",
"title": "证照属性设置"
},{
"data": null,
"title": "操作"
}],
"columnDefs": [
{
"targets": [1],
"data": "typeName",
"render": function (data, type, row) {
//isLockStatus:true:显示有锁 false:显示没有锁
if(row.isLockStatus ==true){
return ""+row.typeName+"&nbsp;&nbsp;<i class='fa fa-lock l-fa' style='cursor: pointer'></i>";
}else{
return row.typeName;
}
}
},
{
"targets": [2],
"data": "id",
"sWidth":"60px",
"sClass": "text-tables-center",
"render": function (data, type, row) {
var id= row.id ==undefined ? -1:row.id ;//-1说明是新增,其余表示为编辑
if(row.isLockStatus ==true){
return "-";
}else{
return "<i class='fa fa-pencil l-fa edit-btn' style='cursor: pointer' id=\'" + id + "\'></i>&nbsp;&nbsp;<i class='fa fa-trash-o m-fa del-btn' style='cursor: pointer' id=\'" + id + "\'></i>";
}
}
}, {
sDefaultContent: '',
aTargets: ['_all']
}]
}); //新增一行表格
$("#newAttribute").unbind('click').click(function () {
if ($("#newAttributeTable tbody tr:last td i").eq(0).attr('id') == -1) {
return;
}
$('#newAttributeTable').dataTable().fnAddData([]);
var nTrsLength = $("#newAttributeTable tbody tr").length;
var nTrs = $("#newAttributeTable tbody tr").eq(nTrsLength - 1);
var nIs = nTrs.find("td").eq(2).find("i").eq(0)
nIs.click();
});
//点击编辑图标出现编辑框
$("#newAttributeTable tbody").unbind('click').on("click", ".edit-btn", function () {
var tds = $(this).parents("tr").children();
var i;
$.each(tds, function (i, val) {
var jqob = $(val);
var txt = jqob.text();
if (i == 1) {
var put = $("<input type='text' class='form-control' style='width:100%'>");
put.val(txt);
jqob.html(put);
}
});
$(this).toggleClass("edit-btn fa-pencil");
$(this).toggleClass("save-btn fa-save");
});
//保存按钮
$("#newAttributeTable tbody").on("click", ".save-btn", function () {
var id =$(this).attr("id");
var tds = $(this).parents("tr").children();
var sTds = tds.length;
var lisval = [];
for (var i = 0; i < sTds; i++) {
if (i < sTds - 1)
lisval.push(tds.eq(i).children("input").val());
else lisval.push(tds.eq(i).children("input").val())
}
if (i == sTds - 1) lisval.push();
if (lisval[1] == "") {
toNotification("警告!", "证照属性设置不能为空!", "warning");
return false;
} else if(lisval[1].length >10){
toNotification("警告!", "证照属性设置不能超过十个字符!", "warning");
return false;
}else {
$.each(tds, function (i, val) {
var jqob = $(val);
//把input变为字符串
if (!jqob.has('i').length) {
var txt = jqob.children("input").val();
jqob.html(txt);
}
});
$(this).toggleClass("edit-btn fa-pencil");
$(this).toggleClass("save-btn fa-save");
//保存数据
SaveData(lisval[1],id);
}
}); //证照属性设置保存信息
function SaveData(msg,id){
var data ={
id: id, //-1表示是新增
typeName: msg
}
var url = encodeURI("${pageContext.request.contextPath}/ajax/certificate/saveCertificateType");
actionRequest(data,url,function(data){
if (data.successed) {
toNotification("提示!","操作成功!", "success");
//刷新表格
initnewAttributeTable();
}else {
toNotification("操作失败!", data.message, "error");
//刷新表格
initnewAttributeTable();
}
});
} //证照属性设置删除
$("#newAttributeTable tbody").on("click", ".del-btn", function () {
var id =$(this).attr("id");
if (id==-1) {
var trs = $(this).parents("tr");
initnewAttributeTable();
return false;
}else{
var tds = $(this).parents("tr").children();
var data = {id: id };
var url="/ajax/certificate/deleteCertificateType";
//执行删除
toConfirm(data,url,function(data){
if (data.successed) {
swal({
title: "删除成功",
text: "<small>1秒后自动关闭<small>",
type: "success",
html: true,
timer: 1000
});
initnewAttributeTable();
} else {
swal("删除失败!", data.message, "error");
}
});
}
});
}
//获得标识
function getActionId() {
return new Date().getTime();
}
</script >

3、最终效果

最新文章

  1. redmine整合GIT版本库
  2. 大白话讲解Promise(三)搞懂jquery中的Promise
  3. .Net Core WebAPI 基于Task的同步&amp;异步编程快速入门
  4. STM32命名原则
  5. Java基础之写文件——使用带缓冲的Writer写文件(WriterOutputToFile)
  6. sqlserver根据id集合,批量插入。(巧用sqlserver内置函数)
  7. 【转】Eclipse配置Struts2问题:ClassNotFoundException: org...dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  8. [liu yanling]软件测试的分类
  9. Eclipse编译ijkplayer
  10. spoj PARTIT
  11. USACO Section 1.2 Transformations 解题报告
  12. BigDecimal 高精度计算 熟悉扩展,java除法保留小数问题
  13. AngularJS -- 提供者(Providers)
  14. app后端设计(11)-- 系统架构(2014.12.05更新)
  15. C#基础加强(7)之ref与out
  16. Android---Hellow World
  17. 008-Centos 7.x安装 Ambari 2.2.2 + HDP 2.4.2 搭建Hadoop集群
  18. django 模型类的常见字段约束,以及filter 过滤和查询
  19. js的RegExp
  20. 看看Spring的源码(二)——bean实例化

热门文章

  1. C#下的 Emgu CV
  2. Ubuntu 12.04 wine QQ
  3. 获取WINDOWS特殊文件夹
  4. android89 服务service
  5. java11 Guava:谷歌开发的集合库
  6. linux上gcc
  7. 轮播图-JavaScript
  8. 使用HttpURLConnection实现在android客户端和服务器之间传递对象
  9. Beyond REST: How to build a HATEOAS API in Java with Spring MVC, Jersey (JAX-RS) and VRaptor
  10. 微信公众号支付(一):获取用户openId