JXL組件:jxl-table基于el-table表格封裝的組件

JxlTable 表格

滿足開發(fā)者采用配置的方式听盖,快速搭建表格數(shù)據(jù)的需求杨拐。

基于 ElementUI 的 el-table 棕所、el-table-column糊肠、el-pagination辨宠、el-button、el-row 和 el-col 標(biāo)簽封裝货裹。
基于自封組件的 jxl-tip 標(biāo)簽封裝嗤形。

<template>
  <div class="el-table-wrapper">
    <div class="el-table-body">
      <el-table
        v-loading="loading"
        row-key="id"
        :data="dataPage"
        :border="border"
        :highlight-current-row="getRowType() === 'radio'"
        :class="isAutoLayout ? 'auto-layout': ''"
        v-bind="$attrs"
        v-on="$listeners"
        @current-change="currentChange"
        @expand-change="expandChange"
        @selection-change="selectionChange"
      >
        <!-- 表格下方追加內(nèi)容 -->
        <template slot="append">
          <slot name="table-append" />
        </template>
        <!-- 表格沒有數(shù)據(jù)時顯示的內(nèi)容 -->
        <template slot="empty">
          <slot name="table-empty" />
        </template>
        <!-- 表格擴(kuò)展行,顯示額外的數(shù)據(jù) -->
        <template v-if="$scopedSlots['row-expand']">
          <el-table-column type="expand">
            <template v-slot="{row, $index}">
              <slot name="row-expand" :row="row" :index="$index + 1" />
            </template>
          </el-table-column>
        </template>
        <!-- 選擇列 -->
        <el-table-column v-if="getRowType() === 'checkbox'" type="selection" width="80" />
        <template v-for="(item, key) in columns">
          <!-- 隱藏列 -->
          <template v-if="isHidden(item)" />
          <!-- 正常列 -->
          <el-table-column
            v-else
            :key="key"
            :fixed="item.fixed"
            :prop="item.prop"
            :min-width="getMinWidth(item)"
            :width="isActions(item) ? getActionWidth(item) : getWidth(item)"
            :align="getAlign(item)"
            :header-align="getHeaderAlign(item)"
            v-bind="insertAttrs(item)"
          >
            <template slot="header">
              <!-- 表頭名稱 -->
              <span>{{ item.label }}</span>
              <!--條目提示符-->
              <jxl-tip v-if="item.tip" :options="item.tip">
                <template v-if="_get(item, 'tip.prompt.content.slotName')" v-slot:content>
                  <slot v-if="$scopedSlots['tipContent']" name="tipContent" :row="item" />
                </template>
              </jxl-tip>
            </template>
            <template v-slot="{row, $index}">
              <!-- 插槽渲染 -->
              <slot v-if="item.slot === true && $scopedSlots[item.prop]" :name="item.prop" :row="row" :index="$index + 1" />
              <slot v-else-if="_typeof(item.slotName) === String && $scopedSlots[item.slotName]" :name="item.slotName" :row="row" :index="$index + 1" />
              <!-- 方法渲染 -->
              <template v-else-if="typeof item.render === 'function'">{{ item.render(row, $index + 1) }}</template>
              <!-- 序號渲染 -->
              <template v-else-if="item.type === 'index' || item.prop === 'index'">{{ $index + 1 }}</template>
              <!-- 操作按鈕 -->
              <template v-else-if="isActions(item)">
                <div :class="item.overlayClassName">
                  <template v-for="(btn, key1) in item.actions">
                    <el-button
                      v-if="buttonVisible(btn, row)"
                      :key="key1"
                      :disabled="buttonDisabled(btn, row)"
                      :loading="btn.loading"
                      v-bind="btn.attrs"
                      v-on="eventsBindHandle(row, btn.events)"
                      @click="buttonClick(btn, row)"
                    >{{ buttonText(btn, row) }}</el-button>
                  </template>
                </div>
              </template>
              <!-- 普通文本 -->
              <template v-else>
                {{ friendly(_get(row, item.prop), isVoid(item.void) ? '-' : item.void) }}
              </template>
            </template>
          </el-table-column>
        </template>
      </el-table>
    </div>
    <el-row v-if="$scopedSlots['table-tip'] || paginationVisible()" type="flex" justify="end" class="el-table-pagination">
      <el-col :xs="24" :sm="24" :lg="8" :xl="8">
        <div v-if="$scopedSlots['table-tip']" class="el-table-tip">
          <slot name="table-tip" />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="16" :xl="16">
        <el-pagination
          v-if="paginationVisible()"
          :class="pagination.overlayClassName"
          :layout="pagination.layout"
          :background="pagination.background"
          :total="pagination.total"
          :current-page="pagination.pageNum"
          :page-size="pagination.pageSize"
          :page-sizes="pagination.pageSizeOptions"
          @current-change="pageNumChange"
          @size-change="pageSizeChange"
        />
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { _get, _typeof } from '@/components/libs/util'
import JxlTip from '@/components/libs/Tip/JxlTip'
import mixin from '@/components/libs/mixin'

