vue实现的一个撤销和恢复功能

8 篇文章 0 订阅

1、有三个表格
2、添加不同得数据
3、在点击撤销时删除最后一条添加的数据,在点击恢复时复原删除的数据

<template>
    <div style="width:1000px;margin:0 auto">
        <Button type="info" @click="handleOk">添加</Button>
        <Table :columns="columns" :data="datas"></Table>
        <Button type="info" @click="handleOkOne">添加</Button>
        <Table :columns="columnsOne" :data="datasOne"></Table>
        <Button type="info" @click="handleOkTwo">添加</Button>
        <Table :columns="columnsTwo" :data="datasTwo"></Table>
        <Button @click="handleRevoke">撤销</Button>
        <Button type="primary" @click="handleRecovery">恢复</Button>
    </div>
</template>

顺序存储:用js内置对象Array实现 定义一个栈

function ArrayStack () {
    let arr = [];
    // 压入栈中  
    this.push = function (element) {
        arr.push(element);
    }
    // 弹出栈顶元素  
    this.pop = function () {
        return arr.pop();
    }
    // 查看栈顶元素  
    this.top = function () {
        return arr[arr.length - 1];
    }
    // 获取栈长  
    this.size = function () {
        return arr.length;
    }
    // 清空栈  
    this.clear = function () {
        arr = [];
        return true;
    }

    this.toString = function () {
        return arr.toString();
    }
}
export default {
    data () {
        return {
            columns: [
                { type: 'index', width: 60, align: 'center' },
                { title: 'Name', key: 'name' },
                { title: 'Age', key: 'age' },
                { title: 'Address', key: 'address' }
            ],
            datas: [],
            columnsOne: [
                { type: 'index', width: 60, align: 'center' },
                { title: 'Name', key: 'name' },
                { title: 'Age', key: 'age' },
                { title: 'Address', key: 'address' }
            ],
            datasOne: [],
            columnsTwo: [
                { type: 'index', width: 60, align: 'center' },
                { title: 'Name', key: 'name' },
                { title: 'Age', key: 'age' },
                { title: 'Address', key: 'address' }
            ],
            datasTwo: [],
            stackRevoke: new ArrayStack(), //撤销栈
            stackRecovery: new ArrayStack() //恢复栈
        }
    },
    methods: {
        // 添加时向恢复栈中加数据
        handleOk () {
            let obj = { id: 'text001', name: '北京市', age: 18, address: '北京', code: '1001' }
            this.datas.push(obj)
            this.stackRecovery.push(obj)
        },
        handleOkOne () {
            let obj = { id: 'text005', name: '石家庄市', age: 18, address: '河北省', code: '1002' }
            this.datasOne.push(obj)
            this.stackRecovery.push(obj)
        },
        handleOkTwo () {
            let obj = { id: 'text009', name: '杭州市', age: 18, address: '浙江省', code: '1003' }
            this.datasTwo.push(obj)
            this.stackRecovery.push(obj)
        },
        // 撤销(删除)
        handleRevoke () {
            let p = this.stackRecovery.pop() //读取恢复栈顶数据

            if (p !== undefined) {
                switch (p.code) {
                    case '1001': {
                        if (this.datas.length > 0) {
                            let p = this.datas.pop()
                            this.stackRevoke.push(p) //向删除栈中增加数据
                        }
                    }
                        break;
                    case '1002': {
                        if (this.datasOne.length > 0) {
                            let p = this.datasOne.pop()
                            this.stackRevoke.push(p)
                        }
                    }
                        break;
                    case '1003': {
                        if (this.datasTwo.length > 0) {
                            let p = this.datasTwo.pop()
                            this.stackRevoke.push(p)
                        }
                    }
                        break;
                }
            }
        },
        // 恢复(复原)
        handleRecovery () {
            let p = this.stackRevoke.pop() //读取撤销栈中得第一条数据

            if (p !== undefined) { //判断取出对象是否存在
                switch (p.code) { //判断是哪一个数组下得数据
                    case '1001':
                        this.stackRecovery.push(p) //向恢复栈中添加数据
                        this.datas.push(p) //向指定数组添加数据
                        break;

                    case '1002':
                        this.stackRecovery.push(p)
                        this.datasOne.push(p)
                        break;

                    case '1003':
                        this.stackRecovery.push(p)
                        this.datasTwo.push(p)
                        break;
                }
            }
        }
    }
}
</script>
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值