最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決ant design vue 表格a-table二次封裝,slots渲染的問題

 更新時(shí)間:2020年10月28日 09:46:30   作者:Cookysurongbin  
這篇文章主要介紹了解決ant design vue 表格a-table二次封裝,slots渲染的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

目的就是對(duì)a-table進(jìn)行二次封裝,但是在如何顯示a-table的slot時(shí)遇到了問題,原本想法是在a-table內(nèi)把$slots都渲染,期望在使用該組件時(shí)能正確渲染,然而。。。并不會(huì)正確渲染

<template>
 <a-table
  bordered
  :scroll="{ x: scrollX, y: 600 }"
  v-bind="{...$attrs, ...$props, ...{dataSource: body, columns: header}}"
  :loading="loadingObj"
  v-on="listeners"
 >
  <template v-for="(val, slot) in $slots" :slot="slot">{{ this.$slots[slot] }}</template>
 </a-table>
</template>

后來,在某個(gè)寫法里找到了a-table有scopedSlots這個(gè)參數(shù),但是在template里直接賦值也不行,如下

<a-table
  bordered
  :scroll="{ x: scrollX, y: 600 }"
  v-bind="{...$attrs, ...$props, ...{dataSource: body, columns: header}}"
  :loading="loadingObj"
  v-on="listeners"
  :scopedSlots="$scopedSlots"
 >

可行方法

組件不采用template寫,用render()

則變成:

render () {
  const on = {
   ...this.$listeners
  }
  const props = { ...this.$attrs, ...this.$props, ...{ dataSource: this.body, columns: this.header }}
  const table = (
   <a-table bordered props={props} scopedSlots={ this.$scopedSlots } on={on}>
   </a-table>
  )
  return (
   <div class="dc-table">
    { table }
   </div>
  )
 },
methods: () {
 
}

重點(diǎn)在于scopedSlots={ this.$scopedSlots }, 這樣在使用組件的外部直接寫slot就可以正常渲染

調(diào)用方法

<dc-table
   ref="table"
   :columns="header"
   :dataSource="body"
   :loading="loading"
   :pagination="pagination"
   @change="handleTableChange"
   class="table-fit"
  >
 
    // 這里是表頭的渲染,下面會(huì)講到
   <template v-for="title in headerSlots" :slot="'$' + title">
    <span :key="title">
     <a-tooltip placement="right" trigger="click">
      <template slot="title">{{ getHeaderTarget(title).remark }}</template>{{ getHeaderTarget(title).title }}<a-icon type="info-circle"/>
     </a-tooltip>
    </span>
   </template>
  // 這里渲染列數(shù)據(jù)
   <template v-for="(item, index) in DECIMAL_PARAMS" :slot="item" slot-scope="text">
    <span :key="index">
     <!-- <span v-if="item === 'estimated_ROI'">
      <template v-if="text === 0">{{ text }}</template>
      <template v-else>{{ (text * 100) | NumberFixed }}%</template>
     </span> -->
     <span>{{ text | NumberFixed | NumberFormat }}</span>
    </span>
   </template>
  </dc-table>

如下表格數(shù)據(jù)原本是數(shù)據(jù),渲染成逢三斷一,并2位小數(shù)

this.$scopedSlots數(shù)據(jù)格式:

在header中為scopedSlots: {customRender: 'consumption'} 格式

表頭slot如何渲染

還是同一個(gè)表格,要求表頭有提示信息,所以我在表頭也做了slots渲染了a-tooltip顯示提示信息

此時(shí)header格式為

[{scopedSlots: {customRender: 'consumption', title: '$consumption'} }]

表頭渲染設(shè)置scopedSlots里title字段,名字自定義

此時(shí)的this.$scopedSlots也有$consumption表頭slot字段,但是并不能正常渲染出來

但是發(fā)現(xiàn)this.$slots里有且只有表頭的slot

此時(shí)我覺得,我應(yīng)該把$slots的內(nèi)容渲染在a-table里,則

 render () {
  const on = {
   ...this.$listeners
  }
  const props = { ...this.$attrs, ...this.$props, ...{ dataSource: this.body, columns: this.header }}
  // slots循環(huán)
  const slots = Object.keys(this.$slots).map(slot => {
   return (
    <template slot={slot}>{ this.$slots[slot] }</template>
   )
  })
  const table = (
   <a-table bordered props={props} scopedSlots={ this.$scopedSlots } on={on}>
    {slots} // 放這里
   </a-table>
  )
  return (
   <div class="dc-table">
    { table }
   </div>
  )
 },

