Skip to content
mine.component.ts 2.65 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import {Component, OnInit} from '@angular/core';
import {SystemService} from '../system/system.service';
import {LocalStorageService} from 'ngx-webstorage';
import {NzMessageService} from 'ng-zorro-antd';
wangqinghua's avatar
wangqinghua committed
import {UtilService} from '../shared/common/util.service';
wangqinghua's avatar
wangqinghua committed

@Component({
    selector: 'smart-mine',
    templateUrl: './mine.component.html',
    styles: []
})
export class MineComponent implements OnInit {

wangqinghua's avatar
wangqinghua committed
    editCache = [];
wangqinghua's avatar
wangqinghua committed
    dataSet = [];

    constructor(private systemSer: SystemService, private localStorage: LocalStorageService,
wangqinghua's avatar
wangqinghua committed
                private message: NzMessageService, private util: UtilService) {
wangqinghua's avatar
wangqinghua committed
    }

    ngOnInit() {
        this.getList();
    }

    getList() {
        this.systemSer.getMenuBySeparation().subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.dataSet = res.data;
                    this.updateEditCache(null);
                }
            }
        );
    }

    updateEditCache(num: number): void {
        for (let i = 0; i < this.dataSet.length; i++) {
            this.dataSet[i].key = i.toString();
        }
        this.dataSet.forEach((item, index) => {
            item.key = index;
            if (!this.editCache[item.key]) {
                this.editCache[item.key] = {
                    edit: false,
                    data: item
                };
            }
            if (item.key == num) {
                this.editCache[item.key].edit = true;
            }
        });
    }

    add() {
wangqinghua's avatar
wangqinghua committed
        this.dataSet.forEach((e, index) => e.key = index + 1);
wangqinghua's avatar
wangqinghua committed
        const item = {
            name: '',
            parentId: '',
            code: '',
            url: '',
            imageUrl: '',
            showOrder: '',
wangqinghua's avatar
wangqinghua committed
            key: 0
wangqinghua's avatar
wangqinghua committed
        };
wangqinghua's avatar
wangqinghua committed
        this.dataSet = [item, ...this.dataSet];
        this.editCache = [{data: item, edit: true}, ...this.editCache];
wangqinghua's avatar
wangqinghua committed
    }

    startEdit(key: string): void {
        this.editCache[key].edit = true;
    }

    cancelEdit(key: string): void {
        this.editCache[key].edit = false;
    }

    saveEdit(key: string): void {
        const index = this.dataSet.findIndex(item => item.key === key);
        this.dataSet[index] = this.editCache[key].data;
        this.editCache[key].edit = false;
wangqinghua's avatar
wangqinghua committed
        console.log(this.dataSet[index]);
wangqinghua's avatar
wangqinghua committed
        this.systemSer.createMenu(this.dataSet[index]).subscribe(
            (res) => {
                this.message.success('保存成功');
            }
        );
    }

    deleteItem(id: string) {
        this.systemSer.deleteMenu(id).subscribe(
            (res) => {
                this.message.success('删除成功');
                this.getList();
            }
        );
    }

}