0

I'm using Vue.js Bootstrap table and I would like to be able to collect each table row id in an Array or Object data property.

Here is an example of a bootstrap table template:

      <template v-slot:cell(label)="row" >
        <div >
          <div class="label"></div>
        </div>
      </template>

So, how can I collect row.item.id values in an Array or in an Object in order to use this data for other purposes?

1
  • As you are binding the data to the bootstrap table. You can simply get the id's from each object by using Array.map() method from the source data object itself. Commented Jul 13, 2023 at 6:40

1 Answer 1

1

You can simply store any property from the items array into a separate array by iterating using Array.map() method.

For example :

data() {
  return {
    items: [
      { id: 1, age: 40, first_name: 'Dickerson', last_name: 'Macdonald' },
      { id: 2, age: 21, first_name: 'Larsen', last_name: 'Shaw' },
      { id: 3, age: 89, first_name: 'Geneva', last_name: 'Wilson' },
      { id: 4, age: 38, first_name: 'Jami', last_name: 'Carney' }
    ],
    itemsID: []
  }
}

Then in mounted hook :

mounted() {
    this.itemsID = this.items.map(({id}) => id)
}
Sign up to request clarification or add additional context in comments.

2 Comments

I would recommend using a computed property, it is usually more robust than a one-time call in a lifecycle hook
@MoritzRingler Yeah that will also work, I just gave a suggestion to achieve that. At the end it depends on PO how they want to utilize this.

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.