亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Vuejs 和 Bootstrap Vue 中編輯行

在 Vuejs 和 Bootstrap Vue 中編輯行

莫回無 2023-06-29 21:04:49
我正在嘗試實現一個表格,允許用戶通過使用“編輯”按鈕切換來編輯行。我能夠實現切換功能,但遇到一個問題,它切換所有行而不是單行。我相信我必須按索引選擇行,并且通過我的實現,我可以這樣做,但似乎我無法找到它的任何用處。Vue.component('employee-data', {    template:        /*html*/        `        <b-container>            <h3>Employee Data</h3>                        <b-pagination v-model="currentPage" :total-rows="rows" :per-page="perPage"                             aria-controls="employee-table"></b-pagination>            <b-table striped hover :items="employees" id="employee-table"                    :per-page="perPage" :current-page="currentPage" :fields="fields">                <template v-slot:cell(employeeName)="row" v-if="edit">                    <b-form-input v-model="row.item.employeeName"/>                </template>                <template v-slot:cell(joinDate)="row" v-if="edit">                    <b-form-input v-model="row.item.joinDate"/>                </template>                <template v-slot:cell(selectedDepartment)="row" v-if="edit">                    <b-form-input v-model="row.item.selectedDepartment"/>                </template>                                <template v-slot:cell(jobDescription)="row" v-if="edit">                    <b-form-input v-model="row.item.jobDescription"/>                </template>                <template v-slot:cell(actions)="row">                    <b-button @click="toggleEdit(row.index)">                        {{ edit ? 'Save' : 'Edit' }}                    </b-button>                </template>            </b-table>        </b-container>    
查看完整描述

1 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

如果您有唯一標識符(如 id)并且只想允許一次編輯一行,則可以將edit變量設置為id當前正在編輯的行的 。

您還可以使用通用槽v-slot:cell()來減少編寫的模板數量。

例子

new Vue({

  el: "#app",

  data() {

    return {

      edit: null,

      employees: [{

          id: 0,

          employeeName: "Jane",

          joinDate: "11-11-1111",

          selectedDepartment: "IT",

          jobDescription: "Nerd"

        },

        {

          id: 1,

          employeeName: "Peter",

          joinDate: "12-12-1212",

          selectedDepartment: "Accounting",

          jobDescription: "Moneier"

        }

      ],

      fields: [{

          key: 'employeeName',

          label: 'Employee Name',

          sortable: true

        },

        {

          key: 'joinDate',

          label: 'Join Date',

          sortable: true

        },

        {

          key: 'selectedDepartment',

          label: 'Selected Department',

          sortable: true

        },

        {

          key: 'jobDescription',

          label: 'Job Description',

          sortable: true

        },

        {

          key: 'actions',

          label: 'Actions'

        }

      ]

    }

  },

  computed: {

    rows() {

      return this.employees.length

    }

  },

  methods: {

    onEdit(id) {

      this.edit = this.edit !== id ? id : null;

    }

  }

})

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-vue/2.18.1/bootstrap-vue.min.css" />


<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-vue/2.18.1/bootstrap-vue.min.js"></script>


<div id="app">

  <b-table striped hover :items="employees" :fields="fields">

    <template v-slot:cell()="{ value, item, field: { key }}">

      <template v-if="edit != item.id">{{ value }}</template>

    <b-form-input v-else v-model="item[key]" />

    </template>


    <template v-slot:cell(actions)="{ item: { id }}">

        <b-dropdown variant="primary" text="Actions">

          <b-dropdown-item @click="onEdit(id)">{{ edit === id ? 'Save' : 'Edit' }}</b-dropdown-item>

          <b-dropdown-divider></b-dropdown-divider>

          <b-dropdown-item @click="onDelete(id)">Delete</b-dropdown-item>

        </b-dropdown>

      </template>

  </b-table>

</div>

如果您沒有每行的唯一標識符,或者希望能夠一次編輯多行,您可以向您的項目添加一個標志(在示例中),這將切換isEditing當前是否正在編輯該行或不。

實施例2

new Vue({

  el: "#app",

  data() {

    return {

      employees: [{

          id: 0,

          employeeName: "Jane",

          joinDate: "11-11-1111",

          selectedDepartment: "IT",

          jobDescription: "Nerd"

        },

        {

          id: 1,

          employeeName: "Peter",

          joinDate: "12-12-1212",

          selectedDepartment: "Accounting",

          jobDescription: "Moneier"

        }

      ],

      fields: [{

          key: 'employeeName',

          label: 'Employee Name',

          sortable: true

        },

        {

          key: 'joinDate',

          label: 'Join Date',

          sortable: true

        },

        {

          key: 'selectedDepartment',

          label: 'Selected Department',

          sortable: true

        },

        {

          key: 'jobDescription',

          label: 'Job Description',

          sortable: true

        },

        {

          key: 'actions',

          label: 'Actions'

        }

      ]

    }

  },

  computed: {

    rows() {

      return this.employees.length

    }

  },

  methods: {

    onEdit(item) {

      if (item.isEditing)

        item.isEditing = false;

      else

        this.$set(item, 'isEditing', true)

    }

  }

})

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-vue/2.18.1/bootstrap-vue.min.css" />


<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-vue/2.18.1/bootstrap-vue.min.js"></script>


<div id="app">

  <b-table striped hover :items="employees" :fields="fields">

    <template v-slot:cell()="{ value, item, field: { key }}">

      <template v-if="!item.isEditing">{{ value }}</template>

    <b-form-input v-else v-model="item[key]" />

    </template>


    <template v-slot:cell(actions)="{ item }">

        <b-dropdown variant="primary" text="Actions">

          <b-dropdown-item @click="onEdit(item)">{{ item.isEditing ? 'Save' : 'Edit' }}</b-dropdown-item>

          <b-dropdown-divider></b-dropdown-divider>

          <b-dropdown-item>Delete</b-dropdown-item>

        </b-dropdown>

      </template>

  </b-table>

</div>


查看完整回答
反對 回復 2023-06-29
  • 1 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號