Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try with nested view and I would suggest using template</p> <pre><code>var data = [ {"id":1,"name":"one","parent_id":0}, {"id":2,"name":"two","parent_id":0}, {"id":3,"name":"three","parent_id":0}, {"id":4,"name":"four","parent_id":0}, {"id":5,"name":"five","parent_id":0}, {"id":6,"name":"one","parent_id":2}, {"id":7,"name":"two","parent_id":2}, {"id":8,"name":"three","parent_id":2}, {"id":9,"name":"four","parent_id":2}, {"id":10,"name":"five","parent_id":2}, ] var NestedView = Backbone.View.extend({ render: function(){ this.$el.html( "&lt;ul id='ul-0'&gt;&lt;/ul&gt;" ) this.collection.each( function( model ){ this.renderElement( model ) }, this ) }, renderElement: function( model ){ var ul = this.getParentUl( model ); this.appendElement( ul, model ); }, getParentUl: function( model ) { var ul = this.$el.find( "#ul-" + model.get( "parent_id" ) ); if( ul.length == 0 ) { this.appendListInElement( model ); ul = this.$el.find( "#ul-" + model.get( "parent_id" ) ); } return ul; }, appendListInElement: function( model ){ var li = this.$el.find( "#li-" + model.get( "parent_id" ) ); li.after( "&lt;ul id='ul-" + model.get( "parent_id" ) + "'&gt;&lt;/ul&gt;" ); }, appendElement: function( ul, model ){ ul.append( "&lt;li id='li-" + model.get( "id" ) + "'&gt;" + model.get( "name" ) + "&lt;/li&gt;" ); } }); var elements = new Backbone.Collection( data ); var nestedView = new NestedView({ el: "#wrapper", collection: elements }); nestedView.render(); &lt;div id="wrapper"&gt;&lt;/div&gt; </code></pre> <p>Here is the complete code, with underscore template</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Backbone&lt;/title&gt; &lt;script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://underscorejs.org/underscore.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://backbonejs.org/backbone.js"&gt;&lt;/script&gt; &lt;script type="text/template" id="parent-template"&gt; &lt;ul id='ul-&lt;%= parent_id %&gt;'&gt;&lt;/ul&gt; &lt;/script&gt; &lt;script type="text/template" id="child-template"&gt; &lt;li id="li-&lt;%= id %&gt;"&gt;&lt;%= name %&gt;&lt;/li&gt; &lt;/script&gt; &lt;script type="text/javascript"&gt; var compiledParent = _.template( $('#parent-template').html() ); var compiledChild = _.template( $('#child-template').html() ); var data = [ {"id":1,"name":"one","parent_id":0}, {"id":2,"name":"two","parent_id":0}, {"id":3,"name":"three","parent_id":0}, {"id":4,"name":"four","parent_id":0}, {"id":5,"name":"five","parent_id":0}, {"id":6,"name":"one","parent_id":2}, {"id":7,"name":"two","parent_id":2}, {"id":8,"name":"three","parent_id":2}, {"id":9,"name":"four","parent_id":2}, {"id":10,"name":"five","parent_id":2}, ]; var NestedView = Backbone.View.extend({ render: function(){ this.$el.html( compiledParent({parent_id : '0'}) ); this.collection.each( function( model ){ this.renderElement( model ) }, this ) }, renderElement: function( model ){ var ul = this.getParentUl( model ); this.appendElement( ul, model ); }, getParentUl: function( model ) { var ul = this.$el.find( "#ul-" + model.get( "parent_id" ) ); if( ul.length == 0 ) { this.appendListInElement( model ); ul = this.$el.find( "#ul-" + model.get( "parent_id" ) ); } return ul; }, appendListInElement: function( model ){ var li = this.$el.find( "#li-" + model.get( "parent_id" ) ); li.after( compiledParent({parent_id : model.get( "parent_id" )}) ); }, appendElement: function( ul, model ){ ul.append( compiledChild({id:model.get( "id" ) , name:model.get( "name" )}) ); } }); $(document).ready(function() { var elements = new Backbone.Collection( data ); var nestedView = new NestedView({ el: "#wrapper", collection: elements }); nestedView.render(); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="wrapper"&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload