how to display to buttons delete and edit in field action in vuejs and bootstrap

Viewed 56

I am working a crud app with vue.js add bootstrap I want to create a dynamic data table in vue.js and bootstrap.

I have create for html:

<template>
  <div>
    <b-table striped hover :items="items" :fields="fields"></b-table>    
  </div>
</template>

I completed there files and items from json also for javascript.

script>

  export default {
    name:'DataTable',
    data() {
      return {
        // Note `isActive` is left out and will not appear in the rendered table
        fields: [ "id", "name",  "email", "phone", "action"],
        items: [
          { id: 1, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 2, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 3, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 4, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 5, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 6, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 7, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 8, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 9, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 10, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "}
      
          // { isActive: false, age: 21, first_name: 'Larsen', last_name: 'Shaw' },
          // { isActive: false, age: 89, first_name: 'Geneva', last_name: 'Wilson' },
          // { isActive: true, age: 38, first_name: 'Jami', last_name: 'Carney' }
        ]
      }
    }
  }
</script>

My code look like picture below until now: enter image description here

but how I can make that in field Action to display to button edit and delete like below:enter image description here

Thanks.

1 Answers

You can achieve this by using of slot attribute in your <b-table> template.

Live Demo :

window.onload = () => {
  new Vue({
    el: '#app',
    data() {
      return {
        fields: [ "id", "name",  "email", "phone", "action"],
        items: [
          { id: 1, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 2, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 3, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 4, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 5, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 6, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 7, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 8, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 9, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "},
          { id: 10, name:"Leannes", email: "Sincere@april.biz", phone:"1-770-736-8031 x56442 "}
        ]
      }
    },
    methods: {
      doEdit(item) {
        this.$set(item, 'temp', JSON.parse(JSON.stringify(item)))
        this.$set(item, 'editing', true)
      },
      doSave(item) {
        this.$set(item, 'editing', false)
        for(let key in item.temp){
          if(item[key] != item.temp[key]){
            item[key] = item.temp[key]
          }
        }
      },
      doCancel(item) {
        this.$set(item, 'editing', false)
        this.$delete(item, 'temp')
      }
    }
  })
}
<script src="https://unpkg.com/vue@2.6.10/dist/vue.min.js"></script>
<script src="https://unpkg.com/bootstrap-vue@2.0.0-rc.27/dist/bootstrap-vue.js"></script>
<link rel="stylesheet" href="https://unpkg.com/bootstrap-vue@2.0.0-rc.27/dist/bootstrap-vue.css"/>
<link rel="stylesheet" href="https://unpkg.com/bootstrap@4.3.1/dist/css/bootstrap.min.css"/>
<div id="app">
  <b-row class="p-4">
    <b-col>
      <b-table striped hover :items="items" :fields="fields">
        <template slot="action" slot-scope="{ item }">
          <b-button-group>
            <b-btn size="sm" @click="doEdit(item)" variant="primary">
              Edit    
            </b-btn>
            <b-btn size="sm" @click="doDelete(item)" variant="secondary">
              Delete
            </b-btn>
          </b-button-group>
        </template>
      </b-table>
    </b-col>
  </b-row>
</div>

Related