newscategory.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. layui.use(['laypage', 'layer', 'table', 'element', 'form'], function () {
  2. var laypage = layui.laypage //分页
  3. , layer = layui.layer //弹层
  4. , table = layui.table //表格
  5. , element = layui.element //元素操作
  6. , form = layui.form
  7. var tableIds = 'newsCategoryList';
  8. //第一个实例
  9. table.render({
  10. elem: '#' + tableIds
  11. , url: '/dictionary/newsform' //数据接口
  12. , where: {token: csrfToken}
  13. , method: 'post'
  14. , page: true //开启分页
  15. , height: 600
  16. , text: '数据加载中'
  17. , cols: [[ //表头
  18. // {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left',align:'center'},
  19. {field: 'news_name', title: '资讯名称', align: 'center'}
  20. , {field: 'sort', title: '排序', width: 80, edit: 'text', align: 'center', sort: true, templet: "#sortBox"}
  21. // ,{field: 'state', title: '状态', width:80,align:'center'}
  22. , {
  23. field: 'state',
  24. title: '是否下架',
  25. width: 100,
  26. align: 'center',
  27. templet: "#Toexamine",
  28. templet: function (d) {
  29. if (d.state == 2) {
  30. return '<input type="checkbox" value="' + d.id + '" name="' + d.state + '" lay-skin="switch" lay-filter="switchTest" lay-text="|">'
  31. } else {
  32. return '<input type="checkbox" checked value="' + d.id + '" name="' + d.state + '" lay-skin="switch" lay-filter="switchTest" lay-text="|">'
  33. }
  34. }
  35. }
  36. // ,{field: 'recommend', title: '是否推荐', width:120,align:'center'}
  37. , {field: 'create_at', title: '添加时间', width: 150, align: 'center'}
  38. , {field: 'update_at', title: '更新时间', width: 150, align: 'center'}
  39. , {fixed: 'right', title: '操作', width: 165, align: 'center', toolbar: '#operate'}
  40. ]]
  41. });
  42. //列表操作
  43. table.on('tool(' + tableIds + ')', function (obj) {
  44. var layEvent = obj.event,
  45. data = obj.data;
  46. if (layEvent === 'edit') {
  47. layer.open({
  48. type: 2,
  49. title: '修改户型',
  50. closeBtn: 1, //不显示关闭按钮
  51. shade: [0],
  52. area: ['600px', '500px'],
  53. anim: 2,
  54. content: ['/dictionary/newsedit?id=' + data.id, 'yes'], //iframe的url,no代表不显示滚动条
  55. });
  56. } else if (layEvent === 'del') {
  57. layer.confirm('您确定要删除吗?', {btn: ['确定', '取消']}, function () {
  58. $.post('/dictionary/newsdel', {'id': data.id, type: 'del'}, function (data) {
  59. if (data.code == 200) {
  60. layer.msg(data.msg);
  61. table.reload(tableIds);
  62. } else if (data.code == 300) {
  63. layer.msg(data.msg);
  64. }
  65. })
  66. })
  67. }
  68. });
  69. var $ = layui.$, active = {
  70. addData: function () { //获取选中数据
  71. layer.open({
  72. type: 2,
  73. title: '添加资讯分类',
  74. closeBtn: 1, //不显示关闭按钮
  75. shade: [0],
  76. area: ['600px', '500px'],
  77. anim: 2,
  78. content: ['/dictionary/newsadd', 'yes'], //iframe的url,no代表不显示滚动条
  79. });
  80. // var checkStatus = table.checkStatus('idTest')
  81. // ,data = checkStatus.data;
  82. // layer.alert(JSON.stringify(data));
  83. }
  84. };
  85. $('.demoTable .layui-btn').on('click', function () {
  86. var type = $(this).data('type');
  87. active[type] ? active[type].call(this) : '';
  88. });
  89. //显示或隐藏
  90. form.on('switch(switchTest)', function (data) {
  91. var state = '';
  92. if (data.elem.name == 1) {
  93. state = 2;
  94. } else if (data.elem.name == 2) {
  95. state = 1;
  96. }
  97. layer.confirm('确定要修改此操作吗?', {btn: ['确定', '取消']}, function () {
  98. $.post('/dictionary/newsdel', {id: data.value, type: 'state', state: state}, function (data) {
  99. if (data.data != null) {
  100. layer.msg(data.msg);
  101. return false;
  102. }
  103. layer.msg(data.msg);
  104. table.reload(tableIds);
  105. })
  106. }, function (aa) {
  107. table.reload(tableIds);
  108. })
  109. });
  110. table.on('edit(' + tableIds + ')', function (obj) {
  111. var data = obj.data;
  112. if (obj.field == "sort") {
  113. $.post('/dictionary/newsdel', {id: data.id, sort: data.sort, type: 'sort'}, function (data) {
  114. if (data.code == 200) {
  115. layer.msg(data.msg);
  116. } else if (data.code == 300) {
  117. layer.msg(data.msg);
  118. }
  119. })
  120. }
  121. })
  122. //修改
  123. form.on('submit(editnews)', function (data) {
  124. $.post('/dictionary/newseditform', {'data': data.field, 'token': csrfToken}, function (data) {
  125. if (data.code == 200) {
  126. layer.msg(data.msg);
  127. parent.layui.table.reload(tableIds);
  128. TableClose();
  129. } else if (data.code == 300) {
  130. layer.msg(data.msg);
  131. }
  132. })
  133. return false;
  134. });
  135. //添加
  136. form.on('submit(addnews)', function (data) {
  137. $.post('/dictionary/newsaddform', {'data': data.field, 'token': csrfToken}, function (data) {
  138. if (data.code == 200) {
  139. layer.msg(data.msg);
  140. parent.layui.table.reload(tableIds);
  141. TableClose();
  142. } else if (data.code == 300) {
  143. layer.msg(data.msg);
  144. }
  145. })
  146. return false;
  147. });
  148. });