账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Vue+elementUI分页后使用splice()删除某一行时出错
    31
    0

    项目有个表格页面使用了elementUI来做,且开启了分页.

    数据渲染完成后,测试删除功能发现个问题:

    点击"删除"后,除了第一页正常外,第二页开始,无论点击的是哪一行的数据,删除的都是第一行(0)的数据...,

    我试过将数据直接写在vue里渲染而不是从后端接口拿,结果也是一样的

    也试过将要删除的行的scope.$index数据的id打印出来看过,和点击的是一致的,但删除的依然是第一行的.是不是我的分页没配置好,还是我的删除方式有问题

    相关代码

    删除方法

    delete (rowData) { // 删除文章
          console.log(rowData)
          this.tableData.splice(rowData, 1)
        },

    分页代码

    <el-table border :data="tableData.slice((currentPage-1)*pagesize, currentPage*pagesize)" :row-class-name="tableRowClassName">
              <el-table-column label="ID" prop="id" width="100"></el-table-column>
              <el-table-column label="标题" prop="title" width="250"></el-table-column>
              <el-table-column label="副标题" prop="stitle" width="250"></el-table-column>
              <el-table-column label="最后修改时间" prop="date" width="180"></el-table-column>
              <el-table-column label="类别" prop="type" width="100"></el-table-column>
              <el-table-column label="标签" prop="tag" width="100"></el-table-column>
              <el-table-column label="发布人" prop="pubilsher" width="100"></el-table-column>
              <el-table-column label="状态" prop="status" width="100"></el-table-column>
              <el-table-column label="操作" width="450">
                <template slot-scope='scope'>
                  <el-button size="mini" type="info" @click.native.prevent="delete(scope.$index)">删除</el-button>
                  <el-button size="mini" type="primary" @click.native.prevent="edit(scope.row)">修改</el-button>
                </template>
              </el-table-column>
            </el-table>
    <div id="pages">
          <el-pagination background layout="prev, pager, next" :total="tableData.length" @current-change='currentChange'></el-pagination>
        </div>
    data () {
        return {
          pagesize: 10,
          currentPage: 1,
          tableData: [ //表格的数据,从后端拿
          ]
        }
      },
      created: function () {
        this.total = this.tableData.length
      },
    currentChange (currentPage) {
          this.currentPage = currentPage
        }

    各位路过的老哥们帮小弟看看,跪谢!!

    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 掏枪毙了你 普通会员 1楼

      在 Vue 中结合 ElementUI 使用分页并尝试通过 splice() 方法删除某一行数据时,可能会出现错误的情况。这是因为 Vue 的响应式系统并不能直接监听到数组的变动方法(如 splice)对数据的更改。

      假设你的数据结构是这样的: javascript data() { return { tableData: [], // 这里是表格的数据 pagination: {}, // 分页配置项 // ...其他属性 } } 当你尝试通过 splice 删除某一行时,例如: javascript this.tableData.splice(index, 1); 虽然这条数据被从 tableData 数组中移除了,但 ElementUI 的表格并未能正确响应这个变化,导致分页仍然显示已删除的数据。

      要解决这个问题,你需要使用 Vue 提供的数组变异方法,同时更新表格和分页:

      ```javascript this.$set(this.tableData, index, undefined); // 或者 this.tableData.splice(index, 1);

      // 刷新表格的同时更新分页信息 this.$nextTick(() => { const currentLength = this.tableData.length; this.pagination.total = currentLength; // 更新总条数 // 如果需要,可以重新计算当前页码以确保其有效性 }); ``` 这样,ElementUI 的表格就能正确响应数据的变化并更新视图了。

      另外,如果你使用的是后端分页,那么在删除数据后应该重新获取后台接口的数据以保证数据的准确性和一致性。

    更多回答
    网站公告
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部