当前位置:   article > 正文

Element-ui——对表格的增删改查以及分页制作_element ui 实现表格的弹框增删改查

element ui 实现表格的弹框增删改查

main.js 

  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. import router from './router'
  4. import store from './store'
  5. import './plugins/element.js'
  6. import Api from './api/index.js'
  7. import './mock'
  8. import { MessageBox, Message } from 'element-ui'
  9. Vue.config.productionTip = false
  10. Vue.prototype.$api = Api
  11. Vue.prototype.$confirm = MessageBox.confirm
  12. Vue.prototype.$message = Message
  13. new Vue({
  14. store,
  15. router,
  16. beforeCreate() {
  17. Vue.prototype.$bus = this
  18. },
  19. render: h => h(App)
  20. }).$mount('#app')

http.js(axios)

  1. import Axios from 'axios'
  2. const axios = Axios.create({
  3. // baseURL: process.env.NODE_ENV === 'development' ? '' : '',
  4. })
  5. /* resful语法 */
  6. // 添加请求拦截器
  7. axios.interceptors.request.use(function(config) {
  8. // 在发送请求之前做些什么
  9. console.log(config);
  10. return config;
  11. });
  12. // 添加响应拦截器
  13. axios.interceptors.response.use(function(response) {
  14. // 对响应数据做点什么
  15. return response;
  16. }, function(error) {
  17. // 对响应错误做点什么
  18. return Promise.reject(error);
  19. });
  20. /* url就是接口地址
  21. method = 'get'默认的请求方式
  22. data 放参数的位置 */
  23. export default (url, method = 'get', data = {}) => {
  24. return axios({
  25. url,
  26. method,
  27. data
  28. })
  29. }

 index.js

  1. const context = require.context('./', false, /.js$/)
  2. const modules = {}
  3. context.keys().forEach(fileName => {
  4. if (!['./index.js', './http.js'].includes(fileName)) {
  5. Object.assign(modules, context(fileName))
  6. }
  7. })
  8. export default modules

page.js

  1. import http from './http'
  2. export const getList = data => {
  3. return http('/list', 'get', data)
  4. }
  5. export const getTotal = () => {
  6. return http('/list/total')
  7. }
  8. export const getListByValue = data => {
  9. return http('/list/value', 'get', data)
  10. }
  11. export const addList = data => {
  12. return http('/list/add', 'post', data)
  13. }
  14. export const updateList = data => {
  15. return http('/list/update', 'put', data)
  16. }
  17. export const deleteList = data => {
  18. return http('/list/delete', 'delete', data)
  19. }

user.js

  1. import http from './http'
  2. export const login = data => {
  3. return http('/login', 'post', data)
  4. }

TopBar.vue

  1. <template>
  2. <div class="top-bar">
  3. <el-input placeholder="请输入内容,按回车键搜索..." clearable
  4. @clear="getListdata"
  5. @keydown.enter.native="handleSearch"
  6. v-model="inputValue">
  7. <template slot="append">
  8. <span class="search" @click="handleSearch">搜索</span>
  9. </template>
  10. </el-input>
  11. <el-button type="primary" @click="$emit('show')">添加商品</el-button>
  12. </div>
  13. </template>
  14. <script>
  15. export default {
  16. data () {
  17. return {
  18. inputValue: ''
  19. }
  20. },
  21. methods: {
  22. handleSearch () {
  23. this.$emit('handleSearch', this.inputValue)
  24. },
  25. getListdata(){
  26. this.$emit('getdataList')
  27. }
  28. }
  29. }
  30. </script>
  31. <style lang="less" scoped>
  32. .top-bar {
  33. .el-input {
  34. width: 300px;
  35. .search {
  36. cursor: pointer;
  37. color:#333;
  38. }
  39. }
  40. .el-button {
  41. margin-left: 15px;
  42. }
  43. }
  44. </style>

user.vue

  1. <template>
  2. <div>
  3. <top-bar @handleSearch="handlesearch" @getdataList="getdatali"></top-bar>
  4. <el-table :data="tableData" style="width: 100%" v-loading="loading">
  5. <el-table-column type="index" label="#" width="50">
  6. </el-table-column>
  7. <el-table-column prop="date" label="日期" width="180">
  8. </el-table-column>
  9. <el-table-column prop="name" label="姓名" width="180">
  10. </el-table-column>
  11. <el-table-column prop="address" label="地址">
  12. </el-table-column>
  13. <el-table-column prop="likes" label="爱好">
  14. </el-table-column>
  15. <el-table-column label="操作" width="200">
  16. <template v-slot:default="scope">
  17. <div class="btns">
  18. <el-button type="primary">
  19. <i class="el-icon-edit"></i>
  20. </el-button>
  21. <el-button type="danger">
  22. <i class="el-icon-delete"></i>
  23. </el-button>
  24. </div>
  25. </template>
  26. </el-table-column>
  27. </el-table>
  28. </div>
  29. </template>
  30. <script>
  31. import TopBar from '../components/main/TopBar.vue'
  32. export default {
  33. name: 'User',
  34. components: {
  35. TopBar
  36. },
  37. data() {
  38. return {
  39. loading:false,
  40. total:0,
  41. page:{
  42. size: 10,
  43. current: 1
  44. },
  45. tableData: [],
  46. search: ''
  47. }
  48. },
  49. mounted() {
  50. this.getList(this.page.current)
  51. },
  52. methods: {
  53. handleEdit(index, row) {
  54. console.log(index, row);
  55. },
  56. handleDelete(index, row) {
  57. console.log(index, row);
  58. },
  59. getList(current){
  60. this.loading = true
  61. this.$api.getList({
  62. current
  63. }).then(res=>{
  64. console.log(res);
  65. this.tableData = res.data
  66. this.loading = false
  67. })
  68. },
  69. /* 搜索 */
  70. handlesearch(value){
  71. if(value == ''){
  72. this.$message({
  73. message: '警告哦,这是一条警告消息',
  74. type: 'warning'
  75. });
  76. this.getList()
  77. return
  78. }else {
  79. this.loading = true
  80. this.$api.getListByValue({
  81. value
  82. }).then(res=>{
  83. this.tableData = res.data.list
  84. this.total = res.data.total
  85. this.loading = false
  86. })
  87. }
  88. },
  89. getdatali(){
  90. this.getList(this.page.current)
  91. }
  92. }
  93. }
  94. </script>
  95. <style>
  96. </style>

