您好,登錄后才能下訂單哦!
這篇文章主要介紹了layui中樹形關(guān)于取值傳值問題的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體如下:
這個(gè)是我們需要的效果,實(shí)際操作中會(huì)先執(zhí)行渲染 然后在執(zhí)行方法,然后我們發(fā)現(xiàn)樹形的JSON是空,調(diào)試了N遍一直以為是優(yōu)先級(jí)別的問題了。
最后解決方案是
<script type="text/javascript"> ////layui 的 form 模塊 var form = ""; layui.use(['form'], function () { // $ = layui.jquery; form = layui.form; //獲取節(jié)點(diǎn)數(shù)據(jù) getTreeData(); //return false; }); function getTreeData() { $.ajax({ //async: false, type: "post", url: "/api/WebFW//getOrgTree", datatype: "json", contenttype: "application/json; charset=utf-8", success: function (jdata) { var xtree1 = new layuiXtree({ elem: 'xtree1', form: form, data: strToJson(jdata), isopen: true, //false初始關(guān)閉,true打開 click: function (data) { //節(jié)點(diǎn)選中狀態(tài)改變事件監(jiān)聽,全選框有自己的監(jiān)聽事件 console.log(data.elem); //得到checkbox原始DOM對(duì)象 console.log(data.elem.checked); //是否選中,true選中 alert(data.value); //彈出value值 } }); //獲取選中val document.getElementById('btn1').onclick = function () { var oCks = xtree1.GetChecked(); for (var i = 0; i < oCks.length; i++) { alert(oCks[i].value); } } //子節(jié)點(diǎn)選中改變,父節(jié)點(diǎn)更改自身狀態(tài) layuiXtree.prototype.ParendCheck = function (ckelem) { var _this = this; var xtree_p = ckelem.parentNode.parentNode; if (xtree_p.getAttribute('class') == 'layui-xtree-item') { var xtree_all = _this.getChildByClassName(xtree_p, 'layui-xtree-item'); var xtree_count = 0; for (var i = 0; i < xtree_all.length; i++) { if (_this.getChildByClassName(xtree_all[i], 'layui-xtree-checkbox')[0].checked) { xtree_count++; } } if (xtree_count <= 0) { _this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0].checked = false; _this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0].nextSibling.classList.remove('layui-form-checked'); } else { _this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0].checked = true; _this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0].nextSibling.classList.add('layui-form-checked'); } this.ParendCheck(_this.getChildByClassName(xtree_p, 'layui-xtree-checkbox')[0]); } } //渲染之前按照選中的末級(jí)去改變父級(jí)選中狀態(tài) layuiXtree.prototype.ParentCheckboxChecked = function (e) { var _this = this; if (e.parentNode.parentNode.getAttribute('class') == 'layui-xtree-item') { var _pe = _this.getChildByClassName(e.parentNode.parentNode, 'layui-xtree-checkbox')[0]; _pe.checked = true; _this.ParentCheckboxChecked(_pe); } } //獲取全部選中的末級(jí)checkbox對(duì)象 layuiXtree.prototype.GetChecked = function () { var _this = this; var arr = new Array(); var arrIndex = 0; var cks = _this.getByClassName('layui-xtree-checkbox'); for (var i = 0; i < cks.length; i++) { if (cks[i].checked && cks[i].getAttribute('data-xend') == '1') { arr[arrIndex] = cks[i]; arrIndex++; } } return arr; } //獲取全部的原始checkbox對(duì)象 layuiXtree.prototype.GetAllCheckBox = function () { var _this = this; var arr = new Array(); var arrIndex = 0; var cks = _this.getByClassName('layui-xtree-checkbox'); for (var i = 0; i < cks.length; i++) { arr[arrIndex] = cks[i]; arrIndex++; } return arr; } //根據(jù)值來獲取其父級(jí)的checkbox原dom對(duì)象 layuiXtree.prototype.GetParent = function (a) { var _this = this; var cks = _this.getByClassName('layui-xtree-checkbox'); for (var i = 0; i < cks.length; i++) { if (cks[i].value == a) { if (cks[i].parentNode.parentNode.getAttribute('id') == _this._container.getAttribute('id')) return null; return _this.getChildByClassName(cks[i].parentNode.parentNode, 'layui-xtree-checkbox')[0]; } } return null; } } }); } function strToJson(str) { var json = (new Function("return " + str))(); return json; } </script>
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“l(fā)ayui中樹形關(guān)于取值傳值問題的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。