• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    Vue2.5 結合 Element UI 之 Table 和 Pagination 組件實現分頁功能

    來源:懂視網 責編:小采 時間:2020-11-27 22:20:45
    文檔

    Vue2.5 結合 Element UI 之 Table 和 Pagination 組件實現分頁功能

    Vue2.5 結合 Element UI 之 Table 和 Pagination 組件實現分頁功能:2017年底了,總結了這一年多來的前端之路,Vue從入門到放棄,再二進宮,從 Vue1.0 持續跟蹤到 Vue2.5。結合公司的一些實際項目,也封裝了一些比較實用的組件。 由于現在公司管理平臺主要運用Element UI,索性就結合組件Table 和 Pagination
    推薦度:
    導讀Vue2.5 結合 Element UI 之 Table 和 Pagination 組件實現分頁功能:2017年底了,總結了這一年多來的前端之路,Vue從入門到放棄,再二進宮,從 Vue1.0 持續跟蹤到 Vue2.5。結合公司的一些實際項目,也封裝了一些比較實用的組件。 由于現在公司管理平臺主要運用Element UI,索性就結合組件Table 和 Pagination

    2017年底了,總結了這一年多來的前端之路,Vue從入門到放棄,再二進宮,從 Vue1.0 持續跟蹤到 Vue2.5。結合公司的一些實際項目,也封裝了一些比較實用的組件。

    由于現在公司管理平臺主要運用Element UI,索性就結合組件Table 和 Pagination 封裝了一個支持頁面切換的Table組件,不啰嗦,直接上代碼。

    2、實現思路

    2.1、Element UI 引入(整體引入)

    main.js

    // Element UI
    import Element from 'element-ui'
    // 默認樣式
    import 'element-ui/lib/theme-chalk/index.css'

    2.2、開始封裝 iTable.vue 組件 (骨架)

    由于公司項目都是以 i 開頭,所以,為了區分組件和頁面,習慣于組件命名也以 i 開頭。 首先把 Table 、 Pagination 組件加進來

    <template>
     <div class="table">
     <!--region 表格-->
     <el-table id="iTable"></el-table>
     <!--endregion-->
     <!--region 分頁-->
     <el-pagination></el-pagination>
     <!--endregion-->
     </div>
    <template>

    養成寫注釋的好習慣,個人項目的注釋量基本上不會低于 30%

    2.3、在頁面中引用 iTable 組件,并且給 iTable 組件傳值

    <template>
     <div class="table-page">
     <i-table :list="list" 
     :total="total" 
     :otherHeight="otherHeight"
     @handleSizeChange="handleSizeChange"
     @handleIndexChange="handleIndexChange" @handleSelectionChange="handleSelectionChange"
     :options="options"
     :columns="columns"
     :operates="operates"
     @handleFilter="handleFilter"
     @handelAction="handelAction">
     </i-table>
     </div>
    </template>
    <script>
     import iTable from '../../components/Table/Index'
     export default {
     components: {iTable},
     data () {
     return {
     total: 0, // table數據總條數
     list: [], // table數據
     otherHeight: 208, // 除了table表格之外的高度,為了做table表格的高度自適應
     page: 1, // 當前頁碼
     limit: 20, // 每頁數量
     options: {
     stripe: true, // 是否為斑馬紋 table
     loading: false, // 是否添加表格loading加載動畫
     highlightCurrentRow: true, // 是否支持當前行高亮顯示
     mutiSelect: true, // 是否支持列表項選中功能
     filter: false, // 是否支持數據過濾功能
     action: false // 是否支持 表格操作功能
     }, // table 的參數
     columns: [
     {
     prop: 'id',
     label: '編號',
     align: 'center',
     width: 60
     },
     {
     prop: 'title',
     label: '標題',
     align: 'center',
     width: 400,
     formatter: (row, column, cellValue) => {
     return `<span style="white-space: nowrap;color: dodgerblue;">${row.title}</span>`
     }
     },
     {
     prop: 'state',
     label: '狀態',
     align: 'center',
     width: '160',
     render: (h, params) => {
     return h('el-tag', {
     props: {type: params.row.state === 0 ? 'success' : params.row.state === 1 ? 'info' : 'danger'} // 組件的props
     }, params.row.state === 0 ? '上架' : params.row.state === 1 ? '下架' : '審核中')
     }
     },
     ……
     ], // 需要展示的列
     operates: {
     width: 200,
     fixed: 'right',
     list: [
     {
     label: '編輯',
     type: 'warning',
     show: true,
     icon: 'el-icon-edit',
     plain: true,
     disabled: true,
     method: (index, row) => {
     this.handleEdit(index, row)
     }
     },
     {
     label: '刪除',
     type: 'danger',
     icon: 'el-icon-delete',
     show: true,
     plain: false,
     disabled: false,
     method: (index, row) => {
     this.handleDel(index, row)
     }
     }
     ]
     } // 列操作按鈕
     }
     },
     methods: {
     // 切換每頁顯示的數量
     handleSizeChange (size) {
     this.limit = size
     console.log(' this.limit:', this.limit)
     },
     // 切換頁碼
     handleIndexChange (index) {
     this.page = index
     console.log(' this.page:', this.page)
     },
     // 選中行
     handleSelectionChange (val) {
     console.log('val:', val)
     },
     // 編輯
     handleEdit (index, row) {
     console.log(' index:', index)
     console.log(' row:', row)
     },
     // 刪除
     handleDel (index, row) {
     console.log(' index:', index)
     console.log(' row:', row)
     }
     }
     }
    </script>

    除了 columns 參數和 operates 參數 之外,其它的參數應該還好理解,好的。那我們就詳細的解釋下這兩個參數,那么我們就需要結合組件iTable.vue 來講解了,接下來就給 iTable.vue 添加肌肉和血管,代碼都貼了。 比較難理解的就是columns里面的 render 參數,使用了Vue的虛擬標簽,為了就是能夠在 table 表格的列中隨心所欲的使用各種html標簽 和 element UI 的其他組件。( 你也可以直接寫,看看 table 組件是否能識別,呵呵噠! )這個估計對于剛入門的小伙伴是一個比較難理解的地方,詳細的大家可以先看下vue 的render,解釋的更清楚,如果有的小伙伴不理解,可以直接私信我~~~

    <!--region 封裝的分頁 table-->
    <template>
     <div class="table">
     <el-table id="iTable" v-loading.iTable="options.loading" :data="list" :max-height="height" :stripe="options.stripe"
     ref="mutipleTable"
     @selection-change="handleSelectionChange">
     <!--region 選擇框-->
     <el-table-column v-if="options.mutiSelect" type="selection" style="width: 55px;">
     </el-table-column>
     <!--endregion-->
     <!--region 數據列-->
     <template v-for="(column, index) in columns">
     <el-table-column :prop="column.prop"
     :label="column.label"
     :align="column.align"
     :width="column.width">
     <template slot-scope="scope">
     <template v-if="!column.render">
     <template v-if="column.formatter">
     <span v-html="column.formatter(scope.row, column)"></span>
     </template>
     <template v-else>
     <span>{{scope.row[column.prop]}}</span>
     </template>
     </template>
     <template v-else>
     <expand-dom :column="column" :row="scope.row" :render="column.render" :index="index"></expand-dom>
     </template>
     </template>
     </el-table-column>
     </template>
     <!--endregion-->
     <!--region 按鈕操作組-->
     <el-table-column ref="fixedColumn" label="操作" align="center" :width="operates.width" :fixed="operates.fixed"
     v-if="operates.list.filter(_x=>_x.show === true).length > 0">
     <template slot-scope="scope">
     <div class="operate-group">
     <template v-for="(btn, key) in operates.list">
     <div class="item" v-if="btn.show">
     <el-button :type="btn.type" size="mini" :icon="btn.icon" :disabled="btn.disabled"
     :plain="btn.plain" @click.native.prevent="btn.method(key,scope.row)">{{ btn.label }}
     </el-button>
     </div>
     </template>
     </div>
     </template>
     </el-table-column>
     <!--endregion-->
     </el-table>
     <div style="height:12px"></div>
     <!--region 分頁-->
     <el-pagination @size-change="handleSizeChange"
     @current-change="handleIndexChange"
     :page-size="pageSize"
     :page-sizes="[10, 20, 50]" :current-page="pageIndex" layout="total,sizes, prev, pager, next,jumper"
     :total="total"></el-pagination>
     <!--endregion-->
     <!--region 數據篩選-->
     <div class="filter-data fix-right" v-show="options.filter" @click="showfilterDataDialog">
     <span>篩選過濾</span>
     </div>
     <!--endregion-->
     <!--region 表格操作-->
     <div class="table-action fix-right" v-show="options.action" @click="showActionTableDialog">
     <span>表格操作</span>
     </div>
     <!--endregion-->
     </div>
    </template>
    <!--endregion-->
    <script>
     export default {
     props: {
     list: {
     type: Array,
     default: []
     }, // 數據列表
     columns: {
     type: Array,
     default: []
     }, // 需要展示的列 === prop:列數據對應的屬性,label:列名,align:對齊方式,width:列寬
     operates: {
     type: Array,
     default: []
     }, // 操作按鈕組 === label: 文本,type :類型(primary / success / warning / danger / info / text),show:是否顯示,icon:按鈕圖標,plain:是否樸素按鈕,disabled:是否禁用,method:回調方法
     total: {
     type: Number,
     default: 0
     }, // 總數
     pageSize: {
     type: Number,
     default: 20
     }, // 每頁顯示的數量
     otherHeight: {
     type: Number,
     default: 160
     }, // 用來計算表格的高度
     options: {
     type: Object,
     default: {
     stripe: false, // 是否為斑馬紋 table
     highlightCurrentRow: false // 是否要高亮當前行
     },
     filter: false,
     action: false
     } // table 表格的控制參數
     },
     components: {
     expandDom: {
     functional: true,
     props: {
     row: Object,
     render: Function,
     index: Number,
     column: {
     type: Object,
     default: null
     }
     },
     render: (h, ctx) => {
     const params = {
     row: ctx.props.row,
     index: ctx.props.index
     }
     if (ctx.props.column) params.column = ctx.props.column
     return ctx.props.render(h, params)
     }
     }
     },
     data () {
     return {
     pageIndex: 1,
     multipleSelection: [] // 多行選中
     }
     },
     mounted () {
     },
     computed: {
     height () {
     return this.$utils.Common.getWidthHeight().height - this.otherHeight
     }
     },
     methods: {
     // 切換每頁顯示的數量
     handleSizeChange (size) {
     this.$emit('handleSizeChange', size)
     this.pageIndex = 1
     },
     // 切換頁碼
     handleIndexChange (index) {
     this.$emit('handleIndexChange', index)
     this.pageIndex = index
     },
     // 多行選中
     handleSelectionChange (val) {
     this.multipleSelection = val
     this.$emit('handleSelectionChange', val)
     },
     // 顯示 篩選彈窗
     showfilterDataDialog () {
     this.$emit('handleFilter')
     },
     // 顯示 表格操作彈窗
     showActionTableDialog () {
     this.$emit('handelAction')
     }
     }
     }
    </script>
    <style lang="less" rel="stylesheet/less">
     @import "../../assets/styles/mixins";
     .table {
     height: 100%;
     .el-pagination {
     float: right;
     margin: 20px;
     }
     .el-table__header-wrapper, .el-table__fixed-header-wrapper {
     thead {
     tr {
     th {
     color: #333333;
     }
     }
     }
     }
     .el-table-column--selection .cell {
     padding: 0;
     text-align: center;
     }
     .el-table__fixed-right {
     bottom: 0 !important;
     right: 6px !important;
     z-index: 1004;
     }
     .operate-group {
     display: flex;
     flex-wrap: wrap;
     .item {
     margin-top: 4px;
     margin-bottom: 4px;
     display: block;
     flex: 0 0 50%;
     }
     }
     .filter-data {
     top: e("calc((100% - 100px) / 3)");
     background-color: rgba(0, 0, 0, 0.7);
     }
     .table-action {
     top: e("calc((100% - 100px) / 2)");
     background-color: rgba(0, 0, 0, 0.7);
     }
     .fix-right {
     position: absolute;
     right: 0;
     height: 100px;
     color: #ffffff;
     width: 30px;
     display: block;
     z-index: 1005;
     writing-mode: vertical-rl;
     text-align: center;
     line-height: 28px;
     border-bottom-left-radius: 6px;
     border-top-left-radius: 6px;
     cursor: pointer;
     }
     }
    </style>

    總結

    以上所述是小編給大家介紹的Vue2.5 結合 Element UI 之 Table 和 Pagination 組件實現分頁功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    Vue2.5 結合 Element UI 之 Table 和 Pagination 組件實現分頁功能

    Vue2.5 結合 Element UI 之 Table 和 Pagination 組件實現分頁功能:2017年底了,總結了這一年多來的前端之路,Vue從入門到放棄,再二進宮,從 Vue1.0 持續跟蹤到 Vue2.5。結合公司的一些實際項目,也封裝了一些比較實用的組件。 由于現在公司管理平臺主要運用Element UI,索性就結合組件Table 和 Pagination
    推薦度:
    標簽: VUE 組件 分頁
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 国产精品粉嫩美女在线观看| 久久99精品国产麻豆蜜芽| 国产成人综合久久精品尤物| 亚洲国产精品乱码一区二区 | 国产精品毛片一区二区| 久久99精品久久久久久久不卡| 精品国产人成亚洲区| 久久免费精品一区二区| 九九精品99久久久香蕉| 亚洲а∨天堂久久精品| 国产精品亚洲视频| 热re99久久精品国产99热| 久久久免费精品re6| 亚洲精品一级无码鲁丝片| 精品露脸国产偷人在视频| 成人国产精品一区二区视频| 欧美精品在线一区| 极品精品国产超清自在线观看| 国产精品嫩草影院AV| 久久香蕉超碰97国产精品| 亚洲日韩精品无码一区二区三区 | 伊人久久无码精品中文字幕| 看99视频日韩精品| 成人午夜精品久久久久久久小说 | 国产精品高清免费网站| 久久国产精品一区二区| 国产精品免费高清在线观看| 四虎成人精品无码| 一本色道久久88精品综合| 亚洲综合精品网站| 亚洲精品99久久久久中文字幕 | 亚洲国产综合精品一区在线播放| 久久伊人精品青青草原日本| 久久久久久久久久久免费精品| 精品久久久久久久中文字幕| 国产这里有精品| 国产精品亚洲专区无码WEB| 精品国产一区二区三区AV性色 | 精品无码国产自产拍在线观看蜜| 国产一区二区精品久久岳| 精品无码久久久久久国产|