解决Ant Design Vue table组件固定某列(fixed)和展开某行(expandedRowRender)不能同时使用以及控制只展开一行的问题

解决Ant Design Vue table组件固定某列(fixed)和展开某行(expandedRowRender)不能同时使用以及控制只展开一行的问题

在这里插入图片描述

开发项目中时使用 antdv 发现 table组件固定某列(fixed)和展开某行(expandedRowRender)不能同时使用。样式上不兼容。经大神指点,找到了办法,在此记录一下。

遇到的问题如下图:
问题
问题描述:点击左侧的加号(+)展开一行时,固定在右侧的操作列样式不能适应整个整个table的高度。

解决方法:在展开行以后对单独固定列的td单独进行高度控制。
正常的效果如下:(重新定义红色区域的高度)
在这里插入图片描述
直接上代码:

	<!-- 这是外层table,右侧操作栏固定在右侧 -->
 	<a-table
        :columns="columns"
        :data-source="sourceData"
        table-layout="auto"
        :row-key="record => record.versionId"
        class="components-table-demo-nested"
        :pagination="pagination"
        :expanded-row-keys.sync="expandedRowKeys"
        :scroll="{ x: true }"
        @change="handleTableChange"
        @expand="expandedOneRows"
      >
        <div slot="desc" slot-scope="text, record" class="update-desc">
          <a-tooltip placement="bottom">
            <template slot="title">
              <span v-html="record.updateDescHtml" />
            </template>
            {{ record.updateDesc }}
          </a-tooltip>
        </div>
        <div slot="status" slot-scope="text, record, index">
          <a-switch checked-children="已发布" un-checked-children="未发布" :checked="record.checked" @change="(checked, item, i)=>onChange(checked, record, index)" />
        </div>
        <div slot="action" slot-scope="text, record">
          <a-button type="link" icon="eye" size="small" @click="viewRelease(record.versionId)">
            查看
          </a-button>
          <a-button type="link" icon="edit" size="small"  @click="handleRelease('edit', record)">
            编辑
          </a-button>
          <a-button type="link" icon="delete" size="small"  @click="delRelease(record)">
            删除
          </a-button>
        </div>
        <!-- 这是内层table,用于展开 -->
        <a-table
          slot="expandedRowRender"
          :columns="innerColumns"
          :data-source="innerData"
          :pagination="false"
        >
          <div slot="num" slot-scope="text, record, index">{{ index+1 }}</div>
          <div slot="fileName" slot-scope="text, record" class="table-operation">
            <a :href="record.fileUrl">{{ record.fileName }}</a>
          </div>
          <div slot="operation" slot-scope="text, record" class="table-operation">
            <a :href="record.fileUrl"> <a-icon type="eye" /> 下载</a>
          </div>
        </a-table>
      </a-table>

在expandedOneRows方法里处理高度

  expandedOneRows(expanded, record) {
      // 控制只展开一行
      if (expanded) {
        // 展开前先清空,在赋值,只展开一行
        this.expandedRowKeys = []
        this.expandedRowKeys.push(record.versionId)
      } else {
        this.expandedRowKeys = []
      }
      //  解决Ant Design Vue table组件固定某列(fixed)和展开某行(expandedRowRender)不能同时使用
      this.$nextTick(() => {
        setTimeout(() => {
          const element = document.querySelectorAll(`[data-row-key='${record.versionId}-extra-row']`)
          // 计算需要展开高度, 采用append dom元素的方式撑开高度,直接修改高度,
          // 但是当前行触发moursemove事件 会触发dom style 重置
          // 33 是td 的 padding 值 加 border值
          const height = element[0].getBoundingClientRect().height - 33
          element[1].childNodes[0].innerHTML = `<div style="height: ${height}px;"></div>`
        }, 0)
      })
    },
  • 12
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Ant Design VueTable 组件中,可以通过设置 `scroll` 属性来实现固定表头和表尾。具体步骤如下: 1. 设置 `scroll` 属性 将 `scroll` 属性设置为一个对象,其中包含 `x` 或 `y` 属性,用于指定滚动条的方向。例如,设置 `scroll` 属性为 `{ x: 1500 }` 表示横向滚动条的宽度为 1500px,设置 `scroll` 属性为 `{ y: 300 }` 表示纵向滚动条的高度为 300px。 ```html <template> <a-table :columns="columns" :data-source="data" :scroll="{ x: 1500, y: 300 }"></a-table> </template> ``` 2. 设置表头和表尾 在 `columns` 中设置 `fixed` 属性,用于指定列是否固定。`fixed` 属性可以设置为 `left`、`right` 或 `true`。当设置为 `left` 时,表示该列固定在左侧;当设置为 `right` 时,表示该列固定在右侧;当设置为 `true` 时,则表示该列固定在左侧,并且前面的所有列也固定在左侧。 ```html <template> <a-table :columns="columns" :data-source="data" :scroll="{ x: 1500, y: 300 }"> <template #title> <thead> <tr> <th a-fixed="left">Name</th> <th a-fixed="left">Age</th> <th a-fixed="right">Address</th> <th>Company</th> <th>Phone</th> <th>Email</th> <th a-fixed="right">Action</th> </tr> </thead> </template> <template #footer> <tfoot> <tr> <td a-fixed="left">Total</td> <td a-fixed="left">{{ data.length }}</td> <td a-fixed="right"></td> <td></td> <td></td> <td></td> <td a-fixed="right"></td> </tr> </tfoot> </template> </a-table> </template> ``` 在上面的代码中,使用了 `title` 和 `footer` 插槽来设置表头和表尾。在表头中,通过设置 `a-fixed` 属性来指定哪些列需要固定在左侧或右侧;在表尾中,通过设置 `a-fixed` 属性来指定哪些列需要固定在左侧或右侧,并且在左侧的列中显示合计信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值