How to perform a foreach loop of a multi-row and multi-column list in Knockout.js


Echt Ottoban

enter image description here

I have understood every function in knockout js, but I am trying to execute one where there are 3 lines, so after filling the row for 3 users, knockout should go to the next line and fill, how can I achieve at this point??

you

You need to group it based on user index. The end goal is to create an array of arrays where the inner array contains the data for each column. You can create a computed property like this:

var viewModel = function() {
  var self = this;

  self.users = [
   { name: "name 1"}, 
   { name: "name 2"}, 
   { name: "name 3"}, 
   { name: "name 4"}, 
   { name: "name 5"}, 
   { name: "name 6"}, 
   { name: "name 7"}, 
 ];

  // this array looks: [[user1,user2,user3], [user4,user5,user6]]
  self.groupedUsers = ko.computed(function() {
    var rows = [];

    self.users.forEach(function(user, i) {
      // whenever i = 0, 3, 6 etc, we need to initialize the inner array
      if (i % 3 == 0)
        rows[i/3] = [];

      rows[Math.floor(i/3)][i % 3] = user;
    });

    return rows;
  });
};

ko.applyBindings(new viewModel());

and in your html:

<table>
  <tbody data-bind="foreach: groupedUsers">
    <tr data-bind="foreach: $data">
      <td data-bind="text: name">
      </td>
    </tr>
  </tbody>
</table>

$dataThis is the internal array in the context. You can read about binding contexts here.

Here is the fiddle for the test


CSS options:

I'm no expert, but the key here is whether you have one for containereach of you row. This can be achieved with pure if there is no delimiter between each line css. You can float: leftuse on user container, which will count users per row based on screen size and container width.

The same argument can be made if you use a similar framework bootstrapor one foundationwith grid class help . You'll have it all columnin one rowclass and the grid will automatically adjust based on screen size and customization col-mdand col-smclasses set based on device level requirements .

Related


How to perform multi-row calculations in SQL

Sheshadri I have 4 result sets which I moved into a temp table for reporting purposes: Date Issued_Id Item_Name Qty_Issued Qty_Return Qty_Damage Type Balance OPBal -------------------------------------------------------------------------------

How to perform multi-row calculations in SQL

Sheshadri I have 4 result sets which I moved into a temp table for reporting purposes: Date Issued_Id Item_Name Qty_Issued Qty_Return Qty_Damage Type Balance OPBal -------------------------------------------------------------------------------

How to perform multi-column matching in SQLite?