删除和分页 

  1. <template>
  2. <div>
  3. <top-bar @handleSearch="handlesearch" @getdataList="getdatali"></top-bar>
  4. <el-table
  5. :data="tableData.slice((currpage - 1) * pagesize, currpage * pagesize)"
  6. v-loading="loading"
  7. >
  8. <el-table-column type="index" label="#" width="50"> </el-table-column>
  9. <el-table-column prop="date" label="日期" width="180"> </el-table-column>
  10. <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
  11. <el-table-column prop="address" label="地址"> </el-table-column>
  12. <el-table-column prop="likes" label="爱好"> </el-table-column>
  13. <el-table-column label="操作" width="200">
  14. <template v-slot:default="scope">
  15. <div class="btns">
  16. <el-button type="primary" @click="btnDelete(scope.$index, scope.row)">
  17. <i class="el-icon-edit"></i>
  18. </el-button>
  19. <!-- 删除 -->
  20. <el-button
  21. type="danger"
  22. @click="btnDelete(scope.$index, scope.row)"
  23. >
  24. <i class="el-icon-delete"></i>
  25. </el-button>
  26. </div>
  27. </template>
  28. </el-table-column>
  29. </el-table>
  30. <el-pagination
  31. @size-change="handleSizeChange"
  32. @current-change="handleCurrentChange"
  33. :current-page="currpage"
  34. :page-sizes="[2, 5,10]"
  35. :page-size="100"
  36. layout="total, sizes, prev, pager, next, jumper"
  37. :total="total"
  38. >
  39. </el-pagination>
  40. </div>
  41. </template>
  42. <script>
  43. import TopBar from "../components/main/TopBar.vue";
  44. export default {
  45. name: "User",
  46. components: {
  47. TopBar,
  48. },
  49. data() {
  50. return {
  51. loading: false,
  52. total: 0,
  53. page: {
  54. size: 10,
  55. current: 1,
  56. },
  57. tableData: [],
  58. search: "",
  59. pagesize: 2,
  60. currpage: 1,
  61. };
  62. },
  63. mounted() {
  64. this.getList(this.page.current);
  65. // this.getTotal()
  66. },
  67. methods: {
  68. getList() {
  69. this.$api.getList().then((res) => {
  70. this.tableData = res.data;
  71. });
  72. },
  73. btnDelete(index, row) {
  74. this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
  75. confirmButtonText: "确定",
  76. cancelButtonText: "取消",
  77. type: "warning",
  78. })
  79. .then(() => {
  80. this.getList(this.page.current);
  81. this.$api
  82. .deleteList({
  83. id: row.id,
  84. })
  85. .then((res) => {
  86. console.log(res);
  87. });
  88. this.$message({
  89. type: "success",
  90. message: "删除成功!",
  91. });
  92. })
  93. .catch(() => {
  94. this.$message({
  95. type: "info",
  96. message: "已取消删除",
  97. });
  98. });
  99. },
  100. handleSizeChange(val) {
  101. this.pagesize = val;
  102. console.log(`每页 ${val} 条`);
  103. },
  104. handleCurrentChange(val) {
  105. this.currpage = val;
  106. console.log(`当前页: ${val}`);
  107. },
  108. // 编辑
  109. handleEdit(index, row) {
  110. console.log(index, row);
  111. },
  112. // 请求总页面数据
  113. getList(current) {
  114. this.loading = true;
  115. this.$api
  116. .getList({
  117. current,
  118. })
  119. .then((res) => {
  120. console.log(res);
  121. this.total = res.data.length;
  122. this.tableData = res.data;
  123. this.loading = false;
  124. });
  125. },
  126. /* 搜索 */
  127. handlesearch(value) {
  128. console.log(value);
  129. if (value == "") {
  130. this.getList(this.page.current)
  131. }
  132. else {
  133. this.loading = true;
  134. this.$api
  135. .getListByValue({
  136. value,
  137. })
  138. .then((res) => {
  139. this.tableData = res.data.list;
  140. this.total = res.data.total;
  141. this.loading = false;
  142. });
  143. }
  144. },
  145. getdatali(value) {
  146. console.log(value);
  147. this.getList(this.page.current);
  148. },
  149. getTotal(){
  150. this.$api.getTotal().then(res=>{
  151. console.log(res);
  152. this.total = res.data
  153. })
  154. }
  155. },
  156. };
  157. </script>
  158. <style>
  159. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/103174
推荐阅读
相关标签
  

闽ICP备14008679号