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

JxlTable 表格

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

基于 ElementUI 的 el-table 、el-table-column、el-pagination、el-button、el-row 和 el-col 標簽封裝。
基于自封組件的 jxl-tip 標簽封裝。

<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"
      >
        <!-- 表格下方追加內容 -->
        <template slot="append">
          <slot name="table-append" />
        </template>
        <!-- 表格沒有數(shù)據(jù)時顯示的內容 -->
        <template slot="empty">
          <slot name="table-empty" />
        </template>
        <!-- 表格擴展行,顯示額外的數(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, // 當前頁碼數(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'
    },
    // 自動布局,平均列寬,當數(shù)據(jù)為空時
    autoLayout: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      dataPage: [], // 每頁數(shù)據(jù),當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 當前頁
     */
    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 當前行
     * @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>
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容