dialogtree.blade.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <a href="javascript:void(0)" class="{{ $prefix }}-open-tree" data-checked="{{ $checkAll }}" data-val="{{ $value }}">
  2. <i class='feather icon-align-right'></i> {{ trans('admin.view') }}
  3. </a>
  4. <script require="@jstree">
  5. var selector = '.{{ $prefix}}-open-tree';
  6. $(document).off('click', selector).on('click', selector, function () {
  7. var tpl = '<div class="jstree-wrapper p-1" style="border:0"><div class="da-tree" style="margin-top:10px"></div></div>',
  8. url = '{{ $url }}',
  9. t = $(this),
  10. val = t.data('val'),
  11. ckall = t.data('checked'),
  12. idx,
  13. loading,
  14. opts = {!! json_encode($options) !!};
  15. val = val ? String(val).split(',') : [];
  16. if (url) {
  17. if (loading) return;
  18. loading = 1;
  19. t.buttonLoading();
  20. $.ajax(url, {data: {value: val}}).then(function (resp) {
  21. loading = 0;
  22. t.buttonLoading(false);
  23. if (!resp.status) {
  24. return Dcat.error(resp.message || '系统繁忙,请稍后再试');
  25. }
  26. build(resp.value);
  27. });
  28. } else {
  29. build(val);
  30. }
  31. function build(val) {
  32. opts.core.data = formatNodes(val, {!! json_encode($nodes) !!});
  33. idx = layer.open({
  34. type: 1,
  35. area: {!! json_encode($area) !!},
  36. content: tpl,
  37. title: '{{ $title }}',
  38. success: function (a, idx) {
  39. var tree = $('#layui-layer'+idx).find('.da-tree');
  40. tree.on("loaded.jstree", function () {
  41. tree.jstree('open_all');
  42. }).jstree(opts);
  43. }
  44. });
  45. $(document).one('pjax:complete', function () {
  46. layer.close(idx);
  47. });
  48. }
  49. function formatNodes(value, all) {
  50. var idColumn = '{{ $columnNames['id'] }}',
  51. textColumn = '{{ $columnNames['text'] }}',
  52. parentColumn = '{{ $columnNames['parent'] }}',
  53. parentIds = [], nodes = [], i, v, parentId;
  54. for (i in all) {
  55. v = all[i];
  56. if (!v[idColumn]) continue;
  57. parentId = v[parentColumn] || '#';
  58. if (!parentId) {
  59. parentId = '#';
  60. } else {
  61. parentIds.push(parentId);
  62. }
  63. v['state'] = {'disabled': true};
  64. if (ckall || (value && Dcat.helpers.inObject(value, v[idColumn]))) {
  65. v['state']['selected'] = true;
  66. }
  67. nodes.push({
  68. 'id' : v[idColumn],
  69. 'text' : v[textColumn] || null,
  70. 'parent' : parentId,
  71. 'state' : v['state'],
  72. });
  73. }
  74. return nodes;
  75. }
  76. });
  77. </script>