layui-xtree.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //**********************************
  2. //** 本插件依赖 贤心 layui form模块
  3. //** 由 小巷 制作 QQ:151446298
  4. //** 版本3.0 时间 2018-01-10 20:48
  5. //**********************************
  6. //构造
  7. function layuiXtree(options) {
  8. var _this = this;
  9. _this._containerid = options.elem;
  10. _this._container = document.getElementById(options.elem); //容器
  11. _this._container.style.minHeight = "100px";
  12. _this._options = options;
  13. _this.Loading(options);
  14. }
  15. //封装IE8 Class选择
  16. layuiXtree.prototype.getByClassName = function (cn) {
  17. if (document.getElementsByClassName) return this._container.getElementsByClassName(cn);
  18. var _xlist = this._container.childNodes;
  19. var _xtemp = new Array();
  20. for (var i = 0; i < _xlist.length; i++) {
  21. var _xchild = _xlist[i];
  22. var _xclassNames = _xchild.getAttribute('class').split(' ');
  23. for (var j = 0; j < _xclassNames.length; j++) {
  24. if (_xclassNames[j] == cn) {
  25. _xtemp.push(_xchild);
  26. break;
  27. }
  28. }
  29. }
  30. return _xtemp;
  31. }
  32. //在一个对象下面找子级
  33. layuiXtree.prototype.getChildByClassName = function (obj, cn) {
  34. var _xlist = obj.childNodes;
  35. var _xtemp = new Array();
  36. for (var i = 0; i < _xlist.length; i++) {
  37. var _xchild = _xlist[i];
  38. var _xclassNames = _xchild.getAttribute('class').split(' ');
  39. for (var j = 0; j < _xclassNames.length; j++) {
  40. if (_xclassNames[j] == cn) {
  41. _xtemp.push(_xchild);
  42. break;
  43. }
  44. }
  45. }
  46. return _xtemp;
  47. }
  48. //加载特效,且获取数据
  49. layuiXtree.prototype.Loading = function (options) {
  50. var _this = this;
  51. _this.xloading = document.createElement("span"); //创建加载对象
  52. _this.xloading.setAttribute('class', 'layui-icon layui-anim layui-anim-rotate layui-anim-loop');
  53. _this.xloading.innerHTML = '&#xe63e;';
  54. _this.xloading.style.fontSize = "50px";
  55. _this.xloading.style.color = "#009688";
  56. _this.xloading.style.fontWeight = "bold";
  57. _this.xloading.style.marginLeft = _this._container.offsetWidth / 2 - 25 + 'px';
  58. _this.xloading.style.marginTop = _this._container.offsetHeight / 2 - 50 + 'px';
  59. _this._container.innerHTML = '';
  60. _this._container.appendChild(_this.xloading); //加载显示
  61. if (typeof (options.data) == 'object') {
  62. _this._dataJson = options.data;
  63. _this.Initial(options);
  64. return;
  65. }
  66. //如果是字符串url,进行异步加载
  67. // $.post(options.data,{},function (data) {
  68. // // console.log(data.data);
  69. // _this._dataJson = data.data;
  70. // _this.Initial = options;
  71. // })
  72. var obj = new XMLHttpRequest();
  73. obj.open('get', options.data, true);
  74. obj.onreadystatechange = function () {
  75. if (obj.readyState == 4 && obj.status == 200 || obj.status == 304) { //回调成功
  76. // console.log(obj.responseText);
  77. // console.log(obj.responseText);
  78. _this._dataJson = eval('(' + obj.responseText + ')'); //将返回的数据转为json
  79. _this.Initial(options);
  80. }
  81. };
  82. obj.send();
  83. }
  84. //data验证后的数据初始化
  85. layuiXtree.prototype.Initial = function (o) {
  86. var _this = this;
  87. _this._form = o.form; //layui from对象
  88. _this._domStr = ""; //结构字符串
  89. _this._isopen = o.isopen != null ? o.isopen : true;
  90. if (o.color == null) o.color = { open: '#2F4056', close: '#2F4056', end: '#2F4056' };//图标颜色
  91. _this._iconOpenColor = o.color.open != null ? o.color.open : "#2F4056";
  92. _this._iconCloseColor = o.color.close != null ? o.color.close : "#2F4056";
  93. _this._iconEndColor = o.color.end != null ? o.color.end : "#2F4056";
  94. if (o.icon == null) o.icon = { open: '&#xe625;', close: '&#xe623;', end: '&#xe621;' };//图标样式
  95. _this._iconOpen = o.icon.open != null ? o.icon.open : '&#xe625;';
  96. _this._iconClose = o.icon.close != null ? o.icon.close : '&#xe623;';
  97. _this._iconEnd = o.icon.end != null ? o.icon.end : '&#xe621;';
  98. _this._click = o.click != null ? o.click : function () { };
  99. _this._ckall = o.ckall != null ? o.ckall : false; //全选是否启用
  100. _this._ckallSuccess = o.ckallback != null ? o.ckallback : function () { };//全选回调
  101. _this.CreateCkAll();
  102. // console.log(_this._dataJson.data);
  103. _this.dataBind(_this._dataJson.data);
  104. _this.Rendering();
  105. }
  106. //全选框
  107. layuiXtree.prototype.CreateCkAll = function () {
  108. var _this = this;
  109. if (_this._ckall) {
  110. _this._domStr += '<div class="layui-xtree-item">';
  111. _this._domStr += '<input type="checkbox" class="layui-xtree-checkbox layui-xtree-ckall" title="全选" lay-skin="primary" lay-filter="xtreeckall' + _this._containerid + '">';
  112. _this._domStr += '</div>';
  113. }
  114. }
  115. //生产结构
  116. layuiXtree.prototype.dataBind = function (d) {
  117. var _this = this;
  118. var datas = d;
  119. if (typeof datas == 'object') {
  120. for (i in datas) {
  121. // console.log(datas[i].value);
  122. // console.log( d[i].value);
  123. var xtree_isend = '';
  124. var xtree_ischecked = '';
  125. var xtree_isdisabled = datas[i].disabled ? ' disabled="disabled" ' : '';
  126. _this._domStr += '<div class="layui-xtree-item" style="height:100%;">';
  127. if (typeof datas[i].data == 'object') {
  128. _this._domStr += '<i class="layui-icon layui-xtree-icon" data-xtree="' + (_this._isopen ? '1' : '0') + '">' + (_this._isopen ? _this._iconOpen : _this._iconClose) + '</i>';
  129. } else {
  130. _this._domStr += '<i class="layui-icon layui-xtree-icon-null">' + _this._iconEnd + '</i>';
  131. xtree_isend = 'data-xend="1"';
  132. xtree_ischecked = datas[i].checked ? ' checked ' : '';
  133. xtree_isdisabled = datas[i].disabled ? ' disabled="disabled" ' : '';
  134. }
  135. _this._domStr += '<input type="checkbox" class="layui-xtree-checkbox" ' + xtree_isend + xtree_ischecked + xtree_isdisabled + ' value="' + datas[i].value + '" title="' + datas[i].title + '" lay-skin="primary" lay-filter="xtreeck' + _this._containerid + '">';
  136. _this.dataBind(datas[i].data);
  137. _this._domStr += '</div>';
  138. }
  139. }
  140. }
  141. //渲染呈现
  142. layuiXtree.prototype.Rendering = function () {
  143. var _this = this;
  144. _this._container.innerHTML = _this._domStr;
  145. _this._domStr = "";
  146. //检查选中状态
  147. var xtree_ckitems = _this.getByClassName('layui-xtree-checkbox');
  148. for (var i = 0; i < xtree_ckitems.length; i++) {
  149. if (xtree_ckitems[i].getAttribute('data-xend') == '1' && xtree_ckitems[i].checked) {
  150. _this.ParentCheckboxChecked(xtree_ckitems[i]);
  151. }
  152. }
  153. _this._form.render('checkbox'); //layui渲染
  154. var xtree_items = _this.getByClassName('layui-xtree-item');
  155. var xtree_icons = _this.getByClassName('layui-xtree-icon');
  156. var xtree_nullicons = _this.getByClassName('layui-xtree-icon-null');
  157. for (var i = 0; i < xtree_items.length; i++) {
  158. if (xtree_items[i].parentNode == _this._container)
  159. xtree_items[i].style.margin = '5px 0 0 10px';
  160. else {
  161. xtree_items[i].style.margin = '5px 0 0 45px';
  162. if (!_this._isopen) xtree_items[i].style.display = 'none';
  163. }
  164. }
  165. for (var i = 0; i < xtree_icons.length; i++) {
  166. xtree_icons[i].style.position = "relative";
  167. xtree_icons[i].style.top = "3px";
  168. xtree_icons[i].style.margin = "0 5px 0 0";
  169. xtree_icons[i].style.fontSize = "18px";
  170. xtree_icons[i].style.color = _this._isopen ? _this._iconOpenColor : _this._iconCloseColor;
  171. xtree_icons[i].style.cursor = "pointer";
  172. xtree_icons[i].onclick = function () {
  173. var xtree_chi = this.parentNode.childNodes;
  174. if (this.getAttribute('data-xtree') == 1) {
  175. for (var j = 0; j < xtree_chi.length; j++) {
  176. if (xtree_chi[j].getAttribute('class') == 'layui-xtree-item')
  177. xtree_chi[j].style.display = 'none';
  178. }
  179. this.setAttribute('data-xtree', '0')
  180. this.innerHTML = _this._iconClose;
  181. this.style.color = _this._iconCloseColor;
  182. } else {
  183. for (var j = 0; j < xtree_chi.length; j++) {
  184. if (xtree_chi[j].getAttribute('class') == 'layui-xtree-item')
  185. xtree_chi[j].style.display = 'block';
  186. }
  187. this.setAttribute('data-xtree', '1')
  188. this.innerHTML = _this._iconOpen;
  189. this.style.color = _this._iconOpenColor;
  190. }
  191. }
  192. }
  193. for (var i = 0; i < xtree_nullicons.length; i++) {
  194. xtree_nullicons[i].style.position = "relative";
  195. xtree_nullicons[i].style.top = "3px";
  196. xtree_nullicons[i].style.margin = "0 5px 0 0";
  197. xtree_nullicons[i].style.fontSize = "18px";
  198. xtree_nullicons[i].style.color = _this._iconEndColor;
  199. }
  200. _this._form.on('checkbox(xtreeck' + _this._containerid + ')', function (da) {
  201. //获取当前点击复选框的容器下面的所有子级容器
  202. var xtree_chis = da.elem.parentNode.getElementsByClassName('layui-xtree-item');
  203. //遍历它们,选中状态与它们的父级一致(类似全选功能)
  204. for (var i = 0; i < xtree_chis.length; i++) {
  205. if (!_this.getChildByClassName(xtree_chis[i], 'layui-xtree-checkbox')[0].disabled) {
  206. _this.getChildByClassName(xtree_chis[i], 'layui-xtree-checkbox')[0].checked = da.elem.checked;
  207. if (da.elem.checked) _this.getChildByClassName(xtree_chis[i], 'layui-xtree-checkbox')[0].nextSibling.classList.add('layui-form-checked');
  208. else _this.getChildByClassName(xtree_chis[i], 'layui-xtree-checkbox')[0].nextSibling.classList.remove('layui-form-checked');
  209. }
  210. }
  211. _this.ParendCheck(da.elem);
  212. _this._click(da);
  213. });
  214. var _xtree_disableds = _this.getByClassName('layui-disabled');
  215. for (var i = 0; i < _xtree_disableds.length; i++) {
  216. _xtree_disableds[i].getElementsByTagName('span')[0].style.color = "#B5B5B5";
  217. }
  218. //全选按钮
  219. if (_this._ckall) {
  220. _this._form.on('checkbox(xtreeckall' + _this._containerid + ')', function (data) {
  221. var xtree_allck = data.elem.parentNode.parentNode.getElementsByClassName('layui-form-checkbox');
  222. for (var i = 0; i < xtree_allck.length; i++) {
  223. if (xtree_allck[i].getAttribute('class').indexOf('layui-checkbox-disbaled') == -1) {
  224. if (data.elem.checked) {
  225. xtree_allck[i].classList.add('layui-form-checked');
  226. }
  227. else {
  228. xtree_allck[i].classList.remove('layui-form-checked');
  229. }
  230. xtree_allck[i].parentNode.getElementsByClassName('layui-xtree-checkbox')[0].checked = data.elem.checked;
  231. }
  232. }
  233. _this._ckallSuccess();
  234. });
  235. }
  236. }
  237. //更新渲染
  238. layuiXtree.prototype.render = function () {
  239. var _this = this;
  240. _this.Loading(_this._options);
  241. }
  242. //子节点选中改变,父节点更改自身状态
  243. layuiXtree.prototype.ParendCheck = function (ckelem) {
  244. var _this = this;
  245. var xtree_p = ckelem.parentNode.parentNode;
  246. if (xtree_p.getAttribute('class') == 'layui-xtree-item') {
  247. var xtree_all = _this.getChildByClassName(xtree_p, 'layui-xtree-item');
  248. var xtree_count = 0;
  249. for (var i = 0; i < xtree_all.length; i++) {
  250. if (_this.getChildByClassName(xtree_all[i], 'layui-xtree-checkbox')[0].checked) {
  251. xtree_count++;
  252. }
  253. }
  254. if (xtree_count <= 0) {
  255. _this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0].checked = false;
  256. _this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0].nextSibling.classList.remove('layui-form-checked');
  257. } else {
  258. _this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0].checked = true;
  259. _this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0].nextSibling.classList.add('layui-form-checked');
  260. }
  261. this.ParendCheck(_this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0]);
  262. }
  263. }
  264. //渲染之前按照选中的末级去改变父级选中状态
  265. layuiXtree.prototype.ParentCheckboxChecked = function (e) {
  266. var _this = this;
  267. if (e.parentNode.parentNode.getAttribute('class') == 'layui-xtree-item') {
  268. var _pe = _this.getChildByClassName(e.parentNode.parentNode, 'layui-xtree-checkbox')[0];
  269. _pe.checked = true;
  270. _this.ParentCheckboxChecked(_pe);
  271. }
  272. }
  273. //获取全部选中的末级checkbox对象
  274. layuiXtree.prototype.GetChecked = function () {
  275. var _this = this;
  276. var arr = new Array();
  277. var arrIndex = 0;
  278. var cks = _this.getByClassName('layui-xtree-checkbox');
  279. for (var i = 0; i < cks.length; i++) {
  280. // && cks[i].getAttribute('data-xend') == '1' 意思是去掉父类
  281. if (cks[i].checked ) {
  282. arr[arrIndex] = cks[i];
  283. arrIndex++;
  284. }
  285. }
  286. return arr;
  287. }
  288. //获取全部的原始checkbox对象
  289. layuiXtree.prototype.GetAllCheckBox = function () {
  290. var _this = this;
  291. var arr = new Array();
  292. var arrIndex = 0;
  293. var cks = _this.getByClassName('layui-xtree-checkbox');
  294. for (var i = 0; i < cks.length; i++) {
  295. arr[arrIndex] = cks[i]; arrIndex++;
  296. }
  297. return arr;
  298. }
  299. //根据值来获取其父级的checkbox原dom对象
  300. layuiXtree.prototype.GetParent = function (a) {
  301. var _this = this;
  302. var cks = _this.getByClassName('layui-xtree-checkbox');
  303. for (var i = 0; i < cks.length; i++) {
  304. if (cks[i].value == a) {
  305. if (cks[i].parentNode.parentNode.getAttribute('id') == _this._container.getAttribute('id')) return null;
  306. return _this.getChildByClassName(cks[i].parentNode.parentNode, 'layui-xtree-checkbox')[0];
  307. }
  308. }
  309. return null;
  310. }