В настоящее время я работаю над веб-порталом с Angular-7 в качестве внешнего интерфейса и Laravel-5.8 в качестве внутреннего. Также я использую Laravel Spatie для управления ролями пользователей.
user.component.ts
export class UsersComponent implements OnInit {
users = null; //Store Users Data
roles = null; //Store all roles Data
clients = null;
public error = {
'role' : null,
'email' : null,
'client_id' : null,
'first_name' : null,
'last_name' : null,
'password' : null
};
//Form errors
keyword = null; //Current Search Keyword
pagination = { //Current Pagination data
'page' : '1',
'max' : '10'
}
role = null;
User = 'User';
data = { //User Update Data
"id" : null,
"first_name" : null,
"last_name" : null,
"client_id" : null,
"role" : []
}
form = { //New User add Data
'first_name' : null,
'last_name' : null,
'email' : null,
'client_id' : null,
'password' : null,
'password_confirmation' : null,
'role' : []
}
headers = { //Token for API Authorization
'Authorization' : this.token.get(),
'X-Requested-With' : 'XMLHttpRequest'
}
sortData = { //Current Sort Data
"col" : null,
"order" : null
}
isSuperAdmin = false;
constructor(private roleManage : RolesCheckService , private route : ActivatedRoute, private pg: NgbPaginationConfig, private token : TokenService, private http : HttpClient, private router : Router,private api : ApiService, private notify : SnotifyService) {
pg.boundaryLinks = true;
pg.rotate = true;
}
ngOnInit() {
console.log('isSuperAdmin: ' + this.roleManage.isSuperAdmin);
this.isSuperAdmin = this.roleManage.isSuperAdmin;
this.route.queryParams.subscribe(params => {
if(params['role']){
this.role = params['role'];
this.User = this.role;
} else {
this.User = 'User';
this.role = '';
}
})
}
checkboxAdd(event){
if(event.srcElement.checked){
this.form.role.push(event.srcElement.name);
} else {
var index =this.form.role.indexOf(event.srcElement.name);
this.form.role.splice(index, index+1);
}
console.log(this.form.role);
}
}
user.component.html
<div id="addModal" class="modal" style="background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.4);">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add New {{ User }}</h5>
<button type="button" class="close" (click)='closeAddModal()'>
<span aria-hidden="true">×</span>
</button>
</div>
<form #editUserForm=ngForm>
<div class="form-group">
<label for="name">First Name</label>
<input type="name" name="first_name" id="inputName" class="form-control" placeholder="First Name" [(ngModel)]="form.first_name" required>
</div>
<div class="form-group">
<label for="name">Last Name</label>
<input type="name" name="last_name" id="inputName" class="form-control" placeholder="Last Name" [(ngModel)]="form.last_name" required>
</div>
<div class="form-group">
<label for="name">Email</label>
<input type="email" name="email" id="inputEmail" class="form-control" placeholder="example@email.com" required [(ngModel)]="form.email">
</div>
<div class="form-group">
<label for="name">Role</label>
<div *ngFor="let role of roles">
<input type="checkbox" name="{{ role.name }}" value="{{ role.name }}" (change)="checkboxAdd($event)"> {{ role.name }}
</div>
</div>
<div class="form-group">
<label for="name">Password</label>
<input type="password" name="password" id="inputPassword" [(ngModel)]="form.password" class="form-control" placeholder="Password" required>
</div>
<div class="form-group">
<label for="name">Password Confirmation</label>
<input type="password" name="password_confirmation" id="inputPasswordConfirm" [(ngModel)]="form.password_confirmation" class="form-control" placeholder="Re enter Password" required>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" (click)='addModalSubmit()' [disabled]="!editUserForm.valid">Save changes</button>
<button type="button" class="btn btn-secondary" (click)='closeAddModal()'>Close</button>
</div>
</div>
</div>
</div>
В настоящее время я использую
checkboxAdd (событие)
, чтобы выбрать роль пользователя по одному. Но я хочу иметь возможность выбрать / снять выделение сразу, как показано ниже:
Как мне этого добиться?