table.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <style>
  2. .jstree-default .jstree-search {
  3. font-style: normal;
  4. color: rgb(55, 71, 79);
  5. font-weight: 700;
  6. }
  7. .jstree-default .jstree-clicked {
  8. background: #ffffff;
  9. border-radius: 2px;
  10. color: rgb(55, 71, 79);
  11. box-shadow: inset 0 0 0 #999;
  12. }
  13. .jstree-default .jstree-hovered {
  14. background: #ffffff;
  15. border-radius: 2px;
  16. color: rgb(55, 71, 79);
  17. box-shadow: inset 0 0 0 #999;
  18. }
  19. .jstree-default .node-icon {
  20. padding-left: 8px;
  21. }
  22. .jstree-anchor {
  23. padding-right: 70px;
  24. }
  25. </style>
  26. <div class="row">
  27. <div class="col-3">
  28. <div class="box">
  29. <div class="box-header">
  30. <h3 class="box-title"><?= $title; ?></h3>
  31. <div class="box-tools">
  32. <button class="btn btn-primary btn-sm" onclick="nodeCreate()"><i class="icon ion-plus"></i> 创建</button>
  33. </div>
  34. </div>
  35. <div class="box-body table-responsive">
  36. <!-- 描述:搜索框 -->
  37. <div class="input-group m-b">
  38. <input type="text" class="form-control" placeholder="请输入搜索关键字..." id="search_ay"
  39. aria-describedby="basic-addon1">
  40. </div>
  41. <div id="<?= $name; ?>"></div>
  42. <div class="m-t">
  43. <span class="orange">按住拖动可更换上级</span>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. <div class="col-9">
  49. <div class="box">
  50. <div class="box-header">
  51. <h3 class="box-title">基本信息 <span class="jstree-table-id"></span></h3>
  52. </div>
  53. <div class="box-body">
  54. <div class="col-lg-12" id="container">
  55. </div>
  56. </div>
  57. <div class="box-footer text-center">
  58. <span class="btn btn-primary" onclick="nodeBeforeSubmit()">保存</span>
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. <?php
  64. $this->registerJs(<<<JS
  65. var treeId = $name;
  66. var ajax = '$ajax';
  67. var editUrl = '$editUrl';
  68. var deleteUrl = '$deleteUrl';
  69. var treeCheckIds = JSON.parse('[]');
  70. var treeData = $jsTreeData;
  71. showCheckboxTree(treeData, $(treeId).attr('id'), treeCheckIds);
  72. /**
  73. * 带checkbox的树形控件使用说明
  74. * @data 应该是一个js数组
  75. * @id: 将树渲染到页面的某个div上,此div的id
  76. * @checkId:需要默认勾选的数节点id;1.checkId="all",表示勾选所有节点 2.checkId=[1,2]表示勾选id为1,2的节点
  77. * 节点的id号由url传入json串中的id决定
  78. */
  79. function showCheckboxTree(data, id, checkId) {
  80. var that = $("#" + id);
  81. menuTree = that.bind("loaded.jstree", function (e, data) {
  82. // that.jstree("open_all");
  83. that.find("li").each(function () {
  84. if (checkId == 'all') {
  85. that.jstree("check_node", $(this));
  86. }
  87. });
  88. }).jstree({
  89. "core": {
  90. "check_callback" : true,
  91. 'data' : parseInt(ajax) === 0 ? data : {
  92. "url" : "$listUrl",
  93. "data" : function (node) {
  94. return {"pid" : node.id === '#' ? 0 : node.id}
  95. }
  96. },
  97. "attr": {
  98. "class": "jstree-checked"
  99. }
  100. },
  101. "check_callback" : function(operation, node, node_parent, node_position, more){
  102. // if(operation === "move_node"){
  103. // var node = this.get_node(node_parent);
  104. // if(node.id === "#"){
  105. // alert("根结点不可以删除");
  106. // return false;
  107. // }
  108. // if(node.state.disabled){
  109. // alert("禁用的不可以删除");
  110. // return false;
  111. // }
  112. // }else if(operation === "delete_node"){
  113. // var node = this.get_node(node_parent);
  114. // if(node.id === "#"){
  115. // alert("根结点不可以删除");
  116. // return false;
  117. // }
  118. // }
  119. // return true;
  120. },
  121. "types": {
  122. "default": {
  123. "valid_children": ["default", "file"]
  124. },
  125. "file": {
  126. "icon": "fa fa-folder-file",
  127. "valid_children": []
  128. }
  129. },
  130. "plugins": [
  131. "contextmenu", "dnd", "search",
  132. "types","sort","state",
  133. ],
  134. "contextmenu": {
  135. "items": {
  136. "create": null,
  137. "rename": null,
  138. "remove": null,
  139. "ccp": null
  140. }
  141. },
  142. 'sort' : function(a, b) {
  143. // console.log(a, b);
  144. // you can do custom sorting here
  145. // based on your user action you can return 1 or -1
  146. // 1 show on top -1 for show bottom
  147. }
  148. });
  149. // 加载完毕关闭所有节点
  150. that.bind('ready.jstree', function (obj, e) {
  151. that.jstree('close_all');
  152. });
  153. // 监听打开事件
  154. that.on("open_node.jstree", function(e,data){
  155. var currentNode = data.node;
  156. data.instance.set_icon(currentNode, "fa fa-folder-open");
  157. });
  158. // 监听关闭事件
  159. that.on("close_node.jstree", function(e,data){
  160. var currentNode = data.node;
  161. data.instance.set_icon(currentNode, "fa fa-folder");
  162. });
  163. // 鼠标移出事件
  164. that.on('dehover_node.jstree', function (e,data) {
  165. $('.node-icon').remove();
  166. });
  167. // 鼠标移上事件
  168. that.on('hover_node.jstree', function (e,data) {
  169. var node = data.node.original;
  170. $('#' + node.id + '_anchor').append("<i class='fa fa-plus node-icon node-create' onclick='nodeCreateChild(" + node.id + ")' title='创建'></i><i class='fa fa-edit node-icon node-create' onclick='nodeEdit(" + node.id + ")' title='编辑'></i><i class='fa fa-trash node-icon node-delete' onclick='nodeDelete(" + node.id + ")' title='删除'></i>")
  171. console.log(data.node);
  172. });
  173. // 拖拽移动
  174. that.on('move_node.jstree', function(e,data) {
  175. moveParentId = data.parent;
  176. if (moveParentId === '#'){
  177. moveParentId = 0;
  178. }
  179. $.ajax({
  180. type: "get",
  181. url: '$moveUrl',
  182. dataType: "json",
  183. data: {id: data.node.id, pid: moveParentId},
  184. success: function(html){
  185. }
  186. });
  187. });
  188. // 单击
  189. that.on("activate_node.jstree", function(e, data){
  190. });
  191. // 创建
  192. that.on("create_node.jstree", function(e, data){
  193. });
  194. // 修改
  195. that.on("rename_node.jstree", function(e, data){
  196. });
  197. // 删除
  198. that.on("delete_node.jstree", function(e, data){
  199. });
  200. // 查询节点名称
  201. var to = false;
  202. $("#search_ay").keyup(function(){
  203. if(to){
  204. clearTimeout(to);
  205. }
  206. to = setTimeout(function(){
  207. // 开启插件查询后 使用这个方法可模糊查询节点
  208. that.jstree(true).search($('#search_ay').val());
  209. },250);
  210. });
  211. $('.btn-tab').click(function(){ //选项事件
  212. //alert($(this).attr("var"))
  213. that.jstree(true).destroy(); //可做联级
  214. that = jstree_fun($(this).attr("var"));//可做联级
  215. //alert($(this).attr("var"))
  216. });
  217. $('.refresh').click(function(){ //刷新事件
  218. that.jstree(true).refresh();
  219. that.jstree('uncheck_all');
  220. });
  221. }
  222. JS
  223. );
  224. ?>
  225. <script>
  226. var isNodeNewRecord = false;
  227. var nodePid = 0;
  228. // 初始化
  229. $(document).ready(function () {
  230. nodeCreate();
  231. });
  232. function nodeCreate() {
  233. $('.jstree-table-id').text('');
  234. isNodeNewRecord = true;
  235. initEdit();
  236. }
  237. function nodeCreateChild(id) {
  238. isNodeNewRecord = true;
  239. initEdit('', id);
  240. }
  241. function initEdit(id = '', pid = 0) {
  242. layer.load(2);
  243. $.ajax({
  244. type: "get",
  245. url: "<?= $editUrl ?>",
  246. dataType: "html",
  247. data: {id: id, pid: pid},
  248. success: function (html) {
  249. $("#container").html(html);
  250. layer.closeAll('loading');
  251. }
  252. });
  253. }
  254. function nodeEdit(id) {
  255. layer.load(2);
  256. isNodeNewRecord = false;
  257. $('.jstree-table-id').html('<small>ID: ' + id + '</small>');
  258. $.ajax({
  259. type: "get",
  260. url: "<?= $editUrl ?>",
  261. dataType: "html",
  262. data: {id: id},
  263. success: function (html) {
  264. $("#container").html(html);
  265. layer.closeAll('loading');
  266. }
  267. });
  268. }
  269. function nodeDelete(id) {
  270. var ref = $("#userTree").jstree(true);
  271. swal("确定要删除么吗?", {
  272. buttons: {
  273. cancel: "取消",
  274. defeat: '确定',
  275. },
  276. title: '确定要删除么吗?',
  277. text: '请谨慎操作',
  278. }).then((value) => {
  279. switch (value) {
  280. case "defeat":
  281. $.ajax({
  282. type: "get",
  283. url: "<?= $deleteUrl ?>",
  284. dataType: "json",
  285. data: {id: id},
  286. success: function (data) {
  287. if (parseInt(data.code) === 200) {
  288. ref.delete_node(id);
  289. initEdit();
  290. $('.jstree-table-id').text('');
  291. rfMsg(data.message);
  292. } else {
  293. rfMsg(data.message);
  294. }
  295. }
  296. });
  297. break;
  298. default:
  299. }
  300. });
  301. }
  302. function nodeBeforeSubmit() {
  303. var data = $("#w0").serializeArray();
  304. var ref = $("#userTree").jstree(true);
  305. var sel = ref.get_selected();
  306. var id = '';
  307. // 创建
  308. if (isNodeNewRecord === false && sel.length > 0) {
  309. id = sel[0];
  310. }
  311. $.ajax({
  312. type: "post",
  313. url: "<?= $editUrl ?>?id=" + id,
  314. dataType: "json",
  315. data: data,
  316. success: function (data) {
  317. if (parseInt(data.code) === 200) {
  318. var res = data.data;
  319. var createPid = parseInt(res.pid);
  320. if (createPid === 0) {
  321. createPid = '#';
  322. }
  323. if (isNodeNewRecord === false) {
  324. ref.set_text(res.id, res.title);
  325. } else {
  326. // 创建新节点
  327. ref.create_node(createPid, {
  328. "id": res.id, // 节点ID
  329. "text": res.title // 节点文本
  330. });
  331. $('.jstree-table-id').html('<small>ID: ' + res.id + '</small>');
  332. }
  333. swal("操作成功", "小手一抖就打开了一个框", "success");
  334. } else {
  335. rfMsg(data.message);
  336. }
  337. }
  338. });
  339. }
  340. </script>
粤ICP备19079148号