大功告成!

補(bǔ)充知識(shí):Ant Design of Vue中 的a-table一些使用問題

1.添加行點(diǎn)擊及復(fù)選框

<template>
 <div>
  <a-table
   :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
   :columns="columns"
   :dataSource="data"
   :customRow="onClickRow"
  />
 </div>
</template>
<script>
 const columns = [
  {
   title: 'Name',
   dataIndex: 'name',
  },
  {
   title: 'Age',
   dataIndex: 'age',
  },
  {
   title: 'Address',
   dataIndex: 'address',
  },
 ];

 const data = [];
 for (let i = 0; i < 46; i++) {
  data.push({
   key: i,
   name: `Edward King ${i}`,
   age: 32,
   address: `London, Park Lane no. ${i}`,
  });
 }

 export default {
  data() {
   return {
    data,
    columns,
    selectedRowKeys: [], // Check here to configure the default column
    selectedRows: [] // 選中的整行數(shù)據(jù)
    loading: false,
   };
  },
  methods: {
   onSelectChange (selectedRowKeys, selectedRows) {
    this.selectedRowKeys = selectedRowKeys;
    this.selectedRows = selectedRows
   },
   onClickRow (record) {
    return {
     on: {
      click: () => {
       const rowKeys = this.selectedRowKeys
       const rows = this.selectedRows
       if (rowKeys.length > 0 && rowKeys.includes(record.key)) {
        rowKeys.splice(rowKeys.indexOf(record.key), 1)
        rows.splice(rowKeys.indexOf(record.key), 1)
       } else {
        rowKeys.push(record.key)
        rows.push(record)
       }
       this.selectedRowKeys = rowKeys
       this.selectedRows = rows
      }
     }
    }
   }
  },
 };
</script>

2.固定列重疊問題

官方給的建議

對(duì)于列數(shù)很多的數(shù)據(jù),可以固定前后的列,橫向滾動(dòng)查看其它數(shù)據(jù),需要和 scroll.x 配合使用。

若列頭與內(nèi)容不對(duì)齊或出現(xiàn)列重復(fù),請(qǐng)指定固定列的寬度 width。

建議指定 scroll.x 為大于表格寬度的固定值或百分比。注意,且非固定列寬度之和不要超過 scroll.x。

const columns = [
  { title: 'Full Name', width: 100, dataIndex: 'name', key: 'name', fixed: 'left' },
  { title: 'Age', width: 100, dataIndex: 'age', key: 'age', fixed: 'left' },
  { title: 'Column 1', dataIndex: 'address', key: '1' },
  { title: 'Column 2', dataIndex: 'address', key: '2' },
  { title: 'Column 3', dataIndex: 'address', key: '3' },
  { title: 'Column 4', dataIndex: 'address', key: '4' },
  { title: 'Column 5', dataIndex: 'address', key: '5' },
  { title: 'Column 6', dataIndex: 'address', key: '6' },
  { title: 'Column 7', dataIndex: 'address', key: '7' },
  { title: 'Column 8', dataIndex: 'address', key: '8' },
  {
   title: 'Action',
   key: 'operation',
   fixed: 'right',
   width: 100,
   scopedSlots: { customRender: 'action' },
  },
 ];

我在項(xiàng)目中為其他列添加width,scroll x設(shè)置為這些width之和,添加一個(gè)空列,讓這列自適應(yīng)寬度

{
 title: ''
},

3.縱向滾動(dòng)條(表格高度隨屏幕高度改變而改變)

<a-table
  :scroll={y: scrollY}
   :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
   :columns="columns"
   :dataSource="data"
   :customRow="onClickRow"
  />
data(){
 return {
 scrollY: document.body.clientHeight - 386, // 表格豎向滾動(dòng)條,386是頁面其他元素的高度
 screenHeight: document.body.clientHeight, // 屏幕的高度
 }
} 

watch: {
  // 監(jiān)聽屏幕高度改變表格高度
  screenHeight (val) {
   // 初始化表格高度
   this.scrollY = val - 386
  }

 },

