javascript - 有没有办法知道 JSON 树中哪个节点发生了更改?

标签 javascript jquery json

我有一个jqtree我应该使用 AJAX 发送哪些节点被更改,为了做这样的事情,我使用了递归函数来读取整个 JSON,问题是,我只想发送哪个节点受到影响。由于树是拖放式的,用户一次只能更改一项。

澄清:

页面已加载,结构也已加载(检查 firebug): enter image description here

然后,用户选择 child1 并将其拖动到 node2 中: enter image description here

通过生成新的 JSON,然后将其分离并分类为 vpais(父级)和 vfilhos(子级) enter image description here

然而,正如你所看到的,没有必要发送所有的parentschildren,因为只有一个节点会被改变(而且它总是会改变,仅仅因为只有一次可以拖动一个项目)。 有没有办法知道哪个节点被拖动及其新的

感谢您到目前为止的努力:)

我的代码:

$(document).ready(function() {

  var data = [{
    label: 'node1',
    id: 1,
    children: [{
      label: 'child1',
      id: 2
    }, {
      label: 'child3',
      id: 3
    }, {
      label: 'child2',
      id: 4
    }, {
      label: 'child2',
      id: 5
    }]
  }, {
    label: 'node2',
    id: 6,
    children: [{
      label: 'child3',
      id: 7
    }]

  }];



  $('#tree1').tree({
    data: data,
    autoOpen: false,
    dragAndDrop: true
  });

  console.log("Original Structure" + $('#tree1').tree('toJson'));

  $('#tree1').bind(
    'tree.move',
    function(event) {
      data = $(this).tree('toJson');
      event.preventDefault();
      event.move_info.do_move();
      console.log("New Structure" + $(this).tree('toJson'));

      data = $(this).tree('toJson');
      var dadSon = [];
      var dad = [],
        son = [];
      var group = "";
      var randomic = "";

      (function printDadSon(data, parent) {
        if (!data) return;
        for (var i = 0; i < (data.length); i++) {
          if (parent && parent != 'undefined') {
            dadSon[i] = ('vpai= ' + parent + "&" + 'vfilho= ' + data[i].id + "&");
            group += dadSon[i];
          }
          printDadSon(data[i].children, data[i].id);
        }
      })(JSON.parse(data));


      var temp = group.length;
      group = group.substring(0, temp - 1);

      console.log(dadSon);

      $.ajax({
        type: 'POST',
        url: 'sistema.agrosys.com.br',
        dataType: 'json',
        data: group
      });
      console.log("Done");
    }
  );

});
#navdata {
  width: auto;
  height: auto;
  flex: 1;
  padding-bottom: 1px;
}
#navgrid {
  width: 50%;
  height: 200px;
  overflow-x: visible;
  overflow-y: scroll;
  border: solid 1px #79B7E7;
  background-color: white;
}
#header {
  background-color: #79B7E7;
  width: 99.6%;
  text-align: center;
  border: 1px solid white;
  margin: 1px;
}
.jqtree-element {
  background-color: white;
  border: 1px solid white;
  height: 23px;
  color: red;
}
.jqtree-tree .jqtree-title {
  color: black;
}
ul.jqtree-tree {
  margin-top: 0px;
  margin-left: 1px;
}
ul.jqtree-tree,
ul.jqtree-tree ul.jqtree_common {
  list-style: none outside;
  margin-bottom: 0;
  padding: 0;
}
ul.jqtree-tree ul.jqtree_common {
  display: block;
  text-align: left;
  padding-left: 0px;
  margin-left: 20px;
  margin-right: 0;
}
ul.jqtree-tree li.jqtree-closed > ul.jqtree_common {
  display: none;
}
ul.jqtree-tree li.jqtree_common {
  clear: both;
  list-style-type: none;
}
ul.jqtree-tree .jqtree-toggler {
  color: #325D8A;
}
ul.jqtree-tree .jqtree-toggler:hover {
  color: #3966df;
  text-decoration: none;
}
.jqtree-tree .jqtree-title.jqtree-title-folder {
  margin-left: 0;
}
span.jqtree-dragging {
  color: #fff;
  background: #79B7E7;
  opacity: 0.8;
  cursor: pointer;
  padding: 2px 8px;
}
ul.jqtree-tree li.jqtree-selected > .jqtree-element,
ul.jqtree-tree li.jqtree-selected > .jqtree-element:hover {
  background: -webkit-gradient(linear, left top, left bottom, from(#BEE0F5), to(#79B7E7));
}
<!DOCTYPE html>
<!-- Programa: JqTree | Envia nova estrutura JSON como v pai e vfilho -->
<!--    Autor: Calne Ricardo de Souza -->
<!-- 	 Data: 06/07/2015 -->
<html>

<head>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script src="http://sistema.agrosys.com.br/sistema/labs/tree.jquery.js"></script>
  <link rel="stylesheet" href="http://sistema.agrosys.com.br/sistema/labs/jqtree.css">
  <script src="http://sistema.agrosys.com.br/sistema/labs/jquery-cookie/src/jquery.cookie.js"></script>

</head>

<body>
  <div id="navgrid">
    <div id="header">Header</div>
    <div id="tree1">
      <ul class="jqtree_common jqtree-tree">
        <li class="jqtree_common jqtree-folder">
          <div class="jqtree-element jqtree_common">
            <a class="jqtree_common jqtree-toggler">â–¼</a>
            <span class="jqtree_common jqtree-title jqtree-title-folder">node1</span>
          </div>
          <ul class="jqtree_common ">
            <li class="jqtree_common">
              <div class="jqtree-element jqtree_common">
                <span class="jqtree-title jqtree_common">child2</span>
              </div>
            </li>
            <li class="jqtree_common">
              <div class="jqtree-element jqtree_common">
                <span class="jqtree-title jqtree_common">child1</span>
              </div>
            </li>
          </ul>
        </li>
        <li class="jqtree_common jqtree-folder">
          <div class="jqtree-element jqtree_common">
            <a class="jqtree_common jqtree-toggler">â–¼</a>
            <span class="jqtree_common jqtree-title jqtree-title-folder">node2</span>
          </div>
          <ul class="jqtree_common ">
            <li class="jqtree_common">
              <div class="jqtree-element jqtree_common">
                <span class="jqtree-title jqtree_common">child3</span>
              </div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
  </div>
</body>

</html>

最佳答案

我认为mutation observers在这里工作得很好。

if (typeof(MutationObserver) != "undefined")
{
    //this is new functionality
    //observer is present in firefox/chrome and IE11+
    // select the target node

        // create an observer instance
        observer = new MutationObserver(observeJSONTree);
        // configuration of the observer:
        var config = { attributes: false, childList: true, characterData: false, subtree: true };

        // pass in the target node, as well as the observer options
        observer.observe([tree container], config);


}

function observerJSONTree(e)
{

            for (eventObject in e)
            {

                switch(e[eventObject].type)
                {
                    case 'characterData' :
                        //when text is changed
                        //for now do nothing

                    break;

                    case 'childList' :
                        //childs added or removed
                        if (e[eventObject].addedNodes.length > 0)
                        {
                            //childs added
                            //do something
                        }
                        if (e[eventObject].removedNodes.length > 0)
                        {
                            //childs removed

                        }
                    break;

                }
            }
}

config 对象决定应该监听哪些事件。在本例中,我们监听树容器中子节点的更改。

当检测到变化时,它将触发对指定函数的突变观察者调用。在这种情况下,有关所做编辑的数据将被存储。对于子拖/放来说,这是两个事件。子项移除和子项追加。

关于javascript - 有没有办法知道 JSON 树中哪个节点发生了更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31919156/

相关文章:

javascript - jQuery:获取前一个元素减1

javascript - 在 JavaScript 位置查找下一个单词的位置

javascript - 如何使用 dataType 作为脚本发送 AJAX?

ios - 奇怪的 JSON API 响应 SWIFT

Javascript 数组过滤器上的多个条件,无需 for 循环

javascript - 我的 HTML5 Web 应用程序崩溃了,我不知道如何调试

jquery - 弹出窗口不适用于 Shiny 的 0.9

javascript - 切换菜单不工作 JQuery

Python:从文件中读取和替换字符串(带有特殊字符)时出错

jquery - 将 json 数据从 Controller 传递到 View