1. 程式人生 > 程式設計 >ant design vue中表格指定格式渲染方式

ant design vue中表格指定格式渲染方式

注意點:定義的columns一定要寫在data中,否則在載入過程中由於渲染順序會導致其中的渲染函式無法識別

渲染方法1:

指定渲染函式:

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,index) => {
 const obj = {
  children: value,attrs: {}
 }
 return obj
}

渲染方法2:

直接呼叫對應插槽模板:

<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',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
 }
]

補充知識:Ant design vue框架,table控制元件中customRow用法的一個坑

今天在寫程式碼時,用到Ant design框架中的<a-table>控制元件,其中的一個需求是:點選table中的一行,需要執行一些操作。因為沒有預設的行點選事件,需要用到customRow來進行自定義。

這個方法,在官方的文件中,有使用說明,如下:

<Table
 customRow={(record) => {
  return {
   props: {
    xxx... //屬性
   },on: { // 事件
    click: (event) => {},// 點選行
    dblclick: (event) => {},contextmenu: (event) => {},mouseenter: (event) => {},// 滑鼠移入行
    mouseleave: (event) => {}
   },};
 )}
 customHeaderRow={(column) => {
  return {
   on: {
    click: () => {},// 點選表頭行
   }
  };
 )}
/>

官方的這個寫法,應該是屬於lamada的語法,今天我在使用時,也是使用這種寫法。

如下:

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”。

不使用lamada表示式,則不會出現這樣的問題,修改後的rowClick方法如下:

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

可正常執行,並能正確呼叫getDetailList方法

以上這篇ant design vue中表格指定格式渲染方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。