mounted () {
  // 監(jiān)聽屏幕高度
  window.onresize = () => {
   return (() => {
    window.screenHeight = document.body.clientHeight
    this.screenHeight = window.screenHeight
   })()
  }
 },

以上這篇解決ant design vue 表格a-table二次封裝,slots渲染的問題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue子級(jí)如何向父級(jí)傳遞數(shù)據(jù)(自定義事件)

    Vue子級(jí)如何向父級(jí)傳遞數(shù)據(jù)(自定義事件)

    這篇文章主要介紹了Vue子級(jí)如何向父級(jí)傳遞數(shù)據(jù)(自定義事件),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 解決vue多層彈框時(shí)存在遮擋問題

    解決vue多層彈框時(shí)存在遮擋問題

    本文給大家介紹vue多層彈框時(shí)存在遮擋問題,解決思路首先想到的是找到對(duì)應(yīng)的遮擋層的css標(biāo)簽,然后修改z-index值,但是本思路只能解決首次問題,再次打開還會(huì)存在相同的問題,故該思路錯(cuò)誤,下面給大家?guī)硪环N正確的思路,一起看看吧
    2022-03-03
  • Vue3快速實(shí)現(xiàn)文件上傳OSS的方法詳解

    Vue3快速實(shí)現(xiàn)文件上傳OSS的方法詳解

    這篇文章給大家介紹了Vue3快速實(shí)現(xiàn)文件上傳OSS的方法,上傳文件可以說是經(jīng)典的需求了,在后臺(tái)管理項(xiàng)目中隨處可見,一般是由前端進(jìn)行文件上傳,然后再由后端去處理,本文旨在實(shí)現(xiàn)上傳功能,不考慮額外的功能(如文件尺寸限制),感興趣的朋友可以參考下
    2024-01-01
  • Vue+Openlayer中使用select選擇要素的實(shí)現(xiàn)代碼

    Vue+Openlayer中使用select選擇要素的實(shí)現(xiàn)代碼

    本文通過實(shí)例代碼給大家介紹Vue+Openlayer中使用select選擇要素,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-08-08
  • vue實(shí)現(xiàn)商品規(guī)格選擇功能

    vue實(shí)現(xiàn)商品規(guī)格選擇功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)商品規(guī)格選擇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • vue?openlayers實(shí)現(xiàn)臺(tái)風(fēng)軌跡示例詳解

    vue?openlayers實(shí)現(xiàn)臺(tái)風(fēng)軌跡示例詳解

    這篇文章主要為大家介紹了vue?openlayers實(shí)現(xiàn)臺(tái)風(fēng)軌跡示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Vue 2.0在IE11中打開項(xiàng)目頁面空白的問題解決

    Vue 2.0在IE11中打開項(xiàng)目頁面空白的問題解決

    這篇文章主要給大家介紹了關(guān)于Vue 2.0在ie 11中打開項(xiàng)目頁面空白問題的解決方法,文中詳細(xì)分析出現(xiàn)該問題的原因,并給出了詳細(xì)的解決方法,需要的朋友可以參考借鑒,下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • 詳解Vue中CSS樣式穿透問題

    詳解Vue中CSS樣式穿透問題

    這篇文章主要介紹了VUE中CSS樣式穿透問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • iview的table組件自帶的過濾器實(shí)現(xiàn)

    iview的table組件自帶的過濾器實(shí)現(xiàn)

    這篇文章主要介紹了iview的table組件自帶的過濾器實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Vue2.4+新增屬性.sync、$attrs、$listeners的具體使用

    Vue2.4+新增屬性.sync、$attrs、$listeners的具體使用

    這篇文章主要介紹了Vue2.4+新增屬性.sync、$attrs、$listeners的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03

最新評(píng)論

南漳县| 健康| 五莲县| 芒康县| 中阳县| 宁津县| 伊吾县| 沂水县| 涿鹿县| 镶黄旗| 东城区| 夏河县| 八宿县| 卫辉市| 青阳县| 姜堰市| 千阳县| 安宁市| 晋中市| 南充市| 乌鲁木齐市| 冷水江市| 自贡市| 大冶市| 卓尼县| 衡山县| 吴忠市| 揭西县| 丹东市| 蓬莱市| 将乐县| 洛南县| 友谊县| 康保县| 青冈县| 永顺县| 莱芜市| 阿拉善左旗| 沈丘县| 凭祥市| 漠河县|