search
HomeWeb Front-endJS TutorialHow to implement paging function through Table and Pagination components in Vue2.5

This article mainly introduces how Vue2.5 combines the Table and Pagination components of Element UI to realize the paging function. It is very good and has reference value. Friends in need can refer to it

It’s the end of 2017, summary In the past year or so of the front-end road, Vue has gone from getting started to giving up, then entering the palace for the second time, and has continued to track from Vue1.0 to Vue2.5. Combined with some actual projects of the company, some more practical components are also encapsulated.

Since the current company management platform mainly uses Element UI, we simply combined the components Table and Pagination to encapsulate a Table component that supports page switching. No verbosity, just enter the code directly.

2. Implementation ideas

2.1. Introduction of Element UI (overall introduction)

main .js

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

2.2. Start encapsulating the iTable.vue component (skeleton)

Since the company's projects all start with i, in order to distinguish components and pages, it is customary Component names also start with i. First, add the Table and Pagination components

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

Develop a good habit of writing comments. The amount of comments for personal projects will basically not be less than 30%

2.3. In the page Reference the iTable component and pass the value to the iTable component

<template>
 <p 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>
 </p>
</template>
<script>
 import iTable from &#39;../../components/Table/Index&#39;
 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: &#39;id&#39;,
      label: &#39;编号&#39;,
      align: &#39;center&#39;,
      width: 60
     },
     {
      prop: &#39;title&#39;,
      label: &#39;标题&#39;,
      align: &#39;center&#39;,
      width: 400,
      formatter: (row, column, cellValue) => {
       return `<span style="white-space: nowrap;color: dodgerblue;">${row.title}</span>`
      }
     },
     {
      prop: &#39;state&#39;,
      label: &#39;状态&#39;,
      align: &#39;center&#39;,
      width: &#39;160&#39;,
      render: (h, params) => {
       return h(&#39;el-tag&#39;, {
       props: {type: params.row.state === 0 ? &#39;success&#39; : params.row.state === 1 ? &#39;info&#39; : &#39;danger&#39;} // 组件的props
       }, params.row.state === 0 ? &#39;上架&#39; : params.row.state === 1 ? &#39;下架&#39; : &#39;审核中&#39;)
      }
     },
     ……
    ], // 需要展示的列
    operates: {
     width: 200,
     fixed: &#39;right&#39;,
     list: [
      {
       label: &#39;编辑&#39;,
       type: &#39;warning&#39;,
       show: true,
       icon: &#39;el-icon-edit&#39;,
       plain: true,
       disabled: true,
       method: (index, row) => {
        this.handleEdit(index, row)
       }
      },
      {
       label: &#39;删除&#39;,
       type: &#39;danger&#39;,
       icon: &#39;el-icon-delete&#39;,
       show: true,
       plain: false,
       disabled: false,
       method: (index, row) => {
        this.handleDel(index, row)
       }
      }
     ]
    } // 列操作按钮
   }
  },
  methods: {
    // 切换每页显示的数量
   handleSizeChange (size) {
    this.limit = size
    console.log(&#39; this.limit:&#39;, this.limit)
   },
   // 切换页码
   handleIndexChange (index) {
    this.page = index
    console.log(&#39; this.page:&#39;, this.page)
   },
   // 选中行
   handleSelectionChange (val) {
    console.log(&#39;val:&#39;, val)
   },
   // 编辑
   handleEdit (index, row) {
    console.log(&#39; index:&#39;, index)
    console.log(&#39; row:&#39;, row)
   },
   // 删除
   handleDel (index, row) {
    console.log(&#39; index:&#39;, index)
    console.log(&#39; row:&#39;, row)
   }
  }
 }
</script>

In addition to the columns parameter and the operators parameter, the other parameters should be easy to understand, okay. Then we will explain these two parameters in detail, then we need to combine the component iTable.vue to explain. Next, we will add muscles and blood vessels to iTable.vue, and the codes are posted. What is more difficult to understand is the render parameter in columns, which uses Vue's virtual tags in order to be able to use various html tags and other components of element UI in the columns of the table as desired. (You can also write it directly and see if the table component can be recognized, hahaha!) This is probably a difficult place to understand for those who are just getting started. For more details, you can first look at vue's render for a clearer explanation. , If some friends don’t understand, you can send me a private message directly~~~

<!--region 封装的分页 table-->
<template>
 <p 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">
   <p class="operate-group">
   <template v-for="(btn, key) in operates.list">
    <p 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>
    </p>
   </template>
   </p>
  </template>
  </el-table-column>
  <!--endregion-->
 </el-table>
 <p style="height:12px"></p>
 <!--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 数据筛选-->
 <p class="filter-data fix-right" v-show="options.filter" @click="showfilterDataDialog">
  <span>筛选过滤</span>
 </p>
 <!--endregion-->
 <!--region 表格操作-->
 <p class="table-action fix-right" v-show="options.action" @click="showActionTableDialog">
  <span>表格操作</span>
 </p>
 <!--endregion-->
 </p>
</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(&#39;handleSizeChange&#39;, size)
  this.pageIndex = 1
  },
  // 切换页码
  handleIndexChange (index) {
  this.$emit(&#39;handleIndexChange&#39;, index)
  this.pageIndex = index
  },
  // 多行选中
  handleSelectionChange (val) {
  this.multipleSelection = val
  this.$emit(&#39;handleSelectionChange&#39;, val)
  },
  // 显示 筛选弹窗
  showfilterDataDialog () {
  this.$emit(&#39;handleFilter&#39;)
  },
  // 显示 表格操作弹窗
  showActionTableDialog () {
  this.$emit(&#39;handelAction&#39;)
  }
 }
 }
</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>

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

NavigatorIOS component in React Native (detailed tutorial description)

About how to use ejsExcel template

How to create a logistics map in D3.js (detailed tutorial)

The above is the detailed content of How to implement paging function through Table and Pagination components in Vue2.5. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.