Views and appending table rows in backbone.js

nope

I am trying to create a code using backbone.The user inputs text into two fields Item and price. This info then is appended to a table and creates a new row for every input pair entered.

I've created a template for the table. My question when creating my view since the data from the text fields of item and price need to be sorted into the corresponding cells, how do I append the new rows correctly. Can I just make a view for the row? Or do I need to do it for each cell?

 <div id="container">
    <script id="grocery-list-template" type="text/template">
        <table>
        <thead>
            <tr>
            <th>Item</th>
            <th>Price</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><%=something to get item%></td>
                <td><%=something to get the price%></td>
                <td></td>
                <td><button class="complete"></button><button class="remove"></td>
            </tr>
            </tbody>
        </table>
    </script>
</div>

This is essentially what Im trying to do, but I dont know how to specify the row AND the cell? Do I have to make a view for each cell using tagname: td or can I just keep tagname: tr and route the data into the apropriate cells? I found an example that just appended a list using li elements so I was trying to adapt some of it to my code

 var TableView = Backbone.View.extend({
            tagname: "tr",
            classname: "itemAndprice",
            template:_.template($("grocery-list-template").html()),
            render: function() {
                this.$el.html(this.template(this.model.toJSON()));
                this.$el.attr("id", this.model.id);
                if (this.model.get('done')) this.$el.addClass('done');
                $("#GroceryList").append(this.$el);
                return this;
            },
biril

You can create a view for the whole row. For each row you would need a row-model with item and price attributes which you would pass into the row-view's constructor. This pretty much looks like the TableView in your code (except I would expect that to be called RowView instead, the TableView being a different, 'parent' view)

Given that your model contains item and price attributes, the row-view-template should contain

....
<td><%=item%></td>
<td><%=price%></td>
....

Then this.model.toJSON() would give you a hash with item and price attributes which would be passed into this.template, populating <%=item%> and <%=price%> with the relevant values.

You can also take a look at underscore's template function for further information.

Note however that the row-view and the table-view should be two different views. From your code it seems that you have a template appropriate for a table-view (which also contains a single row that shouldn't be there) and a view appropriate as a row-view.

Having two views you would pass a collection of row-models in the table-view which would iterate over the collection and add a row(-view) for each model. When the user creates a new item you'd add a model into the collection and re-render the table.

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Backbone JS, navigating different views

From Dev

Backbone.js appending [] to key name on fetch

From Dev

Appending rows into table using JQuery

From Dev

Backbone.js views and models correlation?

From Dev

how to switch views with the backbone.js router?

From Dev

Backbone.js is not rendering views properly

From Dev

Backbone JS - Combination of collection, views and models

From Dev

Backbone.js is not rendering views properly

From Dev

Backbone.js: Populating my collection and then appending it to the page

From Dev

Bugs appending rows to table and scrolling to bottom of page

From Dev

Appending Rows to Nested HTML Table using Jquery

From Dev

Backbone Marionette composite view is not showing rows in the table

From Dev

Appending table, tr, td tags to innerHTML does not create table rows

From Dev

What exactly is this.el in Backbone.js views?

From Dev

Backbone.js parent vs. child views

From Dev

What is the order of events binded by Views in Backbone.js?

From Dev

Re-rendering backbone.js views on resize?

From Dev

Is it an anti-pattern to instantiate models in views in Backbone.js?

From Dev

How to load multiple Backbone.js views on a single page

From Dev

Passing Data to Views in Backbone Js does not see in the browser

From Dev

backbone js view event binding only to views elements

From Dev

Adding views to different el based on condition in Backbone.js

From Dev

Best way to start a backbone.js app with multiple views and routes

From Dev

What exactly is this.el in Backbone.js views?

From Dev

Using backbone-validation.js on a model without views

From Dev

Backbone.js views will not display on page load, but works with jQuery in console

From Dev

Backbone.js multiple views, one collection, one fetch

From Dev

How to redirect to other views after file download using backbone js?

From Dev

Backbone.js: Table not presenting correcting with templates

Related Related

  1. 1

    Backbone JS, navigating different views

  2. 2

    Backbone.js appending [] to key name on fetch

  3. 3

    Appending rows into table using JQuery

  4. 4

    Backbone.js views and models correlation?

  5. 5

    how to switch views with the backbone.js router?

  6. 6

    Backbone.js is not rendering views properly

  7. 7

    Backbone JS - Combination of collection, views and models

  8. 8

    Backbone.js is not rendering views properly

  9. 9

    Backbone.js: Populating my collection and then appending it to the page

  10. 10

    Bugs appending rows to table and scrolling to bottom of page

  11. 11

    Appending Rows to Nested HTML Table using Jquery

  12. 12

    Backbone Marionette composite view is not showing rows in the table

  13. 13

    Appending table, tr, td tags to innerHTML does not create table rows

  14. 14

    What exactly is this.el in Backbone.js views?

  15. 15

    Backbone.js parent vs. child views

  16. 16

    What is the order of events binded by Views in Backbone.js?

  17. 17

    Re-rendering backbone.js views on resize?

  18. 18

    Is it an anti-pattern to instantiate models in views in Backbone.js?

  19. 19

    How to load multiple Backbone.js views on a single page

  20. 20

    Passing Data to Views in Backbone Js does not see in the browser

  21. 21

    backbone js view event binding only to views elements

  22. 22

    Adding views to different el based on condition in Backbone.js

  23. 23

    Best way to start a backbone.js app with multiple views and routes

  24. 24

    What exactly is this.el in Backbone.js views?

  25. 25

    Using backbone-validation.js on a model without views

  26. 26

    Backbone.js views will not display on page load, but works with jQuery in console

  27. 27

    Backbone.js multiple views, one collection, one fetch

  28. 28

    How to redirect to other views after file download using backbone js?

  29. 29

    Backbone.js: Table not presenting correcting with templates

HotTag

Archive