Pascal Given two tables T1with T2overlapping column definitions ( Band C), how can I select the row T1where Band Cnot found in the same T2row? Here is the setup: create table T1('A' TEXT, 'B' TEXT, 'C' TEXT); insert into T1 values ('a1', 'b1', 'c1'), ('a2'

How to perform multi-column matching in SQLite?

Pascal Given two tables T1with T2overlapping column definitions ( Band C), how can I select the row T1where Band Cnot found in the same T2row? Here is the setup: create table T1('A' TEXT, 'B' TEXT, 'C' TEXT); insert into T1 values ('a1', 'b1', 'c1'), ('a2'

How to create multi-column dropdown list using React Js?

Avinash I want to create a multi-column dropdown where I can display the name and its description in one row. But when I select the dropdown, it should show the value of column 1 as the selected value. Please refer to the attached picture. I don't know where t

How to perform complex multi-column matching in R/

username I want to match two dataframes based on a condition on multiple conditions, but can't figure out how. So if I have my dataset: df1 <- data.frame(lower=c(0,5,10,15,20), upper=c(4,9,14,19,24), x=c(12,45,67,89,10)) df2 <- data.frame(age=c(12, 14, 5, 2, 9

How to perform a multi-column semi-join in pandas?

fat dragon Suppose I have the following two dataframes: >>> df1 = pd.DataFrame({'grp':[1,1,2], 'x':[6,4,2], 'y':[7,8,9]}) >>> df1 grp x y 0 1 6 7 1 1 4 8 2 2 2 9 >>> df2 = pd.DataFrame({'grp':[1], 'x':[6], 'z':[3]}) >>> df2 grp x z 0

How to perform complex multi-column matching in R/

username I want to match two dataframes based on a condition on multiple conditions, but can't figure out how. So if I have my dataset: df1 <- data.frame(lower=c(0,5,10,15,20), upper=c(4,9,14,19,24), x=c(12,45,67,89,10)) df2 <- data.frame(age=c(12, 14, 5, 2, 9

How to perform a multi-column semi-join in pandas?

fat dragon Suppose I have the following two dataframes: >>> df1 = pd.DataFrame({'grp':[1,1,2], 'x':[6,4,2], 'y':[7,8,9]}) >>> df1 grp x y 0 1 6 7 1 1 4 8 2 2 2 9 >>> df2 = pd.DataFrame({'grp':[1], 'x':[6], 'z':[3]}) >>> df2 grp x z 0

How to perform a multi-column semi-join in pandas?

fat dragon Suppose I have the following two dataframes: >>> df1 = pd.DataFrame({'grp':[1,1,2], 'x':[6,4,2], 'y':[7,8,9]}) >>> df1 grp x y 0 1 6 7 1 1 4 8 2 2 2 9 >>> df2 = pd.DataFrame({'grp':[1], 'x':[6], 'z':[3]}) >>> df2 grp x z 0

How to perform complex multi-column matching in R/

username I want to match two dataframes based on a condition on multiple conditions, but can't figure out how. So if I have my dataset: df1 <- data.frame(lower=c(0,5,10,15,20), upper=c(4,9,14,19,24), x=c(12,45,67,89,10)) df2 <- data.frame(age=c(12, 14, 5, 2, 9

How to perform a multi-column semi-join in pandas?

fat dragon Suppose I have the following two dataframes: >>> df1 = pd.DataFrame({'grp':[1,1,2], 'x':[6,4,2], 'y':[7,8,9]}) >>> df1 grp x y 0 1 6 7 1 1 4 8 2 2 2 9 >>> df2 = pd.DataFrame({'grp':[1], 'x':[6], 'z':[3]}) >>> df2 grp x z 0

How to perform a multi-column semi-join in pandas?

fat dragon Suppose I have the following two dataframes: >>> df1 = pd.DataFrame({'grp':[1,1,2], 'x':[6,4,2], 'y':[7,8,9]}) >>> df1 grp x y 0 1 6 7 1 1 4 8 2 2 2 9 >>> df2 = pd.DataFrame({'grp':[1], 'x':[6], 'z':[3]}) >>> df2 grp x z 0

How to create a multi-column list in swiftUI?

Rahul I am trying to create a multilist using SwiftUI. Is there a better way? The first column in the list has text that can be multiple lines. thank you for your help George Create one Listand HStackinclude your picture. example: struct ContentView: View {

Multi-column multi-row PIVOT

Avax Consider I have a table with data in the following format: Foo_FK MonthCode_FK Activity_FK SumResultsX SumResultsY ----------------------------------------------------------- 1 201312 0 10 2 1 201312 1

Multi-column multi-row PIVOT

Avarkx Consider I have a table with data in the following format: Foo_FK MonthCode_FK Activity_FK SumResultsX SumResultsY ----------------------------------------------------------- 1 201312 0 10 2 1 201312 1

Multi-column multi-row PIVOT

Avarkx Consider I have a table with data in the following format: Foo_FK MonthCode_FK Activity_FK SumResultsX SumResultsY ----------------------------------------------------------- 1 201312 0 10 2 1 201312 1

"unpacking" a pandas column containing a multi-row list

Alex: Say I have the following pandas dataframe: df = pd.DataFrame({"a" : [1,2,3], "b" : [[1,2],[2,3,4],[5]]}) a b 0 1 [1, 2] 1 2 [2, 3, 4] 2 3 [5] How can I "stack" the lists in column "b" to convert it into a dataframe: a b 0

List row values of multi-column pandas dataframe

Zhu ho I have this data in pandas.DataFrame: Date, Team1, Team2, Team1 Score, Team2 Score, Event 8/2/17, Juventus, Milan, 2, 1, Friendly match 6/2/17, Milan, Napoli, 3, 0, Friendly match 5/1/17, Milan, Sampdoria, 1, 0, Friendly match 25/12/16, Parma, Milan, 0,

"unpacking" a pandas column containing a multi-row list

Alex: Say I have the following pandas dataframe: df = pd.DataFrame({"a" : [1,2,3], "b" : [[1,2],[2,3,4],[5]]}) a b 0 1 [1, 2] 1 2 [2, 3, 4] 2 3 [5] How can I "stack" the lists in column "b" to convert it into a dataframe: a b 0

List row values of multi-column pandas dataframe

Zhu ho I have this data in pandas.DataFrame: Date, Team1, Team2, Team1 Score, Team2 Score, Event 8/2/17, Juventus, Milan, 2, 1, Friendly match 6/2/17, Milan, Napoli, 3, 0, Friendly match 5/1/17, Milan, Sampdoria, 1, 0, Friendly match 25/12/16, Parma, Milan, 0,

List row values of multi-column pandas dataframe

Zhu ho I have this data in pandas.DataFrame: Date, Team1, Team2, Team1 Score, Team2 Score, Event 8/2/17, Juventus, Milan, 2, 1, Friendly match 6/2/17, Milan, Napoli, 3, 0, Friendly match 5/1/17, Milan, Sampdoria, 1, 0, Friendly match 25/12/16, Parma, Milan, 0,

"unpacking" a pandas column containing a multi-row list

Alex: Say I have the following pandas dataframe: df = pd.DataFrame({"a" : [1,2,3], "b" : [[1,2],[2,3,4],[5]]}) a b 0 1 [1, 2] 1 2 [2, 3, 4] 2 3 [5] How can I "stack" the lists in column "b" to convert it into a dataframe: a b 0

List row values of multi-column pandas dataframe

Zhu ho I have this data in pandas.DataFrame: Date, Team1, Team2, Team1 Score, Team2 Score, Event 8/2/17, Juventus, Milan, 2, 1, Friendly match 6/2/17, Milan, Napoli, 3, 0, Friendly match 5/1/17, Milan, Sampdoria, 1, 0, Friendly match 25/12/16, Parma, Milan, 0,