Home > Web Front-end > JS Tutorial > Issues related to value-passing in layui

Issues related to value-passing in layui

亚连
Release: 2018-06-11 17:18:01
Original
3939 people have browsed it

This article mainly introduces the detailed explanation of the tree shape in layui about the value transfer problem. Now I will share it with you and give you a reference.

This article introduces the problem of value-passing in the tree shape in layui. I would like to share it with you. The details are as follows:

This is the effect we need. In actual operation, rendering will be performed first and then the method will be executed. Then we found that the tree-shaped JSON was empty. After debugging it N times, we thought it was a priority issue.

The final solution is

<script type="text/javascript">
  ////layui 的 form 模块
  var form = "";
  layui.use([&#39;form&#39;], 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: &#39;xtree1&#39;,
          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(&#39;btn1&#39;).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(&#39;class&#39;) == &#39;layui-xtree-item&#39;) {
            var xtree_all = _this.getChildByClassName(xtree_p, &#39;layui-xtree-item&#39;);
            var xtree_count = 0;
            for (var i = 0; i < xtree_all.length; i++) {
              if (_this.getChildByClassName(xtree_all[i], &#39;layui-xtree-checkbox&#39;)[0].checked) {
                xtree_count++;
              }
            }
            if (xtree_count <= 0) {
              _this.getChildByClassName(xtree_p, &#39;layui-xtree-checkbox&#39;)[0].checked = false;
              _this.getChildByClassName(xtree_p, &#39;layui-xtree-checkbox&#39;)[0].nextSibling.classList.remove(&#39;layui-form-checked&#39;);
            } else {
              _this.getChildByClassName(xtree_p, &#39;layui-xtree-checkbox&#39;)[0].checked = true;
              _this.getChildByClassName(xtree_p, &#39;layui-xtree-checkbox&#39;)[0].nextSibling.classList.add(&#39;layui-form-checked&#39;);
            }
            this.ParendCheck(_this.getChildByClassName(xtree_p, &#39;layui-xtree-checkbox&#39;)[0]);
          }
        }

        //渲染之前按照选中的末级去改变父级选中状态
        layuiXtree.prototype.ParentCheckboxChecked = function (e) {
          var _this = this;
          if (e.parentNode.parentNode.getAttribute(&#39;class&#39;) == &#39;layui-xtree-item&#39;) {
            var _pe = _this.getChildByClassName(e.parentNode.parentNode, &#39;layui-xtree-checkbox&#39;)[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(&#39;layui-xtree-checkbox&#39;);
          for (var i = 0; i < cks.length; i++) {
            if (cks[i].checked && cks[i].getAttribute(&#39;data-xend&#39;) == &#39;1&#39;) {
              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(&#39;layui-xtree-checkbox&#39;);
          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(&#39;layui-xtree-checkbox&#39;);
          for (var i = 0; i < cks.length; i++) {
            if (cks[i].value == a) {
              if (cks[i].parentNode.parentNode.getAttribute(&#39;id&#39;) == _this._container.getAttribute(&#39;id&#39;)) return null;
              return _this.getChildByClassName(cks[i].parentNode.parentNode, &#39;layui-xtree-checkbox&#39;)[0];
            }
          }
          return null;
        }
      }
    });
  }

  function strToJson(str) {
    var json = (new Function("return " + str))();
    return json;
  }
</script>
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Use vue-router to build the bottom navigation bar in vue-cli (detailed tutorial)

How to use Vue -Router mode and hook (detailed tutorial)

How to use the radio in angularJS to choose one of the two options (detailed tutorial)

Get the value method of the default selected radio button in angularjs (detailed tutorial)

Comprehensive interpretation of cli in vue (detailed tutorial)

How to dynamically change radio status through js (detailed tutorial)

The above is the detailed content of Issues related to value-passing in layui. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template