1

I have some data and I am trying to figure out how I can display it in table format in rows and columns in Vue Js. The display should be as shown:

enter image description here

this is my code but only displays in one column only but need to split the data into rows and columns

<table>
<tbody> 
  <tr v-for="product in products">
        <td>{{product.item_name}}</td>
   </tr>
  </tbody>
</table>
3
  • Please share some of your code. Commented Dec 29, 2019 at 11:19
  • This is my code but only displays as one column <table> <tbody> <tr v-for="product in products"> <td>{{product.item_name}}</td> </tr> </tbody> </table> Commented Dec 29, 2019 at 11:34
  • OK. So split your array into subarrays. Then loop through the sub array. Each sub-array will be a row. Each element will be a cell. Commented Dec 29, 2019 at 11:45

1 Answer 1

1

Using this function you can sort the initial array to be in three sections: (Make it a vue method).

function sortList(list, rows) {
    const sortedList = [];

    let rowIndex = 0;

    for (i = 0; i < rows; i++) {
        sortedList[i] = [];
    }

    list.forEach(item => {
        sortedList[rowIndex++].push(item);

        if ((rowIndex + 1) === rows) {
            rowIndex = 0;
        }
    });

    return sortedList;
}

Then you can set:

this.products = this.sortList(list, 3);

list being the initial array and 3 being the number of rows you want.

Then in the template you can do:

<table>
    <tbody> 
        <tr v-for="row in products">
            <td v-for="item in row">{{ item.item_name }}</td>
        </tr>
    </tbody>
</table>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.