question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrapping b-table in a child component

See original GitHub issue

I’m trying to reduce boilerplate code by wrapping a b-table in a component (including filter, pagination, etc.).

Utilising this wrapper component, I want to still be able to make use of named slots to do some formatting on the fields of the table.

However, the naive approach of ‘elevating’ the slots to the parent component doesn’t seem to work.

See this jsfiddle for an example on what 'm trying to accomplish.

VueJS documentation on use of named/scoped slots doesn’t seem to cover this use-case.

How should I approach this best using bootstrap-vue?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

16reactions
emersoncpazcommented, Apr 17, 2018

It works for me:

In child component:

<template>
  <b-table striped hover bordered small responsive="sm" :id="idTable" 
      :items="items"
      :fields="fields"
      <template v-for="field in fields" :slot="field.key" slot-scope="data">
        <slot :name="field.key" v-bind="data">{{ data.item[field.key] }}</slot>
      </template>
    </b-table>
</template>

export default {
    props: {
      idTable: {
        type: String,
        default: 'appTable'
      },
      items: {
        type: Array
      },
      fields: {
        type: Array
      }
    }
}

In parent component

<template>
    <app-table idTable="tblPlaces"
      :items="dataItems" /* from an api request */
      :fields="fields"
      <template slot="actions" slot-scope="data">
        <i class="fa fa-info-circle fa-lg icon-button" title="Detail" @click.stop="showId(data.item.placeId)"></i>
        <i class="fa fa-edit fa-lg icon-button" title="Update" @click.stop="showId(data.item.placeId)"></i>
      </template>
    </app-table>
</template>

<script>
import TableComponent from './TableComponent'

export default {
  data: function () {
    return {
      dataItems: [],
      fields: [
       {
          key: 'placeId',
          label: 'Id'
        },
        {
          key: 'placeName',
          label: 'Place Name',
          sortable: true
        },
        {
          key: 'placeTag',
          label: 'Place Tag',
          sortable: true
        },
        {
          key: 'telephoneNumber',
          label: 'Telephone'
        },
        {
          key: 'actions',
          label: 'Actions'
        }
      ]
    }
  },
  methods: {
    showId(id) {
      alert(id)
    }
  }
  components: {
    appTable: TableComponent
  }
}
</script>
2reactions
chrisb-c01commented, Oct 18, 2019

For future reference: I was revisiting this issue after the change to v-slot syntax and found a thread discussing this as well on SO. Perhaps it is of use to anyone.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReactJS wrapping child component in div breaks table
ReactJS requires you to wrap every element being rendered in a parent div or element. This breaks tables and my responsive table I'm...
Read more >
How to make container shrink-to-fit child elements as they wrap?
Direct child elements of the flexible container automatically ... The flex-wrap property states whether the flex items should wrap or not.
Read more >
What is the best practice for wrapping <tr> and <td> in ... - Reddit
Not as the first wrapper in a component. I could use them in the table without ceasing child components, but using v-for in...
Read more >
PatternFly 4 • Table
Wrap the content of each child row cell in ExpandableRowContent . Enclose each parent/child row pair in a Tbody component with an isExpanded...
Read more >
Forwarding Refs - React
Ref forwarding is a technique for automatically passing a ref through a component to one of its children. This is typically not necessary...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found