Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7857729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:07:47+00:00 2026-06-02T21:07:47+00:00

I have a collection of models that I wish to render in a table

  • 0

I have a collection of models that I wish to render in a table view. Each model should be represented by a single row in the table, and this row should be generated using a template. I should be able to attach event handlers to that row (say click), that upon event alert some specific information regarding the model associated with that row.

A common way I’ve seen similar things to this done is to break each row out into it’s own view, and have a parent view (lets say the table in this case) use the row view to generate the html to include in your code. However I can’t figure out how this works with templates.

In this case, I can not attach events specifically to the RowView as it has no reference to a dom element (this.el for backbone), it simply returns a string. How can I achieve what I want, whilst using a template to maximum capacity?

The question isn’t specifically about events, templating or using nested views, but more about the right way to use Backbone to achieve this kind of output.

Sample code(also in a fiddle):

/** View representing a table */
var TableView = Backbone.View.extend({
    tagName: 'table',
    render: function() {
        var rows = _.map(this.collection.models, function(p) {
            return new RowView({model: p}).render();                        
        });
        $('body').html(this.$el.html(rows.join('')));
    }
});

/** View representing a row of that table */
var RowView = Backbone.View.extend({
    render: function() {
        // imagine this is going through a template, but for now
        // lets just return straight html.
        return '<tr>' + 
                '<td>' + this.model.get('name') + '</td>' + 
                '<td>' + this.model.get('age') + '</td>' +
               '</tr>';
    }
});

var data = [
    {'name': 'Oli', 'age': 25},
    {'name': 'Sarah', 'age': 20}];

/** Collection of models to draw */
var peopleCollection = new Backbone.Collection(data);
var tableView = new TableView({collection: peopleCollection});
tableView.render();

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:07:49+00:00Added an answer on June 2, 2026 at 9:07 pm

    One way to handle a hierarchy of views is to have each view render its children and append them to its el . The events are then handled by each view, according to its model/collection.

    To inject your HTML as the view el and thus control the container element, you can use the setElement method

    setElement view.setElement(element)

    If you’d like to apply a Backbone
    view to a different DOM element, use setElement, which will also
    create the cached $el reference and move the view’s delegated events
    from the old element to the new one.

    Your example could be rewritten as

    var rowTemplate=_.template("<tr>"+
         "<td class='name'><%= name %></td>"+
         "<td class='age'><%= age %></td>"+
         "</tr>");
    
    /** View representing a table */
    var TableView = Backbone.View.extend({
        tagName: 'table',
    
        initialize : function() {
            _.bindAll(this,'render','renderOne');
        },
        render: function() {
            this.collection.each(this.renderOne);
            return this;
        },
        renderOne : function(model) {
            var row=new RowView({model:model});
            this.$el.append(row.render().$el);
            return this;
        }
    });
    
    /** View representing a row of that table */
    var RowView = Backbone.View.extend({  
        events: {
            "click .age": function() {console.log(this.model.get("name"));}
        },
    
        render: function() {
            var html=rowTemplate(this.model.toJSON());
            this.setElement( $(html) );
            return this;
        }
    });
    
    var data = [
        {'name': 'Oli', 'age': 25},
        {'name': 'Sarah', 'age': 20}];
    
    /** Collection of models to draw */
    var peopleCollection = new Backbone.Collection(data);
    var tableView = new TableView({collection: peopleCollection});
    $("body").append( tableView.render().$el );
    

    And a Fiddle http://jsfiddle.net/9avm6/5/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I render a collection of backbone models in a list view, where each model
I have a Backbone View that has a Collection as its Model. If the
I have a collection that fetches models from server. This works, now I want
I have a code in Javascript as : _.each(this.collection.models, function (student) { $(this.el).append(new StudCloneItemView({
I have a collection of models, each of which is attached a view. The
I have a collection of user models that starts with basic data: [ {'_id':
Lets say we have the following models: (simplified) class Collection(models.Model): slug = models.SlugField() class
I have this json which populates my collection ( TableListCollection ) of models (
I use backbone.js and have a model without a collection. In the view I
I have a Model called Version that looks like this: from google.appengine.ext import db

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.