Skip to content
select-role.component.ts 2.64 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {SystemService} from '../../system/system.service';
wangqinghua's avatar
wangqinghua committed

@Component({
wangqinghua's avatar
wangqinghua committed
    selector: 'smart-select-role',
    templateUrl: './select-role.component.html',
    styles: []
wangqinghua's avatar
wangqinghua committed
})
export class SelectRoleComponent implements OnInit {
wangqinghua's avatar
wangqinghua committed
    @Output() done = new EventEmitter<any>();
wangqinghua's avatar
wangqinghua committed

wangqinghua's avatar
wangqinghua committed
    dataSet: any = [];
    title;
    selectList = [];
    isVisible = false;
    allChecked = false;
    checkedNumber = 0;
    disabledButton = true;
    indeterminate = false;
wangqinghua's avatar
wangqinghua committed
    userId;
    userRoleList = [];
wangqinghua's avatar
wangqinghua committed
    displayData: Array<{ key1: string; key2: number; key3: string; key4: string; checked: boolean }> = [];
    constructor(private systemSer: SystemService) {
    }
wangqinghua's avatar
wangqinghua committed

wangqinghua's avatar
wangqinghua committed
    checkAll(value: boolean): void {
        this.displayData.forEach(data => data.checked = value);
        this.refreshStatus();
    }
wangqinghua's avatar
wangqinghua committed

wangqinghua's avatar
wangqinghua committed
    currentPageDataChange($event: Array<{ key1: string; key2: number; key3: string; key4: string; checked: boolean }>): void {
        this.displayData = $event;
    }
wangqinghua's avatar
wangqinghua committed

wangqinghua's avatar
wangqinghua committed
    selectItem(event,item){
        if(event){
            this.selectList.push(item);
        }else{
            this.selectList.forEach((value,index)=>{
                if(value.id == item.id){
                    this.selectList.splice(index,1)
                }
            })
        }
        this.refreshStatus()
    }
wangqinghua's avatar
wangqinghua committed

wangqinghua's avatar
wangqinghua committed
    refreshStatus(): void {
        const allChecked = this.displayData.every(value => value.checked === true);
        const allUnChecked = this.displayData.every(value => !value.checked);
        this.allChecked = allChecked;
        this.indeterminate = (!allChecked) && (!allUnChecked);
        this.disabledButton = !this.dataSet.some(value => value.checked);
        this.checkedNumber = this.dataSet.filter(value => value.checked).length;
    }

    ngOnInit() {
    }

wangqinghua's avatar
wangqinghua committed
    showModal(title,id) {
wangqinghua's avatar
wangqinghua committed
        this.title = title;
        this.isVisible = true;
wangqinghua's avatar
wangqinghua committed
        this.userId = id;
        const data = {
            userId:this.userId
        }
        this.systemSer.getRoleByUserId(data).subscribe(
            (res)=>{
                this.userRoleList = res.data;
                this.getRole();
            }
        )
wangqinghua's avatar
wangqinghua committed
    }

    getRole() {
        this.systemSer.role().subscribe(
            (res) => {
                this.dataSet = res.data;
wangqinghua's avatar
wangqinghua committed
                this.dataSet.forEach(e=>{
                    if(this.userRoleList.indexOf(e.id) > -1){
                        e.checked = true;
                    }
                })
wangqinghua's avatar
wangqinghua committed
            }
        );
    }

    handleEditCancel(){
        this.isVisible = false;
    }

    handEditleOk(){
        this.done.emit(this.selectList);
        this.isVisible = false;
    }
wangqinghua's avatar
wangqinghua committed

}