vxe-table 表格多选框回显

文章描述了在Vue.js应用中使用VXE-Table组件实现弹框表格的结构,以及两种不同的数据加载和回显默认选中状态的方法。第一种方法是通过预处理数据并设置`checkboxConfig.checkRowKeys`,而第二种方法则是通过过滤和设置`$refs.qualityTable.setCheckboxRow`来实现。此外,还展示了异步获取数据并在加载完成后设置表格状态的完整流程。

1.弹框表格结构

 

<a-modal

              v-if="visibleQuality"

              title="请选择需要提高的能力素质要求"

              :maskClosable="false"

              :visible="visibleQuality && switchStatus"

              @ok="handleOkQuality"

              @cancel="handleCancelQuality"

              cancelText="取消"

              okText="确定"

              width="600px"

            >

              <a-spin :spinning="quaLoading" tip="加载中...">

                <vxe-table

                  ref="qualityTable"

                  row-id="id"

                  :row-config="{ keyField: 'id', isHover: true }"

                  :data="rowsQuality"

                  :checkbox-config="checkboxConfig"

                  width="600px"

                  @checkbox-change="checkboxChange"

                  @checkbox-all="checkBoxAll"

                >

                  <vxe-column type="checkbox" width="60"></vxe-column>

                  <vxe-column

                    field="abilityContent"

                    title="能力素质要求"

                    width="240"

                  >

                    <template #header>

                      <span><span style="color: #f5222d">* </span> 能力素质要求</span>

                    </template>

                  </vxe-column>

                  <vxe-column

                    field="explain"

                    title="能力素质要求说明"

                    width="240"

                  ></vxe-column>

                </vxe-table>

              </a-spin>

            </a-modal>

2. 数据 

data() {

   return {

       checkboxConfig: {}, // 默认选中

     }

}

3. 实现方法,首先要异步处理,等待数据加载完毕,再去回显

3.1 第一种方法:

ps:(重要提示)设置checkboxConfig默认选中第一次有效,第二次无效,处理:请求数据点击人员时候提前请求拿到数据,给能力素质弹框加v-if重新创建

        let arr = []

        this.qualityRows.map(v => {

          arr.push(v.abilityCode)

        })

        this.$nextTick(() => {

          this.checkboxConfig = {

            checkRowKeys: arr

          }

          this.checkboxConfig.checkRowKeys = arr

          // this.$set(this.checkboxConfig, 'checkRowKeys', arr)

        })

3.2 第二种方法:

// 第二种方法过滤得到新数组arr

        let arr = []

        if (this.rowsQuality && this.qualityRows) {

          this.rowsQuality.map(v => {

            this.qualityRows.map(item => {

              if (item.abilityCode === v.id) {

                arr.push(v)

              }

            })

          })

        }

        this.$nextTick(() => {

          this.$refs.qualityTable.clearCheckboxRow()

          this.$refs.qualityTable.setCheckboxRow(arr, true)

        })

3.3  异步处理和回显完整方法:(try,catch 配合async,await 异步处理)

async addQuality () {

      this.visibleQuality = true

      this.rowsQuality = []

      // 放在点击人员时候请求,这里注释掉

      this.quaLoading = true

      try {

        let params = {

          userId: this.currentRow.userId

        }

        let w = await Api.getAbility(params)

        this.rowsQuality = w.data // 需要选择的能力项弹框的表格数据

        this.quaLoading = false

        this.$nextTick(() => {

          if (this.rowsQuality.length === 0) {

            this.demandPersonRows[this.currentRowIndex].improveAbility = false

          }

        })

      } catch (err) {

        this.quaLoading = false

      }

      if (this.rowsQuality.length === 0) {

        this.$message.warning('未设置能力素质信息请联系管理员!')

      } else {

        // 设置checkboxConfig默认选中第一次有效,第二次无效,处理:请求数据点击人员时候提前请求拿到数据,给能力素质弹框加v-if重新创建

        // let arr = []

        // this.qualityRows.map(v => {

        //   arr.push(v.abilityCode)

        // })

        // this.$nextTick(() => {

        //   this.checkboxConfig = {

        //     checkRowKeys: arr

        //   }

        //   this.checkboxConfig.checkRowKeys = arr

        //   // this.$set(this.checkboxConfig, 'checkRowKeys', arr)

        // })

        // rowsQuality:需要选择的能力项弹框的表格数据(id),qualityRows:第三个表格能力素质项表格数据(abilityCode)

        // 第一种方法过滤 得到条件item.abilityCode === v.id 为true 的新数组arr

        // let arr = this.rowsQuality.filter(v => {

        //   let flag = false

        //   this.qualityRows.map(item => {

        //     if (item.abilityCode === v.id) {

        //       return (flag = true)

        //     }

        //   });

        //   if (flag) {

        //     return true;

        //   }

        // })

        // 第二种方法过滤得到新数组arr

        let arr = []

        if (this.rowsQuality && this.qualityRows) {

          this.rowsQuality.map(v => {

            this.qualityRows.map(item => {

              if (item.abilityCode === v.id) {

                arr.push(v)

              }

            })

          })

        }

        this.$nextTick(() => {

          this.$refs.qualityTable.clearCheckboxRow()

          this.$refs.qualityTable.setCheckboxRow(arr, true)

        })

      }

    }

 

vxe-table表格复选框回显可以通过以下两种方法实现: ### 方法一:利用配置项 “checkbox-config 和 row-config” 需要在 `vxe-table` 组件中配置 `row-config` 的 `keyField` 用于标识每一行的唯一键,同时将 `checkbox-config` 的 `reserve` 属性设置为 `true`,以保留选中的数据。 示例代码如下: ```html <vxe-table ref="tableRef" :header-cell-style="{ 'text-align': 'center', backgroundColor: '#F2F2F2', color: '#000000' }" :cell-style="{ 'text-align': 'center', color: '#000000' }" height="500" min-height="48px" :data="data" border stripe resizable @checkbox-change="handleSelectionChange" @checkbox-all="selectAllCheckboxChange" style="width: 97%; margin: 0 auto" @cell-click="rowClick" :seq-config="{ seqMethod }" :row-config="{ keyField: 'id' }" :checkbox-config="{ reserve: true }" > <vxe-column type="checkbox" width="55" /> <vxe-column title="序号" type="seq" align="center" width="60" /> </vxe-table> ``` ### 方法二:使用 `setCheckboxRow` API 先获取表格数据,然后在 `$nextTick` 中查找需要回显选中的行,并使用 `setCheckboxRow` API 设置该行的选中状态。 示例代码如下: ```javascript /** 获取table的数据 */ async getTableData() { const { data: res } = await http.get("/api/list"); if (res.code !== 200) return; this.tableList = res.data; // 回显选中表格项 this.$nextTick(() => { const row = this.tableList.find((item) => item.id === this.selectedId); if (row) { this.$refs.xTable.setCheckboxRow(row, true); } }); } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值