Ant Design of Vue表格点击行展开与操作单元格冲突

Ant Design of Vue表格点击行展开与操作单元格冲突

问题:

使用Ant Design of Vue的Table组件,实现表格嵌套表格,并且点击父表格行展开子表格,由于父表格中有操作单元格,点击编辑或者删除时候,子表格也会切换显示。

原因:

事件冒泡,点击编辑或者删除时候,点击事件冒泡到父表格行,触发了父表格行的点击事件,才会出现上面的问题。

<template>
  <a-table
    rowKey="id"
    :rowSelection="{
      selectedRowKeys: rowSelected,
      onChange: key => rowSelectChange(key)
    }"
    :expandedRowKeys="expandedRowKeys"
    :columns="columns"
    :dataSource="dataList"
    :expandRowByClick="true"
    @expand="expandBtn">
    <template slot="operate" slot-scope="text, record">
      <div @click.stop>
        <a @click="() => editBtn(record)" href="javascript:;">编辑</a>
        <a-divider type="vertical"/>
        <a-popconfirm title="是否删除?" @confirm="deleteBtn(record.id)">
          <a href="javascript:;">删除</a>
        </a-popconfirm>
      </div>
    </template>
    <div slot="expandedRowRender" slot-scope="record">
      <a-table
      rowKey="id"
      :showHeader="false"
      :columns="columns"
      :loading="record.loading"
      :pagination="false"
      :dataSource="record.expand" :rowSelection="{ selectedRowKeys: expandedSelected, onChange: key => innerRowSelectChange(key)}" >
      <template slot="operate" slot-scope="value, row">
        <div>
          <a @click="() => editBtn(row)" href="javascript:;">编辑</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="是否删除?" @confirm="deleteBtn(row.id)">
            <a href="javascript:;">删除</a>
          </a-popconfirm>
        </div>
      </template>
      </a-table>
    </div>
  </a-table>
</template>
解决办法:

既然是事件冒泡,那就阻止冒泡,vue中有事件修饰符可以解决。

  • 给编辑和删除分别阻止冒泡(父表格)

    <a @click.stop="() => editBtn(row)" href="javascript:;">编辑</a>
    
    <a-popconfirm title="是否删除?" @confirm="deleteBtn(row.id)">
     <a @click.stop="" href="javascript:;">删除</a>
    </a-popconfirm>
    

    注意:删除的阻止冒泡要加在a-popconfirm内部

  • 给编辑表格的最外层加,此时只需要一个(父表格)

    <div @click.stop="">
    	<a @click="() => editBtn(row)" href="javascript:;">编辑</a>
    	<a-divider type="vertical"/>
    	<a-popconfirm title="是否删除?" @confirm="deleteBtn(row.id)">
    		<a href="javascript:;">删除</a>
    	</a-popconfirm>
    </div>
    
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant Design of Vue 提供了一个`<a-table>`组件,可以用来展示和操作表格数据。在这个组件中,合并单元格需要使用`customRow`属性,结合自定义的方法来实现。 具体步骤如下: 1. 定义表格数据源: ```js data() { return { dataSource: [ { key: '1', name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park', children: [ { name: 'Lucy Brown', age: 6 }, { name: 'Tom Brown', age: 4 }, ], }, { key: '2', name: 'Joe Black', age: 42, address: 'London No. 1 Lake Park', children: [ { name: 'Jim Black', age: 6 }, ], }, { key: '3', name: 'Jim Green', age: 32, address: 'Sidney No. 1 Lake Park', children: [], }, ], }; }, ``` 2. 定义表格列的配置项: ```js columns: [ { title: 'Name', dataIndex: 'name', key: 'name', customRow(row, index) { const { dataSource } = this; const { children } = dataSource[index]; const rowSpan = children.length + 1; return { props: { rowSpan } }; }, }, { title: 'Age', dataIndex: 'age', key: 'age', }, { title: 'Address', dataIndex: 'address', key: 'address', }, { title: 'Children', dataIndex: 'children', key: 'children', slots: { customRender: 'children' }, }, ], ``` 在上面的配置项中,`customRow`属性定义了如何合并单元格。具体来说,它会在渲染每一数据时调用,传入两个参数:`row`表示当前的数据,`index`表示当前的索引。在方法体内,我们可以通过`this.dataSource`获取表格数据源,从而计算出当前的`rowSpan`值,即合并的数。 3. 在表格中使用自定义的列配置项: ```html <a-table :columns="columns" :data-source="dataSource"> <template #children="{ text }"> <span v-for="child in text" :key="child.name"> {{ child.name }} <a-divider type="vertical" /> {{ child.age }} </span> </template> </a-table> ``` 在上面的代码中,我们使用`<template>`标签来定义自定义的列渲染方式。其中,`#children`表示渲染`children`列,并传入`{ text }`作为参数,`text`表示当前的`children`属性。在模板内容中,我们可以通过`v-for`来遍历`children`数组,渲染出每个子元素的信息。 以上就是使用 Ant Design of Vue 实现合并表格的方法。需要注意的是,如果表格数据源中有嵌套的数组,我们需要在自定义的列渲染方式中进特殊处理,以展示嵌套数据的信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值