el-table 纯前端简易分页增删改查

<template>
  <div>
    <!-- 查询框 -->
    <el-input v-model="keyword" placeholder="请输入关键字"></el-input>
    <el-button @click="search">搜索</el-button>

    <!-- 添加按钮 -->
    <el-button @click="showAddDialog">添加</el-button>

    <!-- 表格 -->
    <el-table :data="pagedTableData" style="width: 100%">
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="showEditDialog(scope.row)">编辑</el-button>
          <el-button @click="deleteRow(scope.$index)">删除</el-button>
          <el-button @click="reverseSearch(scope.row)">反查</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 对话框 -->
    <el-dialog title="编辑" :visible.sync="dialogVisible">
      <el-input v-model="editForm.name" placeholder="名称"></el-input>
      <el-button @click="saveEdit">保存</el-button>
    </el-dialog>

    <!-- 分页 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[5, 10, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.length">
    </el-pagination>
  </div>
</template>

<script>
import {
  Button,
  Dialog,
  Input,
  Message,
  MessageBox,
  Option,
  Pagination,
  Select,
  Table,
  TableColumn
} from 'element-ui';
export default {
  components: {
    [Table.name]: Table,
    [TableColumn.name]: TableColumn,
    [Button.name]: Button,
    [Pagination.name]: Pagination,
    [Input.name]: Input,
    [Select.name]: Select,
    [Option.name]: Option,
    [Dialog.name]: Dialog,
    [Message.name]: Message,
    [MessageBox.name]: MessageBox,
  },
  data() {
    return {
      tableData: [],
      pagedTableData: [],
      tempData: [],
      keyword: '',
      dialogVisible: false,
      editIndex: -1,
      editForm: {
        id: null,
        name: ''
      },
      currentPage: 1,
      pageSize: 5
    }
  },
  computed: {
    startIndex() {
      return (this.currentPage - 1) * this.pageSize;
    },
    endIndex() {
      return this.currentPage * this.pageSize;
    }
  },
  mounted() {
    this.getList()
    this.updatePagedTableData();
  },
  watch: {
    tableData: {
      handler() {
        this.updatePagedTableData();
      },
      deep: true
    },
    pageSize() {
      this.updatePagedTableData();
    },
    currentPage() {
      this.updatePagedTableData();
    }
  },
  methods: {
    getList() {
      this.tableData = [
        { id: 1, name: '项目A' },
        { id: 2, name: '项目B' },
        { id: 3, name: '项目C' },
        { id: 4, name: '项目D' },
        { id: 5, name: '项目E' },
        { id: 6, name: '项目F' },
        { id: 7, name: '项目G' },
        { id: 8, name: '项目H' }
      ];
      this.tempData = this.tableData
    },
    updatePagedTableData() {
      this.pagedTableData = this.tableData.slice(this.startIndex, this.endIndex);
    },
    search() {
      // 实现搜索功能逻辑
      if (this.keyword == '') {
        this.tableData = this.tempData
      } else {
        const searchArr = []
        for (let item of this.tempData) {
          if (
            item.name.includes(this.keyword)
            // || item.author.includes(this.select)
          ) {
            // 如果tempData中的某一条目中的某个属性包含输入框中的内容时,
            // 就将这个条目放入tableData中,因为tableData中保存的是用来向表格中展示的数据
            searchArr.push(item);
          }
        }
        this.tableData = searchArr
      }
    },
    showAddDialog() {
      this.editForm.id = null;
      this.editForm.name = '';
      this.dialogVisible = true;
    },
    showEditDialog(row) {
      this.editIndex = this.tableData.indexOf(row);
      this.editForm = { ...row };
      this.dialogVisible = true;
    },
    saveEdit() {
      if (this.editIndex > -1) {
        this.tableData.splice(this.editIndex, 1, this.editForm);
      } else {
        this.tableData.push({ id: this.tableData.length + 1, ...this.editForm });
      }
      this.dialogVisible = false;
    },
    deleteRow(index) {
      this.tableData.splice(index, 1);
    },
    reverseSearch(row) {
      // 反查功能逻辑
      console.log("反查:" + row.name);
    },
    handleSizeChange(val) {
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    }
  }

}
</script>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-table 是一个基于 Element UI 的表格组件,而 el-select 是 Element UI 提供的下拉选择框组件。在 el-table 中实现 el-select 的增、改、删、查操作,需要对 el-table 的数据进行操作,并且在 el-table 的列中渲染 el-select 组件。 1. 增加操作: - 在 el-table 的数据源中添加一条数据,例如使用数组的 push 方法或者 Vue 的 $set 方法。 - 在 el-table 的列定义中,使用 slot-scope 来自定义渲染该列的内容,并在其中使用 el-select 组件。 2. 修改操作: - 找到要修改的数据在数据源中的索引位置。 - 更新该索引位置上的数据。 - 在 el-table 的列定义中,使用 slot-scope 来自定义渲染该列的内容,并在其中使用 el-select 组件。 3. 删除操作: - 找到要删除的数据在数据源中的索引位置。 - 使用数组的 splice 方法或者 Vue 的 $delete 方法删除该索引位置上的数据。 4. 查询操作: - 根据需要,可以通过 el-table 的 filter-method 或者自定义筛选方法来实现查询功能。 - 在 el-select 组件中设置好查询条件,通过监听 el-select 的 change 事件或者使用 v-model 来获取用户选择的查询条件。 - 在 el-table 的列定义中,使用 filter-method 属性来指定过滤方法。 需要注意的是,以上提到的操作都是基于 Vue.jsElement UI 的使用方法。具体的实现方式可以参考 Element UI 的官方文档以及 Vue.js 的文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值