2016-03-04 45 views
0

所以問題是自己說的。我做了一個例子here設置拖放子節點來顯示錶中的json數組

或或看這個代碼:

HTML

<div id="jstree"> 
     <ul> 
     <li>Root 
     <ul> 
      <li>Parent1 
       <ul> 
        <li>Child1</li> 
        <li>Child2</li> 
       </ul> 
      </li> 
      <li>Parent2 
       <ul> 
        <li>Child1</li> 
        <li>Child2</li> 
       </ul> 
      </li> 
     </ul> 
     </li> 
     </ul> 
    </div> 
<div class="form-group"> 
<input id="left" type="file" class="file" data-upload-url="/upload"> 
</div> 

JS

var array = [ 
    { 
     "name": "Parent1", 
     "id": "1", 
     "description": "An extended Bootstrap table with radio, checkbox, sort, pagination, and other added features. (supports twitter bootstrap v2 and v3) " 
    }, 
    { 
     "name": "Parent2", 
     "id": "2", 
     "description": "A jQuery plugin to select multiple elements with checkboxes :)" 
    }, 
    { 
     "name": "Parent1", 
     "id": "3", 
     "description": "Show/hide password plugin for twitter bootstrap." 
    } 
]; 

var array2 = [ 
{ 
     "subname": "Parent101", 
     "subid": "101", 
     "description": "An extended Bootstrap table with radio, checkbox, sort, pagination, and other added features. (supports twitter bootstrap v2 and v3) " 
    }, 
    { 
     "subname": "Parent202", 
     "subid": "202", 
     "description": "A jQuery plugin to select multiple elements with checkboxes :)" 
    }, 
    { 
     "subname": "Parent101", 
     "subid": "303", 
     "description": "Show/hide password plugin for twitter bootstrap." 
    } 
]; 
var $table = $('#table'); 
var $study = $('#jstree'); 

$(function() { 

    $table.bootstrapTable({ 
     formatNoMatches: function() { 
      return "This table is empty..."; 
     } 
    }); 


    $('#jstree') 
     .on('select_node.jstree', function(event, data){ 
      // ===== Initialize parent ===== 
      var loMainSelected = data; 
      uiGetParents(loMainSelected); 
      function uiGetParents(node) { 
       try { 
        var level = node.node.parents.length; 
        var elem = $('#' + node.node.id); 
        var parent = node.node.text; 
        for (var ln = 0; ln <= level - 1; ln++) { 
         elem = elem.parent(); 
         var child = elem.children()[-1]; 
         if (child != undefined) { 
          parent = child.text; 
         } 
        } 
        console.log(parent); 
       } 
       catch (err) { 
        console.log('Error in uiGetParents'); 
       } 
      } 
      // ===== Click event on node ===== 
      for(var i = 0; i < data.selected.length; i++) { 
       var node = data.instance.get_node(data.selected[i]).text; 
       if (node == "Child1") { 
       $(function() { 
         $table.bootstrapTable('refreshOptions', 
         { 
          data: array, 
          columns: [ 
           { 
            title:"Name", 
            field:"name" 
           }, 
           { 
            title:"Id", 
            field:"id" 
           }, 
           { 
            title:"Description", 
            field:"description" 
           } 
          ] 
         }); 
         }); 
       } 
       else if (node == "Child2"){ 
       $(function() { 
         $table.bootstrapTable('refreshOptions', 
         { 
          data: array2, 
          columns: [ 
           { 
            title:"Subname", 
            field:"subname" 
           }, 
           { 
            title:"Subid", 
            field:"subid" 
           }, 
           { 
            title:"Description", 
            field:"description" 
           } 
          ] 
         }); 
         }); 
       } 
      } 
     }) 
     .jstree({ 
     "core" : { 
      "themes": { 
       "url": true, 
       "icons": true, 
       "dots": true 
      } 
     } 
    }); 
    }); 

我想創建拖放節點,所以用戶可以將它拖到並將其放入可丟棄的窗口中,他將看到帶有數據的表格。另外一切正常。 Jstree正在加載,點擊eventHandler的效果很好,你可以通過點擊來查看錶格,甚至拖放窗口也能正常工作,並顯示每個用戶將放入其中的文件,但如何連接所有這些東西,有沒有人想法?

+0

您是否解決了您的問題? –

+0

@NikolayErmakov nope仍然有一些問題 – Anton

回答

1

爲此,您將不得不使用dnd插件並聽取如下的拖放事件。檢查演示 - Fiddle

$(document).on('dnd_stop.vakata', function(e, data) { 
    for (var i = 0; i < data.data.nodes.length; i++) { 
     var node = $('#jstree').jstree().get_node(data.data.nodes[i]).text; 
     if (node == "Child1") { 

      $table.bootstrapTable('refreshOptions', { 
       data: array, 
       columns: [{ 
        title: "Name", 
        field: "name" 
       }, { 
        title: "Id", 
        field: "id" 
       }, { 
        title: "Description", 
        field: "description" 
       }] 
      }); 

     } else if (node == "Child2") { 

      $table.bootstrapTable('refreshOptions', { 
       data: array2, 
       columns: [{ 
        title: "Subname", 
        field: "subname" 
       }, { 
        title: "Subid", 
        field: "subid" 
       }, { 
        title: "Description", 
        field: "description" 
       }] 
      }); 

     } 
    } 
}); 
+0

我在我的頁面上使用這個插件,只是忘了提供它在樣本..我仍然可以弄清楚如何聽它 – Anton

+0

你檢查演示?那是你要的嗎? –

+0

是的,我檢查了演示,它不是我正在尋找的,我希望當我拖動節點child1並將其拖放到拖放窗口時,它會向我顯示此窗口中的表格 – Anton