Creating dynamic columns with textbox in each column row

45 views
Skip to first unread message

kaka

unread,
Mar 22, 2018, 10:59:11 AM3/22/18
to KnockoutJS
I have a requirement as.

I have a table with 2 static columns where each has a textbox.

I also have a add row button outside the table and one remove row button along each row.

When the user clicks add row it adds a row to the table and I can see two columns with textbox in each. User can add more rows if required by clicking add row and click remove row to remove rows.

This all has been setup and works fine as :


My issue is there is also an Get Columns button. When the user clicks this button I fetch a list of columns and then add these columns b/w the already existing columns of the table.

Each of these columns also need to have a textbox.

I am not sure how to add columns dynamically. Would appreciate inputs.

So with final setup I want to loop through each row and save it to db.

Sean Mill

unread,
Mar 28, 2018, 2:57:57 AM3/28/18
to KnockoutJS
one option is to change your Data model to hold an observable array of properties

 var Data = function(items) {
    var self = this;
    self.data = ko.observableArray([]);
    for(var i = 0, i < items.length; i++)
    {
    self.data.push(items[0]);
    }
  };

in your html table body you need two foreach bindings - first for the rows and then for the columns

when you add a column you need to
1. push an extra item onto every row.data array

the only other thing is you need a foreach to lay out the rows in your table > thead
Reply all
Reply to author
Forward
0 new messages