1. 程式人生 > >easyui中combotree迴圈獲取父節點至根節點並輸出路徑

easyui中combotree迴圈獲取父節點至根節點並輸出路徑

前臺頁面:

<pre name="code" class="html"><td style="height: 35px" colspan="7">
      <input id="fm_AEType" class="easyui-combotree" style="width: 240px" />
      <label id="fm_AETypePath" />
</td></pre>

JavaScript頁面(包括資料初始化):
initAEType: function () {
    $.ajax({
        url: AEActionUrl + '?action=listaetype&ParentType=',
        dataType: 'json',
        success: function (jsonstr) {
            $('#fm_AEType').combotree({
                data: jsonstr,
                editable: false,
                //lines: true,
                valueField: 'AE_TYPE_ID',
                textField: 'AE_TYPE_NAME',
                onLoadSuccess: function () {
                    $('#fm_AEType').combotree('tree').tree("collapseAll");
                },
                onSelect: function (item) {
                    var parent = item;
                    var tree = $('#fm_AEType').combotree('tree');
                    var path = new Array();
                    do {
                        path.unshift(parent.text);
                        var parent = tree.tree('getParent', parent.target);
                    } while (parent);
                    var pathStr = '';
                    for (var i = 0; i < path.length; i++) {
                        pathStr += path[i];
                        if (i < path.length - 1) {
                            pathStr += ' - ';
                        }
                    }
                    $('#fm_AETypePath').text(pathStr);
                }
            });
        }
    });
}

如上,關鍵程式碼在onSelect事件中。