Ant Design Vue 表格自定义渲染与 customRow 避坑指南

作者:松果时间:2026-10-23 23:05:48浏览:202

本文详解 Ant Design Vue 中表格的两种指定格式渲染方式:通过 columns 定义 customRender 函数或直接调用插槽模板。重点剖析 customRow 属性在实现行点击事件时的常见陷阱,指出 Lambda 表达式中 this 指向问题导致的 undefined 报错,并提供使用普通函数定义 rowClick 的正确解决方案,确保 getDetailList 等方法能正常调用。

Ant Design Vue 表格自定义渲染方式

关键注意事项:在定义表格列配置 columns 时,必须将其放置在 data 选项中。若未这样做,在组件加载过程中,由于渲染时序的原因,列中定义的渲染函数可能无法被正确识别。

第一种渲染实现方式:

通过指定列的 customRender 函数来实现:


const columns = [
   {
    title: '排名',
    dataIndex: 'key',
    customRender: renderContent // 渲染函数的规则
   }, {
    title: '搜索关键词',
    dataIndex: 'keyword',
    customRender: (text, row, index) => {
      if (index < 4) { // 前4行数据以a标签的形式被渲染
       return <a href="javascript:;" rel="external nofollow" rel="external nofollow" >{text}</a>
      }
      return { // 否则以独占4列的文本形式被渲染
       children: text,
       attrs: {
        colSpan: 4
       }
      }
    }
   }
]
const renderContent = (value, row, index) => {
 const obj = {
  children: value,
  attrs: {}
 }
 return obj
}

第二种渲染实现方式:

直接通过模板插槽进行调用:


<a-table :columns="columns" :dataSource="data" :pagination='pagination'>
  <template slot="operation">
    <a-select placeholder="选择操作" style="width: 100%" @change="listHandleChange">
     <a-select-option value="1">项目进度</a-select-option>
     <a-select-option value="2">质量管控</a-select-option>
     <a-select-option value="3">运维监控</a-select-option>
    </a-select>
  </template>
  <template slot='progress' slot-scope="text,record">
     <span>{{text}}</span>
     <span v-if='record.progressstatus'><a-icon class='arrow-up' type="arrow-up" />    </span>
     <span v-if='!record.progressstatus'><a-icon class='arrow-down' type="arrow-down" /></span>
  </template>
</a-table>
 
const columns = [
   {
    title: '编号',
    dataIndex: 'number',
    customRender: renderContent
   }, {
    title: '项目名称',
    dataIndex: 'name',
    customRender: (text, row, index) => {
     return {
      children: <a href="javascript:;" rel="external nofollow" rel="external nofollow" >{text}</a>,
      attrs: {}
     }
    } 
   }, {
    title: '项目进度',
    dataIndex: 'progress',
    scopedSlots: { customRender: 'progress' } // 模板中对应的slot-scope可以用来传递参数,其中第一个参数是当前字段对应的值progress,第二个参数是当前字段对应的所有值对象,即整个data[n]
   }, {
    title: '操作',
    dataIndex: 'operate',
    scopedSlots: { customRender: 'operation' } // 直接对应插槽名为operation的模板
   }
]
 
const data = [
  {
  key: 6,
  number: 6,
  name: '雅典娜',
  progress: '88%',
  progressstatus: 1
 }
]

customRow 属性使用陷阱与解决方案

补充说明:在 Ant Design Vue 框架的 Table 组件中,customRow 属性的使用存在一个需要注意的细节。

在实际开发中,若需实现点击表格某一行时执行特定操作,由于该组件默认不提供行点击事件,因此必须借助 customRow 属性来自定义行为。

官方文档中对此有明确说明,具体配置如下:


<Table
 customRow={(record) => {
  return {
   props: {
    xxx... //属性
   },
   on: { // 事件
    click: (event) => {},    // 点击行
    dblclick: (event) => {},
    contextmenu: (event) => {},
    mouseenter: (event) => {}, // 鼠标移入行
    mouseleave: (event) => {}
   },

  };
 )}
 customHeaderRow={(column) => {
  return {
   on: {
    click: () => {},    // 点击表头行
   }
  };
 )}
/>

官方示例采用的是 Lambda 箭头函数语法,我在初期使用时也沿用了这种写法。

具体代码如下:


methods:{
 getDetailList(id){
  //执行具体的操作
  },
 rowClick: (record, index) => ({
    // 事件
    on: {
     click: event => {
      // 点击该行时要做的事情
      console.log('record', record)
      console.log('index', index)
      console.log('event', event)
      this.getDetailList(record.id) //这一行会报错,报未定义
     }
    }
   })
  }

然而,在运行代码时会出现报错,错误信息如下:

[Vue warn]: Error in v-on handler: “TypeError: Cannot read property ‘getDetailList' of undefined”。

若避免使用 Lambda 表达式,则不会引发此类问题。修改后的 rowClick 方法定义如下:


rowClick(record, index) {
   return {
    on: {
     click: () => {
      console.log(record, index)
      this.getDetailList(record.matbillid)
     }
    }
   }
  },

修改后代码可正常执行,并能成功调用 getDetailList 方法。

以上关于 Ant Design Vue 表格指定格式渲染及 customRow 注意事项的内容分享完毕,希望能为读者提供参考与帮助。

作者最新文章