1
ネストされた順序なしリスト(<ul>
)を使用してツリービューをレンダリングするには、次のデータプロパティを使用するにはどうすればよいですか。ツリービューの再帰関数を実行するにはどうすればよいですか?
// parentId value is always 0 for root nodes, otherwise this value corresponds to the id of its parent
// sequence represents the order of the element in the branch
// level represents the tree level of the element, root nodes will have a level of 1
var data = [
{ id: 'K66', level: 1, name: 'B', parentId: '0', sequence: 2 },
{ id: 'K65', level: 1, name: 'A', parentId: '0', sequence: 1 },
{ id: 'KK2', level: 2, name: 'Alan', parentId: 'K65', sequence: 1 },
{ id: 'KK22', level: 2, name: 'Bir', parentId: 'K66', sequence: 1 },
{ id: 'KK92', level: 2, name: 'Abe', parentId: 'K65', sequence: 2 },
{ id: 'KK77', level: 3, name: 'Boromir', parentId: 'KK22', sequence: 1 }
];
結果は次のようになります。
A
Alan
Abe
B
Bir
Boromir
私はforループを試してみましたが、すぐに私は子ノードを取得するためのコードを繰り返していたと私はにそれをリファクタリングすることができませんでした再帰関数。
ここでデータとCodePenだ:...私はOPの質問の一部以下にお答えしますhttp://codepen.io/nunoarruda/pen/KgVPmv
あなたはまだSOの「Javascriptが木をレンダリング」で検索しましたでしょうか?既存のソリューションがいくつかあります。あなたはコーディングの試み(MCVE)を投稿していないため、回答率に影響する場合があります。 – Prune
@nunoarruda ...再帰的なアプローチは必要ありません。下の私の答えを見てください。 –