decohelper.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. {extend name="public/layout" /} {block name="title"}风格测试{/block} {block name="body"}
  2. <body>
  3. <div class="layui-fluid">
  4. <div class="layui-card">
  5. <div class="layui-tab">
  6. </div>
  7. <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
  8. <legend>步骤结构及节点操作</legend>
  9. </fieldset>
  10. <div class="layui-card-body">
  11. <div id="treestruc" class="tree_struc"></div>
  12. <div style="padding: 10px 0;">
  13. <button class="layui-btn layuiadmin-btn-style" data-type="save">确认结构</button>
  14. </div>
  15. </div>
  16. <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
  17. <legend>步骤文章操作</legend>
  18. </fieldset>
  19. <div class="layui-card-body">
  20. <div style="padding-bottom: 10px;">
  21. <button class="layui-btn layuiadmin-btn-style" data-type="add">添加步骤文章内容</button>
  22. </div>
  23. <table id="decohelper-table" lay-filter="decohelper-table"></table>
  24. <script type="text/html" id="img">
  25. <img src="{{d.cover}}" style="width: 50px;height: 50px;"/>
  26. </script>
  27. <script type="text/html" id="table-style-list">
  28. {auth:check name="tool/decoPostEdit"}
  29. <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
  30. class="layui-icon layui-icon-edit"></i>编辑</a>
  31. {/auth:check}
  32. {auth:check name="tool/deco_del"}
  33. <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
  34. class="layui-icon layui-icon-delete"></i>删除</a>
  35. {/auth:check}
  36. </script>
  37. </div>
  38. </div>
  39. </div>
  40. {/block} {block name="js"}
  41. <script>
  42. layui.config({
  43. base : '__LAYUI__/' //静态资源所在路径
  44. ,
  45. urlbase: '/sys'
  46. }).extend({
  47. index: 'lib/index' //主入口模块
  48. }).use(['index','table','tree'], function () {
  49. var $ = layui.$,
  50. form = layui.form,
  51. tree = layui.tree,
  52. table = layui.table;
  53. //装修助手
  54. table.render({
  55. elem: '#decohelper-table',
  56. url: "{:url('tool/decohelperlist')}" //模拟接口
  57. ,
  58. cols: [
  59. [{
  60. type: 'numbers',
  61. width: 80,
  62. title: '序号',
  63. }, {
  64. field: 'cover',
  65. title: '图片',
  66. templet: '#img'
  67. }, {
  68. field: 'title',
  69. title: '标题'
  70. }, {
  71. field: 'typepath',
  72. title: '节点路径'
  73. }, {
  74. title: '操作',
  75. width: 150,
  76. align: 'center',
  77. fixed: 'right',
  78. toolbar: '#table-style-list'
  79. }]
  80. ],
  81. page: true,
  82. limit: 10,
  83. limits: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
  84. text: '对不起,加载出现异常!'
  85. });
  86. //监听工具条
  87. table.on('tool(decohelper-table)', function(obj) {
  88. var data = obj.data;
  89. if(obj.event === 'del') {
  90. layer.confirm('确定删除该节点文章吗?', function(index) {
  91. $.ajax({
  92. type: 'post',
  93. url: "{:url('tool/deco_del')}",
  94. data: {
  95. id: data.id
  96. },
  97. success: function(res) {
  98. if(res.code == 0) {
  99. layer.msg(res.msg, {
  100. anim: 0
  101. });
  102. } else {
  103. layer.msg(res.msg, {
  104. anim: 6
  105. });
  106. }
  107. }
  108. });
  109. obj.del();
  110. layer.close(index);
  111. });
  112. } else if(obj.event === 'edit') {
  113. console.log(data.title);
  114. layer.open({
  115. type: 2,
  116. title: '编辑',
  117. content: '{:url("tool/decoPostEdit")}?key=' + data.title,
  118. area: ['800px', '600px'],
  119. btn: ['确定', '取消'],
  120. yes: function(index, layero) {
  121. var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit-submit");
  122. submit.click();
  123. }
  124. })
  125. }
  126. });
  127. //监听搜索
  128. form.on('submit(styleTest-search)', function (data) {
  129. var field = data.field;
  130. //执行重载
  131. table.reload('decohelper-table', {
  132. where: field
  133. ,page: {curr:1}
  134. });
  135. });
  136. //事件
  137. var active = {
  138. save: function () {
  139. layer.confirm('确认保存结构?',function(res){
  140. var saveUrl = '{:url("tool/decoStrucSave")}';
  141. $.post(saveUrl, {
  142. data:JSON.stringify(datasource)
  143. }, function(res) {
  144. if (res.code != 0) {
  145. layer.msg(res.msg, { icon: 5, anim: 6, time: 1500 });
  146. }else{
  147. layer.msg(res.msg, { icon: 6 });
  148. }
  149. });
  150. });
  151. },
  152. add: function () {
  153. layer.open({
  154. type : 2,
  155. title : '添加',
  156. content: '{:url("tool/decoPostAdd")}',
  157. maxmin : true,
  158. area : ['800px', '600px'],
  159. btn : ['确定', '取消'],
  160. yes : function (index, layero) {
  161. //点击确认触发 iframe 内容中的按钮提交
  162. var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
  163. submit.click();
  164. }
  165. });
  166. }
  167. };
  168. $('.layui-btn.layuiadmin-btn-style').on('click', function () {
  169. var type = $(this).data('type');
  170. active[type] ? active[type].call(this) : '';
  171. });
  172. var datasource = JSON.parse('{$datastr|raw}');
  173. tree.render({
  174. elem : '#treestruc'
  175. , data : datasource
  176. , edit : ['add', 'update', 'del'] //操作节点的图标
  177. , click: function (obj) {
  178. //layer.msg(JSON.stringify(obj.data));
  179. console.log(obj);
  180. }
  181. ,operate: function(obj){
  182. var type = obj.type; //得到操作类型:add、edit、del
  183. var data = obj.data; //得到当前节点的数据
  184. var elem = obj.elem; //得到当前节点元素
  185. //Ajax 操作
  186. var id = data.id; //得到节点索引
  187. if(type === 'add'){ //增加节点
  188. //返回 key 值
  189. var newpoint = {'title':'未命名'};
  190. data.children.push(newpoint);
  191. } else if(type === 'update'){ //修改节点
  192. console.log(data);
  193. console.log(datasource);
  194. $.each(datasource, function (k,obj) {
  195. console.log(obj);
  196. if(obj.title === '未命名'){
  197. //alert(1);
  198. obj.title = data.title;
  199. return false;
  200. }else{
  201. if(obj.children){
  202. $.each(obj.children, function (key,child) {
  203. if(child.title === '未命名'){
  204. // alert(2);
  205. child.title = data.title;
  206. return false;
  207. }else{
  208. if(child.children){
  209. $.each(child.children, function (ind,endor) {
  210. if(endor.title === '未命名'){
  211. //alert(3);
  212. endor.title = data.title;
  213. return false;
  214. }
  215. });
  216. }
  217. }
  218. });
  219. }
  220. }
  221. });
  222. } else if(type === 'del'){ //删除节点
  223. // alert(data);
  224. console.log(data);
  225. $.each(datasource, function (k,obj) {
  226. console.log(obj);
  227. if(obj.title == data.title){
  228. return false;
  229. //alert(1);
  230. }else{
  231. if(obj.children){
  232. $.each(obj.children, function (key,child) {
  233. if(child.title == data.title){
  234. //alert(2);
  235. obj.children.splice(key, 1);
  236. return false;
  237. }else{
  238. if(child.children){
  239. $.each(child.children, function (ind,endor) {
  240. if(endor.title == data.title){
  241. //alert(3);
  242. child.children.splice(ind, 1);
  243. return false;
  244. }
  245. });
  246. }
  247. }
  248. });
  249. }
  250. }
  251. });
  252. console.log(datasource);
  253. };
  254. }
  255. });
  256. });
  257. </script>
  258. {/block}