2017-02-21 5 views
1

Как создать древовидную структуру с опцией checkbox в react js. checkbox должно быть расположено в правой части дерева.React Tree view with checkbox option

Ниже json образец для создания treeiew:

var json = [ 
     { 
     "text": "Parent 1", 
     "nodes": [ 
     { 
      "text": "Child 1", 
      "nodes": [ 
       { 
       "text": "Grandchild 1" 
       }, 
       { 
       "text": "Grandchild 2" 
       } 
      ] 
      }, 
      { 
      "text": "Child 2" 
      } 
     ] 
     }, 
     { 
     "text": "Parent 2" 
     }, 
     { 
     "text": "Parent 3" 
     }, 
     { 
     "text": "Parent 4" 
     }, 
     { 
     "text": "Parent 5" 
     } 
    ]; 

ответ

0

Используйте эту рекурсивную функцию для достижения этой цели:

_createList(item, margin){ 
     return item.map((el,i)=>{ 
      return <div key={i} style={{marginLeft: margin}}> 
        {el.text} 
        <input type='checkbox'/> 
        {el.nodes && el.nodes.length ? this._createList(el.nodes, 10) : null} 
       </div> 
     }) 
    } 

    render() { 
    return (
     <div > 
      {this._createList(json, 0)} 
     </div> 
    ) 
    } 

Проверьте рабочее fiddle: https://jsfiddle.net/mayankshukla5031/rm49r11a/

var json = [ 
 
     { 
 
     "text": "Parent 1", 
 
     "nodes": [ 
 
     { 
 
      "text": "Child 1", 
 
      "nodes": [ 
 
       { 
 
       "text": "Grandchild 1", 
 
       "nodes": [ 
 
        { 
 
         'text': 'G GrandChild1.1', 
 
         'nodes': [ 
 
          { 
 
           'text': 'G G GrandChild1.1.1' 
 
          } 
 
         ] 
 
        }, 
 
        { 
 
         \t text: 'G GrandChild1.2' 
 
        } 
 
       ] 
 
       }, 
 
       { 
 
       "text": "Grandchild 2" 
 
       } 
 
      ] 
 
      }, 
 
      { 
 
      "text": "Child 2" 
 
      } 
 
     ] 
 
     }, 
 
     { 
 
     "text": "Parent 2" 
 
     }, 
 
     { 
 
     "text": "Parent 3" 
 
     }, 
 
     { 
 
     "text": "Parent 4" 
 
     }, 
 
     { 
 
     "text": "Parent 5" 
 
     } 
 
    ]; 
 
class App extends React.Component { 
 
    constructor() { 
 
    super(); 
 
    this.state = { 
 
    }; 
 
    } 
 
    
 
    _createList(item, margin){ 
 
     return item.map((el,i)=>{ 
 
      return <div key={i} style={{marginLeft: margin}}> 
 
        {el.text} 
 
        <input defaultChecked type='checkbox'/> 
 
        {el.nodes && el.nodes.length ? this._createList(el.nodes, 10) : null} 
 
       </div> 
 
     }) 
 
    } 
 
    
 
    render() { 
 
    return (
 
     <div > 
 
     \t {this._createList(json, 0)} 
 
     </div> 
 
    ) 
 
    } 
 
} 
 

 
ReactDOM.render(<App />, document.getElementById('container'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 
 

 
<div id="container" ></div>

+0

Спасибо за ваш быстрый ответ Майанк. Как добавить значок разворота и свернуть в виде дерева слева. Onclick на родительском значке мне нужно развернуть treeview. – rafik

+0

Да его рабочий штраф майкан. Как добавить значок разворота и свернуть в виде дерева слева. Onclick на родительском значке мне нужно развернуть treeview. и я использую checked: true в json, но в опции проверки по умолчанию UI его не работает – rafik

+0

вы можете поделиться образцом кода или ссылкой на скрипку (развернуть и закрыть значок с древовидным представлением), то есть мое основное требование – rafik