export default {
  name: 'JxlTable',
  components: { JxlTip },
  mixins: [mixin],
  props: {
    rowSelection: {
      type: Object,
      default: null
    },
    rowKey: {
      type: [String, Function],
      default: 'key',
      required: true
    },
    data: {
      type: Array,
      required: true
    },
    columns: {
      type: [Object, Array],
      required: true
    },
    pagination: {
      type: [Object, Boolean],
      default: () => {
        return {
          total: 0, // 總條數(shù)
          pageNum: 1, // 當(dāng)前頁碼數(shù)
          pageSize: 10, // 每頁顯示條數(shù)
          pageSizeOptions: [10, 20, 30, 50], // 指定每頁可以顯示多少條
          layout: 'total, prev, pager, next, sizes, jumper',
          background: true,
          overlayClassName: ''
        }
      }
    },
    loading: {
      type: Boolean,
      default: false
    },
    border: {
      type: Boolean,
      default: false
    },
    align: {
      type: String,
      default: 'left'
    },
    headerAlign: {
      type: String,
      default: 'left'
    },
    // 自動布局弧圆,平均列寬赋兵,當(dāng)數(shù)據(jù)為空時
    autoLayout: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      dataPage: [], // 每頁數(shù)據(jù),當(dāng)data的數(shù)據(jù)大于pageSize時需要用到
      isAutoLayout: false // 是否自動布局
    }
  },
  watch: {
    data: {
      handler: function() {
        this.dataTotalChange() // 數(shù)據(jù)總量發(fā)生改變
      },
      deep: true
    }
  },
  created() {
    this.dataTotalChange() // 數(shù)據(jù)總量發(fā)生改變
  },
  methods: {
    /**
     * 列顯示條件發(fā)生改變
     */
    columnsChange() {
      Object.keys(this.columns).map(key => {
        const OBJ = this.columns[key]
        if (this._typeof(OBJ.condition) !== Function) return
        OBJ.hidden = OBJ.condition()
      })
    },
    /**
     * 頁碼頁數(shù)發(fā)生改變
     * @param pageNum 當(dāng)前頁
     */
    pageNumChange(pageNum) {
      if (this._typeof(this.pagination) !== Object) return
      this.pagination.pageNum = pageNum
      this.pagingWayHandle() // 分頁方式處理
      if (this._typeof(this.pagination['onChange']) !== Function) return
      this.pagination['onChange'](pageNum, this.pagination['pageSize'])
    },
    /**
     * 頁碼大小發(fā)生改變
     * @param pageSize 每頁條數(shù)
     */
    pageSizeChange(pageSize) {
      if (this._typeof(this.pagination) !== Object) return
      this.pagination['pageSize'] = pageSize
      this.pagingWayHandle() // 分頁方式處理
      if (this._typeof(this.pagination['onPageSizeChange']) !== Function) return
      this.pagination['onPageSizeChange'](this.pagination['pageNum'], pageSize)
    },
    /**
     * 分頁器可見
     * @returns {boolean}
     */
    paginationVisible() {
      if (this._typeof(this.pagination) !== Object) return false
      if (this._typeof(this.pagination['total']) !== Number) return false
      return this.pagination['total'] > 0
    },
    /**
     * 獲取行類型搔预,普通霹期、單選、多選
     * @returns {string|*}
     */
    getRowType() {
      if (this._typeof(this.rowSelection) !== Object) return ''
      if (this._typeof(this.rowSelection.type) !== String) return ''
      return this.rowSelection.type
    },
    /**
     * 數(shù)據(jù)總量發(fā)生改變
     */
    dataTotalChange() {
      if (this._typeof(this.pagination) !== Object) {
        this.dataPage = this.data // 不分頁則直接全部顯示
        return false
      }
      if (this._typeof(this.pagination['total']) === Number) {
        if (this.pagination['total'] < this.data.length) {
          this.pagination['total'] = this.data.length
        }
      } else {
        this.pagination['total'] = this.data.length
      }
      this.pagingWayHandle() // 分頁方式處理
      this.getAutoLayout() // 獲取布局
    },
    /**
     * 分頁方式處理
     */
    pagingWayHandle() {
      /**
       * 數(shù)據(jù)條數(shù)比規(guī)定的頁碼大小要大拯田,說明需要手動支持分頁功能
       */
      if (this.data.length > this.pagination['pageSize']) {
        this.pagingHandle() // 靜態(tài)數(shù)據(jù)分頁處理
      } else {
        this.dataPage = this.data // 動態(tài)數(shù)據(jù)分頁處理
      }
    },
    /**
     * 靜態(tài)數(shù)據(jù)分頁處理
     */
    pagingHandle() {
      const firstIndex = (this.pagination['pageNum'] - 1) * this.pagination['pageSize']
      const lastIndex = firstIndex + this.pagination['pageSize']
      this.dataPage = this.data.slice(firstIndex, lastIndex)
    },
    /**
     * 選擇行發(fā)生改變
     */
    selectionChange(rows) {
      const keys = []
      rows.map(item => {
        let key
        if (this._typeof(this.rowKey) === Function) {
          key = this.rowKey(item)
        } else if (this._typeof(this.rowKey) === String) {
          key = item[this.rowKey]
        }
        keys.push(key)
      })
      if (this.rowSelection.selectedRows) {
        this.rowSelection.selectedRows = rows
      }
      if (this.rowSelection.selectedRowKeys) {
        this.rowSelection.selectedRowKeys = keys
      }
      if (this._typeof(this.rowSelection.onSelectedRowChange) === Function) {
        this.rowSelection.onSelectedRowChange(keys, rows)
      }
    },
    /**
     * 單選行改變
     * @param currentRow 當(dāng)前行
     * @param oldCurrentRow 上一行
     */
    currentChange(currentRow, oldCurrentRow) {
      if (this.getRowType() !== 'radio') return false
      this.selectionChange([currentRow]) // 選擇行發(fā)生改變
    },
    /**
     * 行展開改變
     * @param row
     * @param expandedRows 所有展開的行
     */
    expandChange(row, expandedRows) {
      if (this._typeof(_get(this.rowSelection, 'onExpandChange')) !== Function) return
      if (this._typeof(expandedRows) === Array) this.rowSelection.onExpandChange(row, expandedRows.includes(row), expandedRows)
      if (this._typeof(expandedRows) === Boolean) this.rowSelection.onExpandChange(row, expandedRows)
    },
    /**
     * 事件綁定處理
     * @param item
     * @param events
     */
    eventsBindHandle(item, events) {
      if (this._typeof(events) !== Object) return undefined
      const newEvents = {}
      Object.keys(events).map(key => {
        newEvents[key] = (value, value2) => { events[key](item, value, value2) }
      })
      return newEvents
    },
    /**
     * 按鈕可見
     * @param item 被點擊的按鈕
     * @param row 本行的數(shù)據(jù)
     */
    buttonVisible(item, row) {
      return this.isVisible(item, row)
    },
    /**
     * 按鈕文本
     * @param item 被點擊的按鈕
     * @param row 本行的數(shù)據(jù)
     */
    buttonText(item, row) {
      if (this._typeof(item.text) === Function) return item.text(row)
      return item.text
    },
    /**
     * 按鈕隱藏
     * @param item 被點擊的按鈕
     * @param row 本行的數(shù)據(jù)
     */
    buttonDisabled(item, row) {
      return this.isDisabled(item, row)
    },
    /**
     * 按鈕點擊
     * @param btn 被點擊的按鈕
     * @param row 本行的數(shù)據(jù)
     */
    buttonClick(btn, row) {
      if (this._typeof(btn.onClick) !== Function) return void (0)
      btn.onClick(row)
    },
    /**
     * 是否是操作列
     * @param item
     */
    isActions(item) {
      return this._typein(item.actions, [Array, Object])
    },
    /**
     * 獲取操作列寬度
     */
    getActionWidth(item) {
      if (!item.autoWidth || !this.isActions(item)) return this.getWidth(item)
      let width = 0
      let number = 0
      Object.keys(item.actions).map(key => {
        if (!this.isHidden(item.actions[key])) {
          number += 1
          width += item.actions[key].width
        }
      })
      return width + (item.cellPadding * 2) + item.marginRight + item.spaceBetween * (number - 1)
    },
    /**
     * 獲取列的寬度
     * @param item
     */
    getWidth(item) {
      if (this.isVoid(item.width)) return ''
      return item.width
    },
    /**
     * 獲取列的最小寬度
     * @param item
     */
    getMinWidth(item) {
      if (this.isVoid(item.minWidth)) return 120
      return item.minWidth
    },
    /**
     * 獲取列的對齊方式
     * @param item
     */
    getAlign(item) {
      if (this._typeof(item.align) === String) return item.align
      if (this._typeof(this.align) === String) return this.align
      return 'left'
    },
    /**
     * 獲取表頭列的對齊方式
     * @param item
     */
    getHeaderAlign(item) {
      if (this._typeof(item.headerAlign) === String) return item.headerAlign
      if (this._typeof(this.headerAlign) === String) return this.headerAlign
      return 'left'
    },
    /**
     * 獲取布局
     * @returns {boolean}
     */
    getAutoLayout() {
      if (!this.autoLayout) {
        this.isAutoLayout = false
      } else if (this._typeof(this.pagination) === Boolean) {
        this.isAutoLayout = this.data.length === 0
      } else if (this._typeof(this.pagination) === Object) {
        this.isAutoLayout = this.pagination['total'] === 0
      }
    },
    /**
     * 插入屬性
     * @param item
     */
    insertAttrs(item) {
      const attrs = _typeof(item.attrs) === Object ? item.attrs : {}
      if ('show-overflow-tooltip' in attrs || 'showOverflowTooltip' in attrs) return attrs
      if (this.isActions(item)) return Object.assign(attrs, { 'show-overflow-tooltip': false })
      return Object.assign(attrs, { 'show-overflow-tooltip': true })
    }
  }
}
</script>
<style lang="less" scoped>
.el-table-wrapper {
  .el-table-body {
    padding: 0;

    /deep/ .el-table {
      &.auto-layout colgroup {
        display: none;
      }
      &.auto-layout .el-table__cell.is-hidden > * {
        visibility: visible;
      }
      .header-label-icon {
        margin-left: 5px;
      }
      th {
        background-color: #f5f8fa;
        color: #252631;
        font-weight: 400;
      }
    }
  }
  .el-table-pagination {
    margin-top: 20px;

    .el-table-tip {
      padding: 0 0 20px 20px;
      color: #778ca2;
      font-size: 14px;
    }
    /**分頁樣式*/
    /deep/ .el-pagination {
      padding: 0 20px 20px 0;
      text-align: right;
      font-weight: 500;
      position: relative;
      white-space: normal;

      .el-pager {
        li {
          padding: 0 0;
          height: 28px;
          line-height: 28px;
        }
        li:not(.disabled).active {
          background-color: #409EFF;
          color: #FFF;
        }
        li:hover {
          border: 1px solid #297aff;
          color: #ffffff;
        }
        &:not(.disabled).active {
          border: none;
        }
        &:not(.disabled).active:hover {
          border: none;
        }
      }
      &.is-background {
        .btn-next,
        .btn-prev,
        .el-pager li {
          background-color: #ffffff;
          min-width: 28px;
          border: 1px solid #ececec;
          color: #a6b6c6;

          &:hover {
            border: 1px solid #297aff;
            color: #297aff;
          }
        }

        .btn-next.disabled,
        .btn-next:disabled,
        .btn-prev.disabled,
        .btn-prev:disabled,
        .el-pager li.disabled {
          background-color: #f7f7f7;
          min-width: 28px;
          border: 1px solid #e3e3e3;
          color: #a6b6c6;
        }

        .btn-next:not(.disabled).active {
          border: 1px solid #297aff;
          color: #ffffff;
        }
        .btn-prev:not(.disabled).active {
          border: 1px solid #297aff;
          color: #ffffff;
        }
        .btn-next:not(.disabled):hover,
        .btn-prev:not(.disabled):hover {
          border: 1px solid #297aff;
          color: #297aff;
        }
        .btn-next:disabled:hover,
        .btn-prev:disabled:hover {
          background-color: #f7f7f7;
          border: 1px solid #e3e3e3;
          color: #a6b6c6;
        }
        .btn-next {
          margin-right: 9px;
        }
      }
      .el-pagination__total {
        color: #b4b9c6;
        display: inline-block !important;
      }
      .el-pagination__sizes .el-input__inner {
        color: #b4b9c6;
      }
      .el-pagination__jump {
        color: #b4b9c6;
        margin-left: 0;
      }
      .el-select .el-input {
        width: 82px;
      }
    }
  }
}
</style>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末经伙,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子勿锅,更是在濱河造成了極大的恐慌,老刑警劉巖枣氧,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件溢十,死亡現(xiàn)場離奇詭異,居然都是意外死亡达吞,警方通過查閱死者的電腦和手機(jī)张弛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來酪劫,“玉大人吞鸭,你說我怎么就攤上這事「苍悖” “怎么了刻剥?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長滩字。 經(jīng)常有香客問我造虏,道長御吞,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任漓藕,我火速辦了婚禮陶珠,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘享钞。我一直安慰自己揍诽,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布栗竖。 她就那樣靜靜地躺著暑脆,像睡著了一般。 火紅的嫁衣襯著肌膚如雪划滋。 梳的紋絲不亂的頭發(fā)上饵筑,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天,我揣著相機(jī)與錄音处坪,去河邊找鬼根资。 笑死,一個胖子當(dāng)著我的面吹牛同窘,可吹牛的內(nèi)容都是我干的玄帕。 我是一名探鬼主播,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼想邦,長吁一口氣:“原來是場噩夢啊……” “哼裤纹!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起丧没,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤鹰椒,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后呕童,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體漆际,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年夺饲,在試婚紗的時候發(fā)現(xiàn)自己被綠了奸汇。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡往声,死狀恐怖擂找,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情浩销,我是刑警寧澤贯涎,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站撼嗓,受9級特大地震影響柬采,放射性物質(zhì)發(fā)生泄漏欢唾。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一粉捻、第九天 我趴在偏房一處隱蔽的房頂上張望礁遣。 院中可真熱鬧,春花似錦肩刃、人聲如沸祟霍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽沸呐。三九已至,卻和暖如春呢燥,著一層夾襖步出監(jiān)牢的瞬間崭添,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工叛氨, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留呼渣,地道東北人。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓寞埠,卻偏偏與公主長得像屁置,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子仁连,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容