获取的数据得到更新

时间:2018-10-16 08:37:25

标签: angular angular5 angular6 angular-httpclient

我正在尝试根据不同用户的角色授予其权限。权限为获取,添加,删除,编辑等。例如,当我仅向用户授予特定权限时,如果我仅为user1授予添加和删除,则在创建下一个用户时,然后仅授予权限添加和删除 >可用,其他权限不可用。我该如何解决?我需要每个角色权限的新组ID。我尝试在下面进行编码,但似乎无法正常工作。

  

service.ts

add_permission(data){
var headers = new HttpHeaders().set('Authorization', 'Token ' + 
localStorage.getItem('usertoken'));

var options =  {
  headers: headers
};
return this.httpClient.post('/api/auth/permission_list/',data, 
options)
 }
  

component.ts

id :number;
dropdown_data:any = [];
checkedList = [];
permissionList:any = [];
perms:any = [];
permission_data:any = [];


onChange(email:string, checked: boolean) {
if(checked) {
this.perms.push(this.permission_data.permission_list)
} 
}

ngOnInit(): void{

//get permission 

  this.Authentication.get_permissions().subscribe(res =>{
    this.permissionList = res;
    console.log(res);

    for(let val of this.permissionList){ 
      this.dropdown_data = val.permission_list
      this.id = val.group_id
    }    
  })

}

//getting values of selected check box

onCheckboxChange(val, event) {
if(event.target.checked) {
this.checkedList.push(val);
console.log(this.checkedList);
} else {
for(var i=0 ; i < this.dropdown_data.length; i++) {
  if(this.checkedList[i] == val){
    this.checkedList.splice(i,1);
  }
}
}
}

// add permission

addPermission(){
let permissions = {};
permissions['group_id'] = this.id
permissions['permission_list']= this.checkedList;
console.log(permissions)

this.Authentication.add_permission(permissions).subscribe(res => {
  console.log('added')
},
err =>{
console.log('error');
})
} 
  

component.html

<div class="modal-body">
   <table class="table">
      <thead>
         <tr>
           <th scope="col">Permissions</th>
           <th scope="col">Select/Unselect</th>    
          </tr>
       </thead>
       <tbody *ngFor="let val of dropdown_data" >
          <tr>

            <td>{{val}}</td>
              <td><label class="container-1">
                <input type="checkbox"  value="{{val}}" (change)="onCheckboxChange(val,$event)" >
                <span class="checkmark"></span>
              </label></td>
              </tr>

            </tbody>
          </table>
          <button type="button"  class="btn btn-primary pull-right" (click)="addPermission()">Submit</button>
         </div>

0 个答案:

没有答案