list.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <script type="text/javascript" src="/static/js/jquery.min.js"></script>
  2. <script type="text/javascript" src="/static/zTree3/js/jquery.ztree.core.js"></script>
  3. <script type="text/javascript" src="/static/zTree3/js/jquery.ztree.excheck.js"></script>
  4. <script type="text/javascript" src="/static/zTree3/js/jquery.ztree.exedit.js"></script>
  5. <div class="layui-row">
  6. <div style="margin: 10px 20px">
  7. <blockquote class="layui-elem-quote">
  8. 说明:新增权限,请直接填写相关数据保存即可;修改和删除,请点击左侧权限树选择要修改的权限节点
  9. </blockquote>
  10. </div>
  11. <div class="layui-col-md5 zTreeDemoBackground" style="margin-left: 20px;margin-right: 0px">
  12. <ul id="treeDemo" class="ztree" style="height:320px; width: auto; margin-bottom: 10px;">
  13. </ul>
  14. </div>
  15. <div class="layui-col-md7">
  16. <div class="layui-layout layui-layout-admin" style="padding-left: 40px;margin-top: 20px;">
  17. <form class="layui-form" action="" onsubmit="javascript:;" name="form" method="post">
  18. <div class="layui-form-item">
  19. <label class="layui-form-label">权限名称</label>
  20. <div class="layui-input-inline">
  21. <input type="text" name="auth_name" lay-verify="required" autocomplete="off" placeholder="请输入权限名称" class="layui-input">
  22. </div>
  23. <div class="layui-form-mid layui-word-aux"></div>
  24. </div>
  25. <div class="layui-form-item">
  26. <label class="layui-form-label">上级权限</label>
  27. <div class="layui-input-inline" style="width: 210px;">
  28. <input type="text" name="pname" lay-verify="required" autocomplete="off" placeholder="请选择" disabled value="所有权限" class="layui-input" id="pname">
  29. </div>
  30. <div class="layui-input-inline" style="width: 50px;">
  31. <input type="text" name="pid" autocomplete="off" placeholder="0" id="pid" class="layui-input" readonly value="1">
  32. </div>
  33. <div class="layui-form-mid layui-word-aux">
  34. <button id="menuBtn" type="button" class="layui-btn layui-btn-xs layui-btn-normal" >选择顶级分类</button>
  35. </div>
  36. </div>
  37. <div class="layui-form-item">
  38. <label class="layui-form-label">菜单地址</label>
  39. <div class="layui-input-inline">
  40. <input type="text" name="auth_url" lay-verify="required" autocomplete="off" placeholder="/home" class="layui-input" value="/">
  41. </div>
  42. <div class="layui-form-mid layui-word-aux"></div>
  43. </div>
  44. <div class="layui-form-item">
  45. <label class="layui-form-label">图标字体</label>
  46. <div class="layui-input-inline">
  47. <input type="text" name="icon" autocomplete="off" placeholder="fa-bar-chart-o" class="layui-input">
  48. </div>
  49. <div class="layui-form-mid layui-word-aux"><a href="http://fontawesome.dashgame.com" target="_blank">点击这里参考</a></div>
  50. </div>
  51. <div class="layui-form-item">
  52. <label class="layui-form-label">排序</label>
  53. <div class="layui-input-inline">
  54. <input type="text" name="sort" lay-verify="required" autocomplete="off" placeholder="999" class="layui-input">
  55. </div>
  56. <div class="layui-form-mid layui-word-aux"></div>
  57. </div>
  58. <div class="layui-form-item">
  59. <label class="layui-form-label">是否显示</label>
  60. <div class="layui-input-inline">
  61. <input type="radio" name="is_show" value="0" title="隐藏" checked>
  62. <input type="radio" name="is_show" value="1" title="显示">
  63. </div>
  64. <div class="layui-form-mid layui-word-aux">是否左侧导航栏显示</div>
  65. </div>
  66. <input type="hidden" name="id" class="layui-input" id="id" value="0">
  67. <div class="layui-form-item">
  68. <div class="layui-input-block">
  69. <button class="layui-btn" lay-submit="" lay-filter="add">新增</button>
  70. <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="edit">修改</button>
  71. <a class="layui-btn layui-btn-danger" href="javascript:;" id="del" lay-submit="">删除</a>
  72. <button type="reset" class="layui-btn layui-btn-primary">重置</button>
  73. </div>
  74. </div>
  75. </form>
  76. </div>
  77. </div>
  78. </div>
  79. <div id="menuContent" class="menuContent" style="display:none; position: absolute;">
  80. <ul id="treeMenu" class="ztree" style="margin-top:0; width:200px; height: 200px"></ul>
  81. </div>
  82. <script>
  83. var $,form;
  84. layui.config({
  85. base : "js/"
  86. }).use(['form','element','layer','jquery'],function(){
  87. form = layui.form; //只有执行了这一步,部分表单元素才会自动修饰成功
  88. $ = layui.jquery;
  89. form.on("submit",function(data) {
  90. var sub_type = data.elem.getAttribute("lay-filter")
  91. console.log(sub_type)
  92. if(sub_type=="add"){
  93. $("#id").val(0);
  94. }else{
  95. if($("#id").val()=="0"){
  96. layer.msg("修改先请在左侧权限树选择节点")
  97. return false
  98. }
  99. }
  100. console.log($("form").serialize());
  101. $.post('{{urlfor "AuthController.AjaxSave"}}', $("form").serialize(), function (out) {
  102. if (out.status == 0) {
  103. // layer.msg("操作成功")
  104. layer.alert('你成功了', {icon: 1},function(index){
  105. window.location.reload();
  106. });
  107. } else {
  108. layer.msg(out.message)
  109. }
  110. }, "json");
  111. return false;
  112. });
  113. form.render();
  114. //但是,如果你的HTML是动态生成的,自动渲染就会失效
  115. //因此你需要在相应的地方,执行下述方法来手动渲染,跟这类似的还有 element.init();
  116. });
  117. </script>
  118. <script type="text/javascript">
  119. var zNodes = [{ id:1, pId:0, name:"数据错误"}];
  120. $(document).ready(function(){
  121. // $("form[name=form]").parent().find("input[type=radio]").eq(1).attr("checked", true);
  122. var setting = {
  123. // edit: {
  124. // enable: true
  125. // },
  126. data: {
  127. simpleData: {
  128. enable: true
  129. }
  130. },
  131. callback: {
  132. beforeDrag: beforeDrag,
  133. onClick: editOnClick
  134. }
  135. };
  136. function editOnClick(e, treeId, treeNode) {
  137. var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
  138. nodes = zTree.getSelectedNodes();
  139. if(nodes[0].id==1){
  140. alert('不允许修改根节点');
  141. return;
  142. }
  143. if(nodes[0].parentTId){
  144. var parentInfo = zTree.getNodeByTId(nodes[0].parentTId);
  145. pid = parentInfo.id;
  146. pname = parentInfo.name;
  147. }
  148. var id = nodes[0].id;
  149. $("#pid").val(nodes[0].pId);
  150. $("#id").val(nodes[0].id);
  151. $("#pname").val(pname);
  152. $("form[name=form]").find("input[name=auth_name]").val(nodes[0].name);
  153. $.ajax({
  154. type: "POST",
  155. url: "/auth/getnode",
  156. data: {id:id},
  157. dataType: 'json',
  158. success: function(data) {
  159. if(data.code==0){
  160. $("form[name=form]").find("input[name=auth_url]").val(data.data.auth_url);
  161. $("form[name=form]").find("input[name=icon]").val(data.data.icon);
  162. $("form[name=form]").find("input[name=sort]").val(data.data.sort);
  163. var is_show = data.data.is_show
  164. $("form[name=form]").find("input[type=radio][value="+is_show+"]").attr("checked", true);
  165. $("form[name=form]").find("input[type=radio][value="+is_show+"]").click();
  166. form.render();
  167. }
  168. }
  169. });
  170. }
  171. function beforeDrag(treeId, treeNodes) {
  172. return false;
  173. }
  174. function setEdit() {
  175. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  176. }
  177. // //增加节点
  178. var newCount = 1;
  179. var maxId = 0;
  180. function add(e) {
  181. var maxId = $('#max_id').val();
  182. var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
  183. isParent = e.data.isParent,
  184. nodes = zTree.getSelectedNodes(),
  185. treeNode = nodes[0];
  186. if (treeNode) {
  187. treeNode = zTree.addNodes(treeNode, {id:(maxId -(-newCount)), pId:treeNode.id, isParent:isParent, name:"new node" + treeNode.id + (newCount++)});
  188. } else {
  189. treeNode = zTree.addNodes(null, {id:(maxId -(-newCount)), pId:0, isParent:isParent, name:"new node" + (newCount++)});
  190. }
  191. if (treeNode) {
  192. zTree.editName(treeNode[0]);
  193. } else {
  194. alert("叶子节点被锁定,无法增加子节点");
  195. }
  196. };
  197. function showCode(str) {
  198. var code = $("#code");
  199. code.empty();
  200. for (var i=0, l=str.length; i<l; i++) {
  201. code.append("<li>"+str[i]+"</li>");
  202. }
  203. }
  204. //加载树
  205. refresh_tree();
  206. function refresh_tree()
  207. {
  208. var time = Date.parse(new Date());
  209. $.ajax({
  210. type: "POST",
  211. url: "/auth/getnodes",
  212. data: {time:time},
  213. dataType: 'json',
  214. success: function(data) {
  215. if(data.code==0){
  216. zNodes = data.data;
  217. $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  218. $.fn.zTree.init($("#treeMenu"), menu, zNodes);
  219. }
  220. }
  221. });
  222. setEdit();
  223. $("#remove").bind("change", setEdit);
  224. $("#rename").bind("change", setEdit);
  225. $("#removeTitle").bind("propertychange", setEdit)
  226. .bind("input", setEdit);
  227. $("#renameTitle").bind("propertychange", setEdit)
  228. .bind("input", setEdit);
  229. }
  230. // //新增 type=1
  231. // $('#submit_add_button').on('click',function(){
  232. // var data = $('form[name=form_add]').serialize();
  233. // ajaxRequest({'nodes':data,'type':1},'/auth/auth/save_auth','submit_add_button','POST',0);
  234. // });
  235. // //新增 type=2
  236. // $('#submit_edit_button').on('click',function(){
  237. // var data = $('form[name=form_add]').serialize();
  238. // ajaxRequest({'nodes':data,'type':2},'/auth/auth/save_auth','submit_edit_button','POST',0);
  239. // });
  240. //删除
  241. $('#del').on('click',function(){
  242. var id = $("#id").val();
  243. if (id==0) {
  244. layer.msg('请在左侧权限树选择节点');
  245. return false;
  246. }
  247. layer.confirm('确认要删除吗?', {icon: 3, title:'提示'}, function(index){
  248. $.post('{{urlfor "AuthController.AjaxDel"}}', {id:id}, function (out) {
  249. if (out.status == 0) {
  250. // layer.msg("操作成功")
  251. layer.alert('你删除成功了', {icon: 1},function(index){
  252. window.location.reload();
  253. });
  254. } else {
  255. layer.msg(out.message)
  256. }
  257. }, "json");
  258. });
  259. return false;
  260. });
  261. // //清空
  262. // $('#submit_cancel_button').on('click',function(){
  263. // var fields = ['id','pid','pname','name','menu_url','sort'];
  264. // $.each(fields,function(k,v) {
  265. // $("form[name=form_add]").find("input[name="+v+"]").val('');
  266. // });
  267. // });
  268. //以下是下拉选择框
  269. var menu = {
  270. view: {
  271. dblClickExpand: false
  272. },
  273. data: {
  274. simpleData: {
  275. enable: true
  276. }
  277. },
  278. callback: {
  279. // beforeClick: beforeClick,
  280. onClick: onClick
  281. }
  282. };
  283. function beforeClick(treeId, treeNode) {
  284. var check = (treeNode && !treeNode.isParent);
  285. if (!check) alert("不能选择");
  286. return check;
  287. }
  288. function onClick(e, treeId, treeNode) {
  289. var zTree = $.fn.zTree.getZTreeObj("treeMenu"),
  290. nodes = zTree.getSelectedNodes(),
  291. v = "";
  292. nodes.sort(function compare(a,b){return a.id-b.id;});
  293. for (var i=0, l=nodes.length; i<l; i++) {
  294. v += nodes[i].name + ",";
  295. }
  296. if (v.length > 0 ) v = v.substring(0, v.length-1);
  297. var pid = $("#pid");
  298. $("#pid").val(nodes[0].id);
  299. $("#pname").val(v);
  300. hideMenu()
  301. }
  302. $("#menuBtn").on("click",function(){
  303. showMenu()
  304. })
  305. function showMenu() {
  306. var pname = $("#pname");
  307. var paOffset = $("#pname").offset();
  308. // var sideOffset = $("#left_side").width();
  309. // console.log(sideOffset.left)
  310. $("#menuContent").css({left:(paOffset.left) + "px", top:(paOffset.top - pname.outerHeight()+70) + "px"}).slideDown("fast");
  311. $("body").bind("mousedown", onBodyDown);
  312. }
  313. function hideMenu() {
  314. $("#menuContent").fadeOut("fast");
  315. $("body").unbind("mousedown", onBodyDown);
  316. }
  317. function onBodyDown(event) {
  318. if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
  319. hideMenu();
  320. }
  321. }
  322. });
  323. </script>