layui中的樹形關於取值傳值問題解決方法
本文主要介紹了詳解layui中的樹形關於取值傳值問題,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧,希望能幫助大家
這個是我們需要的效果,實際操作中會先執行渲染然後在執行方法,然後我們發現樹形的JSON是空,調試了N遍一直以為是優先的問題了。
最後解決方案是
<script type="text/javascript"> ////layui 的 form 模块 var form = ""; layui.use(['form'], function () { // $ = layui.jquery; form = layui.form; //获取节点数据 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初始关闭,true打开 click: function (data) { //节点选中状态改变事件监听,全选框有自己的监听事件 console.log(data.elem); //得到checkbox原始DOM对象 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); } } //子节点选中改变,父节点更改自身状态 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]); } } //渲染之前按照选中的末级去改变父级选中状态 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); } } //获取全部选中的末级checkbox对象 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对象 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; } //根据值来获取其父级的checkbox原dom对象 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>
相關推薦:
關於Vue.js表單標籤中的單選按鈕、複選按鈕和下拉清單的取值詳解
#以上是layui中的樹形關於取值傳值問題解決方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

layui 登入頁面跳轉設定步驟:新增跳轉代碼:在登入表單提交按鈕點選事件中新增判斷,成功登入後透過 window.location.href 跳到指定頁面。修改 form 配置:在 lay-filter="login" 的 form 元素中新增 hidden 輸入字段,name 為 "redirect",value 為目標頁面位址。

layui 提供了多種取得表單資料的方法,包括直接取得表單所有欄位資料、取得單一表單元素值、使用formAPI.getVal() 方法取得指定欄位值、將表單資料序列化並作為AJAX 請求參數,以及監聽表單提交事件獲取資料。

透過使用layui框架的響應式佈局功能,可以實現自適應佈局。步驟包括:引用layui框架。定義自適應佈局容器,設定layui-container類別。使用響應式斷點(xs/sm/md/lg)隱藏特定斷點下的元素。利用網格系統(layui-col-)指定元素寬度。透過偏移量(layui-offset-)建立間距。使用響應式實用工具(layui-invisible/show/block/inline)控制元素的可見性和顯示方式。

使用 layui 傳輸資料的方法如下:使用 Ajax:建立請求對象,設定請求參數(URL、方法、資料),處理回應。使用內建方法:使用 $.post、$.get、$.postJSON 或 $.getJSON 等內建方法簡化資料傳輸。

layui與Vue的差異主要體現在功能和關注點上。 layui專注於快速開發UI元素,提供預製元件簡化頁面建置;而Vue則是全端框架,注重資料綁定、元件化開發和狀態管理,更適合建構複雜應用程式。 layui學習簡單,適合快速建立頁面;Vue學習曲線陡峭,但有助於建立可擴展且易於維護的應用程式。根據專案需求和開發者技能水平,可以選擇合適的框架。

若要執行 layui,請執行以下步驟:1. 匯入 layui 腳本;2. 初始化 layui;3. 使用 layui 元件;4. 匯入 layui 樣式(可選);5. 確保腳本相容並注意其他注意事項。透過這些步驟,您就可以使用 layui 的強大功能來建立 web 應用程式。

layui是一個前端UI框架,它提供了豐富的UI元件、工具和功能,幫助開發人員快速建立現代化、響應式和互動式Web應用程序,特點包括:靈活輕量、模組化設計、豐富的元件、強大的工具和易於自訂。它廣泛應用於各種Web應用程式的開發中,包括管理系統、電商平台、內容管理系統、社交網路和行動裝置應用程式。

layui和vue是前端框架,layui是一種輕量級的函式庫,提供UI元件和工具;vue是一個全面的框架,提供UI元件、狀態管理、資料綁定和路由等功能。 layui基於模組化的架構,vue是基於組件化的架構。 layui擁有較小的生態系統,vue擁有龐大且活躍的生態系統。 layui學習曲線較低,vue學習曲線較陡。 layui適用於小型專案和快速開發UI元件,vue適用於大型專案和需要豐富功能的場景。
