截流自动化的商城平台
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.html 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. layui.config({
  40. version:"{$front_version}",
  41. base: '/static/lib/'
  42. }).extend({
  43. dtree:'dtree/dtree'
  44. }).use(['dtree'], function() {
  45. var $ = layui.$
  46. ,dtree = layui.dtree
  47. var source = areaDatas;
  48. dtree.render({
  49. elem: "#cskTree1",
  50. data:areaDatas,
  51. checkbar:true,
  52. initLevel: 1,
  53. skin: "laySimple",
  54. load:true
  55. });
  56. var checkedData = [];//选中的数据;
  57. var delData = [];//删除的数据
  58. var dtreeDiv = dtree.render({
  59. elem: "#cskTree2",
  60. data: [],
  61. checkbar:true,
  62. response:{treeId:"nodeId",title:"context"},
  63. dataFormat:"list",
  64. skin: "laySimple",
  65. none: "暂无",
  66. toolbar:true,
  67. toolbarShow:["delete"],
  68. toolbarWay:"fixed", // "contextmenu":右键菜单(默认),"fixed":"固定在节点后","follow":"跟随节点动态呈现"
  69. toolbarStyle: {title: "选中"},
  70. toolbarFun: {
  71. delTreeNode: function(treeNode, $div){
  72. delData.push(treeNode);
  73. dtreeDiv.changeTreeNodeDel(true); // 删除成功
  74. }
  75. },
  76. });
  77. //框1中选中的数据 在框2中显示
  78. $("#csk_btn").click(function(){
  79. var param = dtree.getCheckbarNodesParam("cskTree1"); // 获取选中数据
  80. if(param.length == 0) {
  81. layer.msg("请至少选择一个");
  82. }
  83. checkedData = param;
  84. delData = [];
  85. dtree.reload("cskTree2",{data:param});
  86. });
  87. $("#area-freight-submit").click(function(){
  88. dtreeDiv.setDisabledAllNodes('cskTree2');
  89. var data = dtreeDiv.getAllDisabledNodesParam('cskTree2');
  90. var needDel = [];//需要删除的数据
  91. //1,遍历删除的地区id;1级不处理,2级,3级处理
  92. for (var x = 0; x < delData.length; x++){
  93. if (delData[x]['level'] == 2){
  94. var sameLevel = [];
  95. var partner = 0;
  96. //删除上一级,同2级中,数量只有一个时,不删除上级
  97. for (var i = 0; i < data.length; i++){
  98. if (data[i]['level'] == 2){
  99. sameLevel.push(data[i]);
  100. }
  101. if (data[i]['nodeId'] == delData[x]['parentId']){
  102. partner = i;
  103. }
  104. }
  105. if (sameLevel.length > 1){
  106. needDel.push(data[partner]['nodeId']);
  107. }
  108. }
  109. if (delData[x]['level'] == 3){
  110. for (var a = 0;a < data.length; a++){
  111. if (data[a]['nodeId'] == delData[x]['parentId']){
  112. for (var b = 0;b < data.length; b++){
  113. if (data[b]['nodeId'] == data[a]['parentId']){
  114. needDel.push(data[b]['nodeId']);
  115. }
  116. }
  117. needDel.push(data[a]['nodeId']);
  118. }
  119. }
  120. }
  121. }
  122. for (var f =0; f < delData.length;){
  123. if (delData[f]['level'] == 1){
  124. delData.splice(f,1);
  125. } else {
  126. f++;
  127. }
  128. }
  129. var result = [];
  130. for (var d = 0; d < data.length; d++){
  131. if(needDel.indexOf(data[d]['nodeId']) == -1) {
  132. result.push(data[d]);
  133. }
  134. }
  135. var lastData = [];
  136. var firstDataIds = [];
  137. var secondDataIds = [];
  138. for (var e = 0; e < result.length; e++){
  139. if (result[e]['level'] == 1){
  140. var sourceFirst = getFirstChildBySource(source,result[e]['nodeId']);//完整数据中一级的子级(二,三级)
  141. var resultFirst = getChild(result,result[e]['nodeId']);//选中的数据中一级的子级数量
  142. if (sourceFirst.length == resultFirst.length){
  143. lastData.push(result[e]);
  144. firstDataIds.push(result[e]['nodeId']);
  145. }
  146. }
  147. }
  148. //检查非全选时,二级是否全选
  149. for (var m =0; m < result.length; m++){
  150. if((result[m]['level'] == 2) && (firstDataIds.indexOf(result[m]['parentId']) == -1)){
  151. var sourceSecond = getSecondChildBySource(source,result[m]['nodeId']);
  152. var resultSecond = getFirstChild(result,result[m]['nodeId']);
  153. if (sourceSecond.length == resultSecond.length){
  154. lastData.push(result[m]);
  155. secondDataIds.push(result[m]['nodeId']);
  156. }
  157. }
  158. }
  159. //检查非全选时,三级是否全选
  160. for (var n =0; n < result.length; n++){
  161. if((result[n]['level'] == 3) && (secondDataIds.indexOf(result[n]['parentId']) == -1)){
  162. //三级的上级(二级id)的上级是否在firstData中
  163. var thirdLeader = getSecondLeader(source,result[n]['nodeId']);
  164. if (firstDataIds.indexOf(thirdLeader) == -1){
  165. lastData.push(result[n]);
  166. }
  167. }
  168. }
  169. parent.window.callTree && parent.window.callTree(lastData);return;
  170. });
  171. //获取3级的上级
  172. function getSecondLeader(data,id) {
  173. for (var i = 0; i < data.length; i++) {
  174. var firstChildren = data[i]['children'];
  175. for (var x =0; x < firstChildren.length; x++){
  176. var secondChildren = firstChildren[x]['children'];
  177. for (var y = 0; y < secondChildren.length; y++){
  178. if (secondChildren[y]['id'] == id){
  179. return data[i]['id'];
  180. }
  181. }
  182. }
  183. }
  184. return 0;
  185. }
  186. //获取一级下的子级(二级,三级)
  187. function getFirstChildBySource(data,pid) {
  188. var child = [];
  189. for (var i = 0; i < data.length; i++){
  190. if (data[i]['id'] == pid){
  191. var firstChildren = data[i]['children'];
  192. for (var x =0; x < firstChildren.length; x++){
  193. child.push(firstChildren[x]);
  194. var secondChildren = firstChildren[x]['children'];
  195. for (var y = 0; y < secondChildren.length; y++){
  196. child.push(secondChildren[y]);
  197. }
  198. }
  199. }
  200. }
  201. return child;
  202. }
  203. //获取二级下的子级(三级)
  204. function getSecondChildBySource(data,pid) {
  205. var child = [];
  206. for (var i = 0; i < data.length; i++){
  207. var firstChildren = data[i]['children'];
  208. for (var x =0; x < firstChildren.length; x++){
  209. if(firstChildren[x]['id'] == pid){
  210. var secondChildren = firstChildren[x]['children'];
  211. for (var y = 0; y < secondChildren.length; y++){
  212. child.push(secondChildren[y]);
  213. }
  214. }
  215. }
  216. }
  217. return child;
  218. }
  219. function getChild(data,pid) {
  220. var child = [];
  221. for (var i = 0; i < data.length; i++){
  222. if (data[i]['parentId'] == pid){
  223. child.push(data[i]);
  224. for (var a = 0; a < data.length; a++){
  225. if (data[a]['parentId'] == data[i]['nodeId']){
  226. child.push(data[a]);
  227. }
  228. }
  229. }
  230. }
  231. return child;
  232. }
  233. function getFirstChild(data,pid) {
  234. var firstChild = [];
  235. for (var i = 0; i < data.length; i++){
  236. if (data[i]['parentId'] == pid){
  237. firstChild.push(data[i]);
  238. }
  239. }
  240. return firstChild;
  241. }
  242. var data = array_column(areas,'id');
  243. //获取父类id
  244. function getParentIds(ids) {
  245. var call_ids=[];
  246. for(var i in ids){
  247. if(data[ids[i]]['parent_id']!=0){
  248. ids.push(data[ids[i]]['parent_id']);
  249. call_ids.push(data[ids[i]]['parent_id']);
  250. }
  251. }
  252. if(call_ids.length!=0){
  253. ids=ids.concat(getParentIds(call_ids));
  254. }
  255. return ids;
  256. }
  257. //获取子类id
  258. function getChildrenIds(ids){
  259. var call_ids=[];
  260. for(var i in ids){
  261. if(data[ids[i]]['level']==3){
  262. continue;
  263. }
  264. for(var j in data){
  265. if(ids[i] == data[j]['parent_id']){
  266. ids.push(data[j]['id']);
  267. call_ids.push(data[j]['id']);
  268. }
  269. }
  270. }
  271. if(call_ids.length!=0){
  272. ids=ids.concat(getChildrenIds(call_ids));
  273. }
  274. return ids;
  275. }
  276. /**
  277. * 获取相关id
  278. * @param ids
  279. */
  280. function getAboutIds(ids){
  281. result = getChildrenIds(ids);
  282. result = result.concat(getParentIds(ids));
  283. console.log(result);
  284. return array_unique(result);
  285. }
  286. // console.log(getAboutIds(['360000','445100','440105']));
  287. });
  288. </script>