在Vuetify数据表中删除行总是移除最后一行。

huangapple go评论78阅读模式
英文:

Deleting row in Vuetify data table always removes the last row

问题

我正在尝试实现从表格中删除单个行的功能。每一行都有一个删除图标,当点击时会打开一个确认对话框。如果用户点击"Yes"按钮,那么应该删除该特定行。

然而,问题是无论我点击哪一行,表格的最后一行总是被删除。

以下是我的代码:

// 表格
<v-data-table
   :headers="headers"
   :items="tableData"
   :search="search"
>
   <template v-slot:item.actions="{ item }">
      <div class="table__icons">
        <v-icon
           small
           class="mr-2"
           @click="goToContract(item)"
        >
          mdi-pencil
        </v-icon>
        <a v-if="$route.name === 'pregled-ugovora' || $route.name === 'pregled-znsa'" :href="ispisRow(item)"
           target="_blank">
           <v-icon
             small
           >
             mdi-download
           </v-icon>
          </a>
          <v-icon
              v-if="$route.name === 'pregled-znsa'"
              small
              @click="openDeleteModal(item)"
          >
            mdi-delete
          </v-icon>
          <v-icon v-if="$route.name === 'pregled-ugovora'"
                  small
                  @click="getIzvjestaj(item)"
          >
            mdi-note-text
          </v-icon>
        </div>
      </template>
</v-data-table>

// 删除对话框
<v-dialog v-model="dialogDelete" max-width="500px">
  <v-card>
    <v-card-title class="text-h2 delete-text">您确定要删除这一行吗?</v-card-title>
    <v-card-actions>
      <v-spacer></v-spacer>
      <div class="m-btn delete-btn" @click="closeDeleteModal">No</div>
      <div class="m-btn delete-btn" @click="deleteZnsConfirm">Yes</div>
      <v-spacer></v-spacer>
    </v-card-actions>
  </v-card>
</v-dialog> 

// 方法
openDeleteModal(item) {
   this.deletedZnsIndex = this.tableData.indexOf(item)
   this.deletedZns = Object.assign({}, item)
   this.dialogDelete = true
},
closeDeleteModal(item) {
   this.deletedZnsIndex = ''
   this.deletedZns = {}
   this.dialogDelete = false
},
deleteZnsConfirm(item) {
   this.tableData.splice(this.tableData.indexOf(this.deletedZns), 1)
   this.dialogDelete = false
},

希望这有助于解决您的问题。如果您有任何其他问题,请随时提出。

英文:

I'm trying to implement deleting of individual rows from table. Each row has a delete icon that opens a confirm dialog when clicked. If the user clicks the "Yes" button, that specific row should be deleted.

However, the issue is that no matter what row I click, the last row of table is always deleted.

Here is my code:

// table
&lt;v-data-table
:headers=&quot;headers&quot;
:items=&quot;tableData&quot;
:search=&quot;search&quot;
&gt;
&lt;template v-slot:item.actions=&quot;{ item }&quot;&gt;
&lt;div class=&quot;table__icons&quot;&gt;
&lt;v-icon
small
class=&quot;mr-2&quot;
@click=&quot;goToContract(item)&quot;
&gt;
mdi-pencil
&lt;/v-icon&gt;
&lt;a v-if=&quot;$route.name === &#39;pregled-ugovora&#39; || $route.name === &#39;pregled-znsa&#39;&quot; :href=&quot;ispisRow(item)&quot;
target=&quot;_blank&quot;&gt;
&lt;v-icon
small
&gt;
mdi-download
&lt;/v-icon&gt;
&lt;/a&gt;
&lt;v-icon
v-if=&quot;$route.name === &#39;pregled-znsa&#39;&quot;
small
@click=&quot;openDeleteModal(item)&quot;
&gt;
mdi-delete
&lt;/v-icon&gt;
&lt;v-icon v-if=&quot;$route.name === &#39;pregled-ugovora&#39;&quot;
small
@click=&quot;getIzvjestaj(item)&quot;
&gt;
mdi-note-text
&lt;/v-icon&gt;
&lt;/div&gt;
&lt;/template&gt;
&lt;/v-data-table&gt;
// delete dialog
&lt;v-dialog v-model=&quot;dialogDelete&quot; max-width=&quot;500px&quot;&gt;
&lt;v-card&gt;
&lt;v-card-title class=&quot;text-h2 delete-text&quot;&gt;Do you want to remove this row?&lt;/v-card-title&gt;
&lt;v-card-actions&gt;
&lt;v-spacer&gt;&lt;/v-spacer&gt;
&lt;div class=&quot;m-btn delete-btn&quot; @click=&quot;closeDeleteModal&quot;&gt;No&lt;/div&gt;
&lt;div class=&quot;m-btn delete-btn&quot; @click=&quot;deleteZnsConfirm&quot;&gt;Yes&lt;/div&gt;
&lt;v-spacer&gt;&lt;/v-spacer&gt;
&lt;/v-card-actions&gt;
&lt;/v-card&gt;
&lt;/v-dialog&gt; 
// methods
openDeleteModal(item) {
this.deletedZnsIndex = this.tableData.indexOf(item)
this.deletedZns = Object.assign({}, item)
this.dialogDelete = true
},
closeDeleteModal(item) {
this.deletedZnsIndex = &#39;&#39;
this.deletedZns = {}
this.dialogDelete = false
},
deleteZnsConfirm(item) {
this.tableData.splice(this.tableData.indexOf(this.deletedZns), 1)
this.dialogDelete = false
},

答案1

得分: 0

我弄清楚了...

不是这样的:

deleteZnsConfirm(item) {
this.tableData.splice(this.deletedZns), 1)
this.dialogDelete = false
},

我的删除函数应该像这样:

deleteZnsConfirm(item) {
this.tableData.splice(this.deletedZnsIndex, 1)
this.dialogDelete = false
},

我在切割 tableData 时使用了错误的索引 在Vuetify数据表中删除行总是移除最后一行。

英文:

Oh I figured it out...

Instead of this:

deleteZnsConfirm(item) {
this.tableData.splice(this.tableData.indexOf(this.deletedZns), 1)
this.dialogDelete = false
},

My delete function should look like this:

deleteZnsConfirm(item) {
this.tableData.splice(this.deletedZnsIndex, 1)
this.dialogDelete = false
},

I was using the wrong index while splicing tableData 在Vuetify数据表中删除行总是移除最后一行。

huangapple
  • 本文由 发表于 2023年5月28日 18:56:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定