亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

el-table在彈窗中的使用示例詳解

 更新時(shí)間:2024年12月03日 09:22:38   作者:道香居  
本文總結(jié)了在Vue2項(xiàng)目中使用element-ui的el-table組件在彈窗中展示數(shù)據(jù),并實(shí)現(xiàn)基礎(chǔ)的勾選功能,內(nèi)容包括如何設(shè)置row-key、使用reserve-selection屬性、修改數(shù)據(jù)獲取函數(shù)以支持跨頁(yè)勾選以及如何在關(guān)閉彈窗時(shí)清理selection,感興趣的朋友跟隨小編一起看看吧

el-table在彈窗中的使用

概要

在實(shí)際的業(yè)務(wù)場(chǎng)景中,需要在彈窗內(nèi)增加表格的展示,勾選.本文以我實(shí)際的開(kāi)發(fā)過(guò)程中,碰到的"坑",來(lái)總結(jié)一下el-table 如何設(shè)置

使用Vue2版本來(lái)開(kāi)發(fā) element-ui

如何展示基本的圖表數(shù)據(jù),并支持基礎(chǔ)的勾選

數(shù)據(jù)的展示包含多個(gè)頁(yè)面
參考官方的代碼

<template>
  <div class="main">
  <!-- 授權(quán)設(shè)備 -->
  <el-dialog title="選擇設(shè)備" :visible.sync="deviceOpen" width="500px">
  <el-table
    ref="multipleTable"
    :data="iotDeviceList"
    tooltip-effect="dark"
    style="width: 100%"
    @selection-change="handleSelectionChange">
    <el-table-column
      type="selection"
      width="55">
    </el-table-column>
    <el-table-column
      label="日期"
      width="120">
      <template slot-scope="scope">{{ scope.row.date }}</template>
    </el-table-column>
    <el-table-column
      prop="name"
      label="姓名"
      width="120">
    </el-table-column>
    <el-table-column
      prop="address"
      label="地址"
      show-overflow-tooltip>
    </el-table-column>
  </el-table>
  <pagination
      v-show="devicesTotal>0"
      :total="devicesTotal"
      :page.sync="deviceQueryParams.pageIndex"
      :limit.sync="deviceQueryParams.pageSize"
      @pagination="getDevicesList"
  />
  <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitDevicesForm">確 定</el-button>
      <el-button @click="cancelDevicesSet">取 消</el-button>
  </div>
  </el-dialog>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        // 設(shè)備設(shè)置界面打開(kāi)
        deviceOpen: false,
        devicesTotal: 0,
        deviceQueryParams: {
          pageIndex: 1,
          pageSize: 10
          deviceType: undefined
        },
        iotDeviceList: [{
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀區(qū)金沙江路 1518 弄'
        }, {
          date: '2016-05-08',
          name: '王小虎',
          address: '上海市普陀區(qū)金沙江路 1518 弄'
        }],
        multipleSelection: []
      }
    },
    methods: {
      // 提交設(shè)備設(shè)置
      submitDevicesForm(){
        var form = {...}
        updateIoDevice(form).then(response => {
          if(response.code === 200){
            // 上方會(huì)顯示成功的消息通知
            this.msgSuccess(response.msg)
            this.deviceOpen = false
            this.deviceLoading = false
            // 此處相當(dāng)于彈窗取消之后,重新刷新頁(yè)面數(shù)據(jù)
            this.getList()
          }else {
            this.msgError(response.msg)
          }
        })
      },
      // 取消設(shè)備設(shè)置
      cancelDevicesSet(){
        this.deviceLoading = false
        this.deviceOpen = false
      },
      // 勾選改變時(shí)觸發(fā)的事件
      handleSelectionChange(selection) {
        // 每次觸發(fā)打印selection信息,總是發(fā)現(xiàn)出現(xiàn)數(shù)組中包含多個(gè)toggle數(shù)據(jù),所以便再此設(shè)置了過(guò)濾,如果有同學(xué)知道為什么,可以評(píng)論區(qū)留言
        this.multipleSelection = selection.filter((item) => Array.isArray(item) === false).map(obj => obj.id);
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      // 分頁(yè)查詢請(qǐng)求
      getDevicesList(){
        this.deviceLoading = true
        listIotDevice(this.deviceQueryParams).then(response => {
            this.iotDeviceList = response.data.lise
            this.deviceTotal = response.data.count
        })
      }
    }
  }
