Element-ui——对表格的增删改查以及分页制作

main.js 

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/element.js'
import Api from './api/index.js'
import './mock'
import { MessageBox, Message } from 'element-ui'

Vue.config.productionTip = false
Vue.prototype.$api = Api
Vue.prototype.$confirm = MessageBox.confirm
Vue.prototype.$message = Message

new Vue({
  store,
  router,
  beforeCreate() {
  	Vue.prototype.$bus = this
  },
  render: h => h(App)
}).$mount('#app')

http.js(axios)

import Axios from 'axios'
const axios = Axios.create({
	// baseURL: process.env.NODE_ENV === 'development' ? '' : '',
})
/* resful语法 */

// 添加请求拦截器
axios.interceptors.request.use(function(config) {
	// 在发送请求之前做些什么
	console.log(config);
	return config;
});


// 添加响应拦截器
axios.interceptors.response.use(function(response) {
	// 对响应数据做点什么
	
	return response;
}, function(error) {
	// 对响应错误做点什么
	return Promise.reject(error);
});
/* url就是接口地址
method = 'get'默认的请求方式
data 放参数的位置 */
export default (url, method = 'get', data = {}) => {
	return axios({
		url,
		method,
		data
	})
}

 index.js

const context = require.context('./', false, /.js$/)
const modules = {}
context.keys().forEach(fileName => {
  if (!['./index.js', './http.js'].includes(fileName)) {
    Object.assign(modules, context(fileName))
  }
})
export default modules

page.js

import http from './http'

export const getList = data => {
  return http('/list', 'get', data)
}

export const getTotal = () => {
  return http('/list/total')
}

export const getListByValue = data => {
  return http('/list/value', 'get', data)
}

export const addList = data => {
  return http('/list/add', 'post', data)
}

export const updateList = data => {
  return http('/list/update', 'put', data)
}

export const deleteList = data => {
  return http('/list/delete', 'delete', data)
}

user.js

import http from './http'

export const login = data => {
  return http('/login', 'post', data)
}

TopBar.vue

<template>
  <div class="top-bar">
    <el-input placeholder="请输入内容,按回车键搜索..." clearable  
	@clear="getListdata" 
	@keydown.enter.native="handleSearch" 
	v-model="inputValue">
      <template slot="append">
       <span class="search" @click="handleSearch">搜索</span>
      </template>
    </el-input>
    <el-button type="primary" @click="$emit('show')">添加商品</el-button>
  </div>
</template>

<script>
export default {
  data () {
    return {
      inputValue: ''
    }
  },
  methods: {
    handleSearch () {
      this.$emit('handleSearch', this.inputValue)
    },
	getListdata(){
		this.$emit('getdataList')
	}
  }
}
</script>

<style lang="less" scoped>
.top-bar {
  .el-input {
    width: 300px;
	.search {
		cursor: pointer;
		color:#333;
	}
  }
  .el-button {
    margin-left: 15px;
  }
}
</style>

user.vue

<template>
	<div>
		<top-bar @handleSearch="handlesearch" @getdataList="getdatali"></top-bar>
		<el-table :data="tableData" style="width: 100%" v-loading="loading">
			<el-table-column type="index" label="#" width="50">
			</el-table-column>
			<el-table-column prop="date" label="日期" width="180">
			</el-table-column>
			<el-table-column prop="name" label="姓名" width="180">
			</el-table-column>
			<el-table-column prop="address" label="地址">
			</el-table-column>
			<el-table-column prop="likes" label="爱好">
			</el-table-column>
			<el-table-column label="操作" width="200">
				<template v-slot:default="scope">
					<div class="btns">
						<el-button type="primary">
							<i class="el-icon-edit"></i>
						</el-button>
						<el-button type="danger">
							<i class="el-icon-delete"></i>
						</el-button>
					</div>
				</template>
			</el-table-column>
		</el-table>
	</div>
</template>

