1

Below shows my data. I am trying to loop Item inside table <td> tag so that it forms two items in one column.

user
    >"name":"Bob"
    >"id":1
    >"Item":
        >"name":"Desk"
        >"name":"Chair"

Code

<table class="table table-hover">
    <tbody>
        <tr>
            <th>Name</th>
            <th>Items</th>
        </tr>

        <tr v-for="use in user.data" :key="use.id">
            <td>{{ use.name }}</td>
            <td v-for="a in use.item">
                {{ a.name }},
            </td>
        </tr>
    </tbody>
</table>

But when I do the codes above, it produce another column just like below picture. How do I put the Desk and Chair sits together in one column?

enter image description here

2
  • which css framework you are using? Commented Feb 13, 2019 at 6:13
  • I'm using adminlte Commented Feb 13, 2019 at 6:15

3 Answers 3

3

Hi just use a span tag in side td here is my solution, p or div tag are block tags, if you are using bootstrap 4 just set class to span inline-block or inline

<table class="table table-hover">
    <tbody>
        <tr>
            <th>Name</th>
            <th>Items</th>
        </tr>

        <tr v-for="use in user.data" :key="use.id">
            <td>{{ use.name }}</td>
            <td>
                <span v-for="a in use.item"> {{ a.name }}, </span>
            </td>
        </tr>
    </tbody>
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

how do i remove the last , (since it display Desk, Chair,)? is there a function in vue to remove the last character?
can i see your controller code how you are getting items there will be better solution from php side no need to loop on front-end.
0

v-for crates another elemenrs for you in each iteration. Use join function to display together

    <tr v-for="use in user.data" :key="use.id">
        <td>{{ use.name }}</td>
        <td>
            {{ use.item.join(", ") }},
        </td>
    </tr>

Output would be Desk, Chair

2 Comments

under item column, i got [object Object], [object Object]
make use.item as array
0

We can do this with using lodash `

  <tr v-for="use in user.data" :key="use.id">
      <td>{{ use.name }}</td>
      <td>
         {{ _.map(use.item, 'name').join(", ") }}
      </td>
  </tr>

`

2 Comments

I have an error Property or method "_" is not defined on the instance but referenced during render
lodash iam used this for pluck name from object need to include your file

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.