</script>

以上代碼只實(shí)現(xiàn)了基本的單頁(yè)面勾選功能,未實(shí)現(xiàn)真正意義上的多頁(yè)面勾選,頁(yè)面切換。想要實(shí)現(xiàn),請(qǐng)看一下介紹

如何支持跨頁(yè)依然能保留記錄的功能

  • el-table 添加row-key
  • <el-table v-loading="deviceLoading" ref="multipleTable" :row-key="getRowKeys"
  • el-table-column 添加reserve-selection
  • <el-table-column type="selection" :reserve-selection="true" width="55" align="center"/>
  • 修改getDevicesList函數(shù),保證在切換Page的時(shí)候,自動(dòng)勾選之前的選項(xiàng)
...
this.iotDeviceList = response.data.list
this.devicesTotal = response.data.count
// 執(zhí)行回顯操作
this.$nextTick(() => {
    let selectItem = []
    this.iotDeviceList.forEach(item =>{
        if (this.devicesIds){
            this.devicesIds.forEach(id => {
                if (item.id === id){
                    selectItem.push(item)
                }
            })
        }
    })
    // 觸發(fā)勾選項(xiàng)
    this.$refs.multipleTable.toggleRowSelection(selectItem)
})

增加getRowKeys,保證每一個(gè)行的數(shù)據(jù)是唯一

methods: {
   getRowKeys(row){
       return row.id
   }
}

注意,在關(guān)閉dialog之后,點(diǎn)擊確定,點(diǎn)擊取消的時(shí)候,需要清理一下selection
如果不清理,目前觀察toggle數(shù)據(jù)會(huì)不斷增長(zhǎng)
this.$refs.multipleTable.clearSelection()

如何實(shí)現(xiàn)從后端記錄的勾選在前端顯示

基本思路是這樣: 從后端獲取之前勾選的device ID之后, 在每次頁(yè)面獲取數(shù)據(jù)后,觸發(fā)勾選,但是如果勾選成功后,記錄要?jiǎng)h除,否則用戶點(diǎn)擊取消以前的數(shù)據(jù),則是無(wú)效的

this.$nextTick(() => {
  ....
})
// 承接上面的代碼,下面是實(shí)際讀取后端數(shù)據(jù)觸發(fā)toggle的操作
this.$nextTick(() => {
      // let selectItem = []
      let remove_ids = []
      this.iotDeviceList.forEach(item =>{
          // 記錄從數(shù)據(jù)庫(kù)獲取的數(shù)據(jù), beforeDevicesIds是之前勾選的設(shè)備id列表
          if (this.beforeDevicesIds){
              this.beforeDevicesIds.forEach(id => {
              if (item.id === id){
                      remove_ids.push(id)
                      this.$refs.multipleTable.toggleRowSelection(item)
                  }
              })
          }
      })
      if (remove_ids.length === 0){
      }else{
          // 過(guò)濾已經(jīng)處理devices ID
          this.beforeDevicesIds = this.beforeDevicesIds.filter(id =>!remove_ids.includes(id))
      }
  }) // 因?yàn)轫?yè)面渲染的異步,所以加一個(gè)延遲刷新數(shù)據(jù)
  this.deviceLoading = false

知識(shí)點(diǎn)

Vue有個(gè)異步更新策略,就是如果數(shù)據(jù)變化,vue不會(huì)立刻更新DOM,而是開(kāi)啟一個(gè)隊(duì)列,把組件更新函數(shù)保存到隊(duì)列中,在同一事件循環(huán)中發(fā)生的所有數(shù)據(jù)變更會(huì)異步的批量更新。如果想獲取更新后的DOM狀態(tài),需要使用nextTick

使用場(chǎng)景:

  • 等待DOM更新完成
  • 避免布局抖動(dòng),比如多次數(shù)據(jù)更新
  • 獲取更新后的DOM

到此這篇關(guān)于el-table在彈窗中的使用的文章就介紹到這了,更多相關(guān)el-table彈窗使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論