无法读取属性“匹配”(nestedSortable)

时间:2015-02-13 12:01:47

标签: javascript jquery nested-sortable tree-structure

我有一个树结构,我试图用jQuery nestedSortable plugin排序。

在这里,您可以看到我的代码和一个工作示例:

$(document).ready(function(){
		$('.sortable').nestedSortable({
			handle: 'div',
			items: 'li',
			toleranceElement: '> div',
            change: function(){
                console.log('moved!');
                console.log(this);
                console.log($(this));
                var arraied = $(this).nestedSortable('toArray', {startDepthCount: 0});          
                arraied = dump(arraied);
                console.log(arraied);
            }
		});

	});
    
    function dump(arr,level) {
      var dumped_text = "";
      if(!level) level = 0;
      //The padding given at the beginning of the line.
      var level_padding = "";
      for(var j=0;j<level+1;j++) level_padding += "    ";

      if(typeof(arr) == 'object') { //Array/Hashes/Objects
          for(var item in arr) {
              var value = arr[item];

              if(typeof(value) == 'object') { //If it is an array,
                  dumped_text += level_padding + "'" + item + "' ...\n";
                  dumped_text += dump(value,level+1);
              } else {
                  dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
              }
          }
      } else { //Strings/Chars/Numbers etc.
          dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
      }
      return dumped_text;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
<script src="http://mjsarfatti.com/sandbox/nestedSortable/jquery.mjs.nestedSortable.js"></script>
<ol class="sortable">
	<li><div>List line 1</div></li>
	<li>
		<div>List line 2</div>
		<ol>
			<li><div>List line 3</div></li>
			<li><div>List line 4</div></li>
		</ol>
	</li>
	<li><div>List line 5</div></li>
</ol>

您可以在树周围移动节点,以便正常工作。不幸的是,我无法获得响应数组,因此我可以更新我的数据库值。

如果你在移动这些微小的节点时检查浏览器控制台,你可以看到我经常收到错误:

  

“未捕获的TypeError:无法读取未定义的属性'匹配'。

这个错误可能是什么原因,同样重要的是,如何在树调整后获得数组?

1 个答案:

答案 0 :(得分:2)

在jquery.mjs.nestedSortable v2.0中),替换第769行:

id = ($(item).attr(o.attribute || "id")).match(o.expression || (/(.+)[-=_](.+)/));

使用:

id = ($(item).attr(o.attribute || "id") ||'').match(o.expression || (/(.+)[-=_](.+)/));