截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

area_edit.html 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. {layout name="layout2" /}
  2. <link rel="stylesheet" href="/static/lib/dtree/dtree.css" media="all">
  3. <link rel="stylesheet" href="/static/lib/dtree/font/dtreefont.css" media="all">
  4. <div class="layui-row " style="margin-top: 10px;background: #f2f2f2">
  5. <div class="layui-col-md5">
  6. <div class="layui-card">
  7. <div class="layui-card-header">选择地区</div>
  8. <div class="layui-card-body">
  9. <div style="height: 280px;overflow: auto;">
  10. <ul id="cskTree1" class="dtree" data-id="0"></ul>
  11. </div>
  12. </div>
  13. </div>
  14. </div>
  15. <div class="layui-col-md2">
  16. <div style="height: 280px;text-align: center">
  17. <button class="layui-btn layui-btn-sm layui-btn-normal" style="margin-top: 180px;" id="csk_btn">选择</button>
  18. </div>
  19. </div>
  20. <div class="layui-col-md5">
  21. <div class="layui-card">
  22. <div class="layui-card-header">已选地区</div>
  23. <div class="layui-card-body">
  24. <div style="height: 280px;overflow: auto;">
  25. <ul id="cskTree2" class="dtree" data-id="-1"></ul>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31. <div class="layui-form-item layui-hide">
  32. <input type="button" lay-submit lay-filter="area-freight-submit" id="area-freight-submit" value="确认">
  33. </div>
  34. <script type="text/javascript" src="/static/lib/dtree/areaData.js"></script>
  35. <script type="text/javascript" src="/static/lib/dtree/dtree.js"></script>
  36. <script src="/static/common/js/area.js"></script>
  37. <script src="/static/common/js/array.js"></script>
  38. <script>
  39. var selectedIds = '';//编辑的id
  40. var regionIds = [];//格式化后的需要编辑的id
  41. function editSelected(data) {
  42. selectedIds = data;
  43. regionIds = selectedIds.split(",");
  44. }
  45. var data = array_column(areas,'id');
  46. layui.config({
  47. version:"{$front_version}",
  48. base: '/static/lib/'
  49. }).extend({
  50. likeedit: 'likeedit/likeedit',
  51. dtree:'dtree/dtree'
  52. }).use(['table', 'form', 'element', 'likeedit','dtree'], function() {
  53. var form = layui.form
  54. ,$ = layui.$
  55. ,table = layui.table
  56. , element = layui.element
  57. ,dtree = layui.dtree
  58. , likeedit = layui.likeedit;
  59. var source = areaDatas;//树状结构的完整数据(用于渲染树形结构插件)
  60. //组装选中地区id几其相关地区id
  61. selectedIds += ',' + getAboutIds(regionIds);
  62. //渲染数据框1的地区选中
  63. dtree.render({
  64. elem: "#cskTree1",
  65. data:areaDatas,
  66. checkbar:true,
  67. initLevel: 1,
  68. skin: "laySimple",
  69. checkbarType:"all",
  70. load:true,
  71. done: function(res, $ul, first){
  72. dtree.chooseDataInit("cskTree1",selectedIds); // 初始化选中
  73. //把需要编辑的选中的数据在数据框2中选中
  74. var editParam = dtree.getCheckbarNodesParam("cskTree1"); // 获取选中数据
  75. checkedData = editParam;
  76. delData = [];
  77. dtree.reload("cskTree2",{data:editParam});
  78. }
  79. });
  80. var checkedData = [];//选中的数据;
  81. var delData = [];//删除的数据
  82. //渲染地区数据框2的数据
  83. var dtreeDiv = dtree.render({
  84. elem: "#cskTree2",
  85. data: [],
  86. checkbar:true,
  87. response:{treeId:"nodeId",title:"context"},
  88. dataFormat:"list",
  89. skin: "laySimple",
  90. none: "暂无",
  91. toolbar:true,
  92. toolbarShow:["delete"],
  93. toolbarWay:"fixed", // "contextmenu":右键菜单(默认),"fixed":"固定在节点后","follow":"跟随节点动态呈现"
  94. toolbarStyle: {title: "选中"},
  95. toolbarFun: {
  96. delTreeNode: function(treeNode, $div){
  97. delData.push(treeNode);
  98. dtreeDiv.changeTreeNodeDel(true); // 删除成功
  99. }
  100. },
  101. });
  102. //框1中选择的数据 在框2中显示
  103. $("#csk_btn").click(function(){
  104. var param = dtree.getCheckbarNodesParam("cskTree1"); // 获取选中数据
  105. if(param.length == 0) {
  106. layer.msg("请至少选择一个");
  107. }
  108. checkedData = param;
  109. delData = [];
  110. dtree.reload("cskTree2",{data:param});
  111. });
  112. //点击确认时,把编辑过的框2的数据返回到地区编辑页
  113. $("#area-freight-submit").click(function(){
  114. dtreeDiv.setDisabledAllNodes('cskTree2');
  115. var data = dtreeDiv.getAllDisabledNodesParam('cskTree2');
  116. var needDel = [];//需要删除的数据
  117. //1,遍历删除的地区id;1级不处理,2级,3级处理
  118. for (var x = 0; x < delData.length; x++){
  119. if (delData[x]['level'] == 2){
  120. var sameLevel = [];
  121. var partner = 0;
  122. //删除上一级,同2级中,数量只有一个时,不删除上级
  123. for (var i = 0; i < data.length; i++){
  124. if (data[i]['level'] == 2){
  125. sameLevel.push(data[i]);
  126. }
  127. if (data[i]['nodeId'] == delData[x]['parentId']){
  128. partner = i;
  129. }
  130. }
  131. if (sameLevel.length > 1){
  132. needDel.push(data[partner]['nodeId']);
  133. }
  134. }
  135. if (delData[x]['level'] == 3){
  136. for (var a = 0;a < data.length; a++){
  137. if (data[a]['nodeId'] == delData[x]['parentId']){
  138. for (var b = 0;b < data.length; b++){
  139. if (data[b]['nodeId'] == data[a]['parentId']){
  140. needDel.push(data[b]['nodeId']);
  141. }
  142. }
  143. needDel.push(data[a]['nodeId']);
  144. }
  145. }
  146. }
  147. }
  148. for (var f =0; f < delData.length;){
  149. if (delData[f]['level'] == 1){
  150. delData.splice(f,1);
  151. } else {
  152. f++;
  153. }
  154. }
  155. var result = [];
  156. for (var d = 0; d < data.length; d++){
  157. if(needDel.indexOf(data[d]['nodeId']) == -1) {
  158. result.push(data[d]);
  159. }
  160. }
  161. var lastData = [];
  162. var firstDataIds = [];
  163. var secondDataIds = [];
  164. for (var e = 0; e < result.length; e++){
  165. if (result[e]['level'] == 1){
  166. var sourceFirst = getFirstChildBySource(source,result[e]['nodeId']);//完整数据中一级的子级(二,三级)
  167. var resultFirst = getChild(result,result[e]['nodeId']);//选中的数据中一级的子级数量
  168. if (sourceFirst.length == resultFirst.length){
  169. lastData.push(result[e]);
  170. firstDataIds.push(result[e]['nodeId']);
  171. }
  172. }
  173. }
  174. //检查非全选时,二级是否全选
  175. for (var m =0; m < result.length; m++){
  176. if((result[m]['level'] == 2) && (firstDataIds.indexOf(result[m]['parentId']) == -1)){
  177. var sourceSecond = getSecondChildBySource(source,result[m]['nodeId']);
  178. var resultSecond = getFirstChild(result,result[m]['nodeId']);
  179. if (sourceSecond.length == resultSecond.length){
  180. lastData.push(result[m]);
  181. secondDataIds.push(result[m]['nodeId']);
  182. }
  183. }
  184. }
  185. //检查非全选时,三级是否全选
  186. for (var n =0; n < result.length; n++){
  187. if((result[n]['level'] == 3) && (secondDataIds.indexOf(result[n]['parentId']) == -1)){
  188. //三级的上级(二级id)的上级是否在firstData中
  189. var thirdLeader = getSecondLeader(source,result[n]['nodeId']);
  190. if (firstDataIds.indexOf(thirdLeader) == -1){
  191. lastData.push(result[n]);
  192. }
  193. }
  194. }
  195. parent.window.callTree && parent.window.callTree(lastData);
  196. });
  197. //获取3级的上级
  198. function getSecondLeader(data,id) {
  199. for (var i = 0; i < data.length; i++) {
  200. var firstChildren = data[i]['children'];
  201. for (var x =0; x < firstChildren.length; x++){
  202. var secondChildren = firstChildren[x]['children'];
  203. for (var y = 0; y < secondChildren.length; y++){
  204. if (secondChildren[y]['id'] == id){
  205. return data[i]['id'];
  206. }
  207. }
  208. }
  209. }
  210. return 0;
  211. }
  212. //获取一级下的子级(二级,三级)
  213. function getFirstChildBySource(data,pid) {
  214. var child = [];
  215. for (var i = 0; i < data.length; i++){
  216. if (data[i]['id'] == pid){
  217. var firstChildren = data[i]['children'];
  218. for (var x =0; x < firstChildren.length; x++){
  219. child.push(firstChildren[x]);
  220. var secondChildren = firstChildren[x]['children'];
  221. for (var y = 0; y < secondChildren.length; y++){
  222. child.push(secondChildren[y]);
  223. }
  224. }
  225. }
  226. }
  227. return child;
  228. }
  229. //获取二级下的子级(三级)
  230. function getSecondChildBySource(data,pid) {
  231. var child = [];
  232. for (var i = 0; i < data.length; i++){
  233. var firstChildren = data[i]['children'];
  234. for (var x =0; x < firstChildren.length; x++){
  235. if(firstChildren[x]['id'] == pid){
  236. var secondChildren = firstChildren[x]['children'];
  237. for (var y = 0; y < secondChildren.length; y++){
  238. child.push(secondChildren[y]);
  239. }
  240. }
  241. }
  242. }
  243. return child;
  244. }
  245. //获取下级
  246. function getChild(data,pid) {
  247. var child = [];
  248. for (var i = 0; i < data.length; i++){
  249. if (data[i]['parentId'] == pid){
  250. child.push(data[i]);
  251. for (var a = 0; a < data.length; a++){
  252. if (data[a]['parentId'] == data[i]['nodeId']){
  253. child.push(data[a]);
  254. }
  255. }
  256. }
  257. }
  258. return child;
  259. }
  260. //第一个下级
  261. function getFirstChild(data,pid) {
  262. var firstChild = [];
  263. for (var i = 0; i < data.length; i++){
  264. if (data[i]['parentId'] == pid){
  265. firstChild.push(data[i]);
  266. }
  267. }
  268. return firstChild;
  269. }
  270. //获取父类id
  271. function getParentIds(ids) {
  272. var call_ids=[];
  273. for(var i in ids){
  274. if(data[ids[i]]['parent_id']!=0){
  275. ids.push(data[ids[i]]['parent_id']);
  276. call_ids.push(data[ids[i]]['parent_id']);
  277. }
  278. }
  279. if(call_ids.length!=0){
  280. ids=ids.concat(getParentIds(call_ids));
  281. }
  282. return ids;
  283. }
  284. //获取子类id
  285. function getChildrenIds(ids){
  286. var call_ids=[];
  287. for(var i in ids){
  288. if(data[ids[i]]['level']==3){
  289. continue;
  290. }
  291. for(var j in data){
  292. if(ids[i] == data[j]['parent_id']){
  293. ids.push(data[j]['id']);
  294. call_ids.push(data[j]['id']);
  295. }
  296. }
  297. }
  298. if(call_ids.length!=0){
  299. ids=ids.concat(getChildrenIds(call_ids));
  300. }
  301. return ids;
  302. }
  303. //获取有关的地区id
  304. function getAboutIds(ids){
  305. result = getChildrenIds(ids);
  306. result = result.concat(getParentIds(ids));
  307. return array_unique(result);
  308. }
  309. });
  310. </script>