<script>
	import TopBar from '../components/main/TopBar.vue'
	export default {
		name: 'User',
		components: {
			TopBar
		},
		data() {
			return {
				loading:false,
				total:0,
				page:{
					size: 10,
					current: 1
				},
				tableData: [],
				search: ''
			}
		},
		mounted() {
			this.getList(this.page.current)
		},
		methods: {
			handleEdit(index, row) {
				console.log(index, row);
			},
			handleDelete(index, row) {
				console.log(index, row);
			},
			getList(current){
				this.loading = true
				this.$api.getList({
					current
				}).then(res=>{
					console.log(res);
					this.tableData = res.data
					this.loading = false
				})
			},
			/* 搜索 */
			handlesearch(value){
				if(value == ''){
					this.$message({
						message: '警告哦,这是一条警告消息',
						type: 'warning'
					});
					this.getList()
					return
				}else {
					this.loading = true
					this.$api.getListByValue({
						value
					}).then(res=>{
						this.tableData = res.data.list
						this.total = res.data.total
						this.loading = false
					})
				}
			},
			getdatali(){
				this.getList(this.page.current)
			}
		}
	}
</script>

<style>
</style>

删除和分页 

<template>
  <div>
    <top-bar @handleSearch="handlesearch" @getdataList="getdatali"></top-bar>
    <el-table
      :data="tableData.slice((currpage - 1) * pagesize, currpage * pagesize)"
      
      v-loading="loading"
    >
      <el-table-column type="index" label="#" width="50"> </el-table-column>
      <el-table-column prop="date" label="日期" width="180"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="address" label="地址"> </el-table-column>
      <el-table-column prop="likes" label="爱好"> </el-table-column>
      <el-table-column label="操作" width="200">
        <template v-slot:default="scope">
          <div class="btns">
            <el-button type="primary" @click="btnDelete(scope.$index, scope.row)">
              <i class="el-icon-edit"></i>
            </el-button>
            <!-- 删除 -->
            <el-button
              type="danger"
              @click="btnDelete(scope.$index, scope.row)"
            >
              <i class="el-icon-delete"></i>
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currpage"
      :page-sizes="[2, 5,10]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import TopBar from "../components/main/TopBar.vue";
export default {
  name: "User",
  components: {
    TopBar,
  },
  data() {
    return {
      loading: false,
      total: 0,
      page: {
        size: 10,
        current: 1,
      },
      tableData: [],
      search: "",
      pagesize: 2,
      currpage: 1,
    };
  },
  mounted() {
    this.getList(this.page.current);
	// this.getTotal()
  },
  methods: {
    getList() {
      this.$api.getList().then((res) => {
        this.tableData = res.data;
      });
    },
    btnDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.getList(this.page.current);
          this.$api
            .deleteList({
              id: row.id,
            })
            .then((res) => {
              console.log(res);
            });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(val) {
      this.pagesize = val;
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currpage = val;
      console.log(`当前页: ${val}`);
    },
	// 编辑
    handleEdit(index, row) {
      console.log(index, row);
    },
	// 请求总页面数据
    getList(current) {
      this.loading = true;
      this.$api
        .getList({
          current,
        })
        .then((res) => {
          console.log(res);
          this.total = res.data.length;
          this.tableData = res.data;
          this.loading = false;
        });
    },
    /* 搜索 */
    handlesearch(value) {
		console.log(value);
      if (value == "") {
		 this.getList(this.page.current)
      }
       else {
        this.loading = true;
        this.$api
          .getListByValue({
            value,
          })
          .then((res) => {
            this.tableData = res.data.list;
            this.total = res.data.total;
            this.loading = false;
          });
      }
    },
    getdatali(value) {
		console.log(value);
      this.getList(this.page.current);
    },
	getTotal(){
		this.$api.getTotal().then(res=>{
			console.log(res);
			this.total = res.data
		})
	}
  },
};
</script>

<style>
</style>

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Southern Wind

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值