Ninad Shinde

Ninad Shinde

  • NA
  • 13
  • 4.5k

User Permission - Handling multiple Checkboxes in single row

Jan 30 2019 2:06 AM
Hi guyz,
I am designing USER PERMISSION component in AngularJS 6 and ASP.NET MVC 5
I want to Design screen like this in AngularJS,
When i am using ngModel - then all checkboxes are getting selected
 
Please suggest solutions... Waiting for help desperately... 
 
Here is my code for UserRights.htm
  1. <div class="card">  
  2. <div class="card-header">  
  3. <h4 class="card-title">User Rights Header</h4>  
  4. </div>  
  5. <div class="card-body">  
  6. <button class="btn btn-default" (click)="AddNew()" >Add New</button>  
  7. <div style="overflow: hidden;" *ngIf="!entryMode" class="table-responsive">  
  8. <data-table name="user-rights-grid"  
  9. headerTitle="UserRightsHeader"  
  10. [items]="items"  
  11. [itemCount]="itemCount"  
  12. (reload)="reloadItems($event)"  
  13. >  
  14. <data-table-column>  
  15. <ng-template #dataTableHeader let-item="item">  
  16. <i><button class="btn btn-sm btn-danger" (click)="DeleteSelected()">Delete Selected</button></i>  
  17. </ng-template>  
  18. <ng-template #dataTableCell let-item="item">  
  19. <input type="checkbox" (change)="selected(item, $event)">  
  20. </ng-template>  
  21. </data-table-column>  
  22. <data-table-column  
  23. [property]="'UserRightsName'"  
  24. [header]="'Name'"  
  25. [sortable]="true"  
  26. [resizable]="true">  
  27. </data-table-column>  
  28. <data-table-column  
  29. [property]="'AuthenticationType'"  
  30. [header]="'Authentication'"  
  31. >  
  32. </data-table-column>  
  33. <data-table-column header="View">  
  34. <ng-template #dataTableHeader let-item="item">  
  35. <i>View</i>  
  36. </ng-template>  
  37. <ng-template #dataTableCell let-item="item">  
  38. <button (click)="ViewUser(item.UserRightsID)" class="btn btn-sm btn-primary">View</button>  
  39. </ng-template>  
  40. </data-table-column>  
  41. <data-table-column header="Detele">  
  42. <ng-template #dataTableHeader let-item="item">  
  43. <i>Delete</i>  
  44. </ng-template>  
  45. <ng-template #dataTableCell let-item="item">  
  46. <button (click)="Delete(item, true)" class="btn btn-sm btn-danger">Delete</button>  
  47. </ng-template>  
  48. </data-table-column>  
  49. <data-table-column header="EditRights" >  
  50. <ng-template #dataTableCell let-item="item" data-visible="true">  
  51. <button (click)="ViewRights(item.UserRightsID,item.UserRightsName)" class="btn btn-sm btn-info">Edit Rights</button>  
  52. </ng-template>  
  53. </data-table-column>  
  54. </data-table>  
  55. </div>  
  56. <!-- This form is for Rights Header -->  
  57. <div *ngIf="entryMode">  
  58. <div class="modal-header">  
  59. <h4 class="modal-title pull-left">Edit User Rights Header Information</h4>  
  60. <button type="button" class="close pull-right" aria-label="Close" (click)="closeEntry()">  
  61. <span aria-hidden="true">×</span>  
  62. </button>  
  63. </div>  
  64. <div class="modal-body">  
  65. <form name="EditUserRights" (change)="FormChanged()" role="form" (submit)="saveUser()">  
  66. <table class="editForm table-responsive" id="editForm">  
  67. <tr class="tr">  
  68. <td style="padding-left: 0px" colspan="2">  
  69. <table>  
  70. <tr class="tr">  
  71. <th class="editForm">  
  72. User Rights Name  
  73. </th>  
  74. <td style="padding-left: 10px">  
  75. <input type="text" name="rightsname" id="rightsname" class="form-control" [(ngModel)]="itemToEdit.UserRightsName" placeholder="User Rights Name" required="required">  
  76. </td>  
  77. <th style="padding-left: 8px" class="editForm">  
  78. Default Store  
  79. </th>  
  80. <td>  
  81. <input type="text" name="defaultstore" id="defaultstore" (keypress)="alphabetOnly($event)" autocomplete="off" class="form-control" [(ngModel)]="itemToEdit.DefaultStore" placeholder="Enter Default Store" required="required">  
  82. </td>  
  83. <th style="padding-left: 8px" class="editForm">  
  84. Default Sub Store  
  85. </th>  
  86. <td>  
  87. <input type="text" name="defaultsubstore" id="defaultsubstore" (keypress)="alphabetOnly($event)" autocomplete="off" class="form-control" [(ngModel)]="itemToEdit.DefaultSubStore" placeholder="Default Sub Store" required="required">  
  88. </td>  
  89. </tr>  
  90. <br>  
  91. <tr class="tr">  
  92. <th style="padding-left: 8px" class="editForm">  
  93. Default Receipt Book  
  94. </th>  
  95. <td>  
  96. <input type="text" name="defaultreceiptbook" id="defaultreceiptbook" autocomplete="off" class="form-control" [(ngModel)]="itemToEdit.DefaultReceiptBook" placeholder="Enter Receipt Book" required="required">  
  97. </td>  
  98. <th style="padding-left: 8px" class="editForm">  
  99. Default Cost Centre  
  100. </th>  
  101. <td>  
  102. <input type="text" name="defaultcostcentre" id="defaultcostcentre" autocomplete="off" class="form-control" [(ngModel)]="itemToEdit.DefaultCostCentre" placeholder="Enter Default Store" required="required">  
  103. </td>  
  104. <th>Active:</th>  
  105. <td style="float : left;">  
  106. <label><input name="options" ng-control="options" type="radio" [value]="true" [(ngModel)]="itemToEdit.Activate"> Yes</label><br/>  
  107. <label><input name="options" ng-control="options" type="radio" [value]="false" [(ngModel)]="itemToEdit.Activate" > No</label><br/>  
  108. </td>  
  109. </tr>  
  110. <tr class="tr">  
  111. <th>Authentication</th>  
  112. <td style="padding-left: 10px">  
  113. <div>  
  114. <input type="checkbox" id="Addition" name="Addition" [(ngModel)]="itemToEdit.Add"> Add<br>  
  115. <input type="checkbox" id="Modification" name="Modification" [(ngModel)]="itemToEdit.Modify"> Modify<br>  
  116. <input type="checkbox" id="Deletion" name="Deletion" [(ngModel)]="itemToEdit.Deactivate"> Deactivate<br>  
  117. <input type="checkbox" id="View" name="View" [(ngModel)]="itemToEdit.View"> View  
  118. </div>  
  119. </td>  
  120. </tr>  
  121. </table>  
  122. </td>  
  123. </tr>  
  124. <tr>  
  125. <td colspan="2">  
  126. <div class="row" *ngIf="showValidation">  
  127. <div class="small-12 columns error">  
  128. <div class="alert alert-danger" [innerHTML]="validationMessage"></div>  
  129. </div>  
  130. </div>  
  131. <div class="row">  
  132. <div class="small-12 columns error">  
  133. </div>  
  134. </div>  
  135. </td>  
  136. </tr>  
  137. <tr>  
  138. <td>  
  139. <button type="submit" [disabled] = "isInProgress" class="btn btn-primary block full-width m-b">Save</button>  
  140. </td>  
  141. <td>  
  142. <button type="button" class="btn btn-default block full-width m-b" (click)="closeEntry()">  
  143. Cancel  
  144. </button>  
  145. </td>  
  146. </tr>  
  147. </table>  
  148. </form>  
  149. </div>  
  150. </div>  
  151. <!-- This form is for Rights Details -->  
  152. <div *ngIf="entryMode1">  
  153. <div class="modal-header">  
  154. <h4 class="modal-title pull-left">Edit User Rights Details</h4>  
  155. <button type="button" class="close pull-right" aria-label="Close" (click)="closeEntry()">  
  156. <span aria-hidden="true">×</span>  
  157. </button>  
  158. </div>  
  159. <div class="modal-body">  
  160. <form name="EditUserRights" (change)="FormChanged()" role="form" (submit)="saveUser()">  
  161. <table class="editForm table-responsive" id="editForm">  
  162. <tr class="tr">  
  163. <td style="padding-left: 0px" colspan="2">  
  164. <table>  
  165. <tr class="tr">  
  166. <th class="editForm">  
  167. User Rights Name  
  168. </th>  
  169. <td style="padding-left: 10px">  
  170. <input type="text" name="rightsname" id="rightsname" class="form-control" [(ngModel)]="itemToEdit.UserRightsName" placeholder="User Rights Name" required="required">  
  171. </td>  
  172. <th style="padding-left: 8px" class="editForm">  
  173. Default Store  
  174. </th>  
  175. <td>  
  176. <input type="text" name="defaultstore" id="defaultstore" (keypress)="alphabetOnly($event)" autocomplete="off" class="form-control" [(ngModel)]="itemToEdit.DefaultStore" placeholder="Enter Default Store" required="required">  
  177. </td>  
  178. <th style="padding-left: 8px" class="editForm">  
  179. Default Sub Store  
  180. </th>  
  181. <td>  
  182. <input type="text" name="defaultsubstore" id="defaultsubstore" (keypress)="alphabetOnly($event)" autocomplete="off" class="form-control" [(ngModel)]="itemToEdit.DefaultSubStore" placeholder="Default Sub Store" required="required">  
  183. </td>  
  184. </tr>  
  185. <br>  
  186. <tr class="tr">  
  187. <th>Active:</th>  
  188. <td style="float : left;">  
  189. <label><input name="options" ng-control="options" type="radio" [value]="true" [(ngModel)]="itemToEdit.Activate"> Yes</label><br/>  
  190. <label><input name="options" ng-control="options" type="radio" [value]="false" [(ngModel)]="itemToEdit.Activate" > No</label><br/>  
  191. </td>  
  192. </tr>  
  193. <tr class="tr">  
  194. <th>Authentication</th>  
  195. <td style="padding-left: 10px">  
  196. <div>  
  197. <input type="checkbox" id="Addition" name="Addition" [(ngModel)]="itemToEdit.Add"> Add<br>  
  198. <input type="checkbox" id="Modification" name="Modification" [(ngModel)]="itemToEdit.Modify"> Modify<br>  
  199. <input type="checkbox" id="Deletion" name="Deletion" [(ngModel)]="itemToEdit.Deactivate"> Deactivate<br>  
  200. <input type="checkbox" id="View" name="View" [(ngModel)]="itemToEdit.View"> View  
  201. </div>  
  202. </td>  
  203. </tr>  
  204. </table>  
  205. </td>  
  206. </tr>  
  207. <tr>  
  208. <td colspan="2">  
  209. <div class="row" *ngIf="showValidation">  
  210. <div class="small-12 columns error">  
  211. <div class="alert alert-danger" [innerHTML]="validationMessage"></div>  
  212. </div>  
  213. </div>  
  214. <div class="row">  
  215. <div class="small-12 columns error">  
  216. </div>  
  217. </div>  
  218. </td>  
  219. </tr>  
  220. <tr>  
  221. <td>  
  222. <button type="submit" [disabled] = "isInProgress" class="btn btn-primary block full-width m-b">Save</button>  
  223. </td>  
  224. <td>  
  225. <button type="button" class="btn btn-default block full-width m-b" (click)="closeEntry()">  
  226. Cancel  
  227. </button>  
  228. </td>  
  229. </tr>  
  230. </table>  
  231. </form>  
  232. </div>  
  233. </div>  
  234. </div>  
  235. </div> 
userrights.component.ts
  1. import { Component, OnInit } from '@angular/core';
  2. import { DataTableResource } from 'angular5-data-table';
  3. import { ValidateService } from 'services/validate.service';
  4. import { MiscService } from 'services/misc.service';
  5. import { UserHeaderService } from 'services/user-header.service';
  6. import { CommonService } from 'services/common.service';
  7. import { Globals } from 'Global/globals';
  8. import { CustomizeService } from 'services/customize.service';
  9. import { RightsHeaderService } from 'services/rights-header.service';
  10. import { NgxPaginationModule } from 'ngx-pagination';
  11. import { from } from 'rxjs';
  12. import { FORMERR } from 'dns';
  13. declare var $: any;

  14. @Component({
  15. selector: 'app-rights-header',
  16. templateUrl: './rights-header.component.html',
  17. styleUrls: ['./rights-header.component.scss']
  18. })
  19. export class RightsHeaderComponent implements OnInit {
  20. today: string;
  21. itemResource = new DataTableResource([]);
  22. itemResource1 = new DataTableResource([]);
  23. items = [];
  24. items1 = [];
  25. itemCount = 0;
  26. entryMode = false;
  27. entryMode1 = false;
  28. validationMessage = '';
  29. showValidation = false;
  30. isInProgress = false;
  31. deleting = false;
  32. itemToEdit =
  33. {
  34. "UserRightsID":0,
  35. "UserRightsName": "",
  36. "AuthenticationType": "",
  37. "Add":false,
  38. "Modify":false,
  39. "Deactivate":false,
  40. "View":false,
  41. "DefaultStore":"",
  42. "DefaultSubStore":"",
  43. "DefaultReceiptBook":"",
  44. "DefaultCostCentre":"",
  45. "Activate": true
  46. };

  47. itemToEditRights = {
  48. "UserRightsID":0,
  49. "UserRightsName": "",
  50. "AuthenticationType": "",
  51. "Add":false,
  52. "Modify":false,
  53. "Deactivate":false,
  54. "View":false,
  55. "DefaultStore":"",
  56. "DefaultSubStore":"",
  57. "Activate": true
  58. };
  59. selectedRows = [];
  60. messages: any;
  61. showSuccess: boolean;
  62. authentication: string[];
  63. client: string[];
  64. location: string[];
  65. employee: string[];
  66. doctor: string[];
  67. id : string;
  68. name : string;

  69. constructor(private validateService : ValidateService,
  70. private miscService: MiscService,
  71. private rightsService: RightsHeaderService,
  72. private commonService: CommonService,
  73. globals: Globals,
  74. private customizeService: CustomizeService
  75. ) {
  76. this.loadMessages();
  77. this.loadUser();
  78. //this.loadDetails();
  79. }

  80. loadMessages(){
  81. this.customizeService.getMessages().subscribe(data => {
  82. this.messages = data;
  83. });
  84. }

  85. loadUser() {
  86. this.rightsService.getUser().subscribe(data => {
  87. this.items = data;
  88. this.itemResource = new DataTableResource(data);
  89. this.itemResource.count().then(count => this.itemCount = count);

  90. setTimeout(function(){
  91. this.reloadItems({});
  92. }.bind(this), 100);
  93. });
  94. }

  95. loadDetails() {
  96. this.rightsService.getDetails().subscribe(data => {
  97. this.items1 = data;
  98. this.itemResource1 = new DataTableResource(data);
  99. this.itemResource1.count().then(count => this.itemCount = count);

  100. setTimeout(function(){
  101. this.reloadItems({});
  102. }.bind(this), 100);
  103. });
  104. }



  105. //Saving User data
  106. saveUser(){
  107. this.isInProgress = true;
  108. this.rightsService.saveUser(this.itemToEdit).subscribe(data => {
  109. this.loadUser();
  110. //this.loadDetails();
  111. alert(this.messages['msg_Success']);
  112. this.entryMode = false;
  113. this.isInProgress = false;
  114. });
  115. }

  116. //getUsers() get called on click of edit rights button
  117. getUser(id)
  118. {
  119. debugger;

  120. }

  121. // validates the form when user enter some values.
  122. FormChanged(){
  123. if(this.validationMessage != ''){
  124. var inValidateResult = this.validateService.validate(this.itemToEdit);
  125. if(inValidateResult){
  126. this.validationMessage = inValidateResult;
  127. this.showValidation = true;
  128. this.isInProgress = true;
  129. }
  130. else{
  131. this.validationMessage = '';
  132. this.showValidation = false;
  133. this.isInProgress = false;
  134. }
  135. }
  136. }

  137. // used to open new form.
  138. AddNew(){
  139. this.itemToEdit =
  140. {
  141. "UserRightsID":0,
  142. "UserRightsName": "",
  143. "AuthenticationType": "",
  144. "Add":false,
  145. "Modify":false,
  146. "Deactivate":false,
  147. "View":false,
  148. "DefaultStore":"",
  149. "DefaultSubStore":"",
  150. "DefaultReceiptBook":"",
  151. "DefaultCostCentre":"",
  152. "Activate": true
  153. };
  154. this.entryMode = true;
  155. }

  156. ViewRights(id,name)
  157. {
  158. this.itemToEditRights =
  159. {
  160. "UserRightsID":id,
  161. "UserRightsName": name,
  162. "AuthenticationType": "",
  163. "Add":false,
  164. "Modify":false,
  165. "Deactivate":false,
  166. "View":false,
  167. "DefaultStore":"",
  168. "DefaultSubStore":"",
  169. "Activate": true
  170. };
  171. this.entryMode = true;
  172. }

  173. // used to get customer w.r.t id.
  174. ViewUser(UserRightsID) {
  175. debugger;
  176. if(this.deleting){
  177. return;
  178. }
  179. this.rightsService.getUserById(UserRightsID).subscribe(data =>{
  180. // var datePipe = new DatePipe('en-US');

  181. // data.DOB = datePipe.transform(data.DOB, 'yyyy-MM-dd');
  182. this.itemToEdit = data;
  183. });
  184. this.entryMode = true;
  185. }

  186. // Deletes customer.
  187. Delete(item, showSuccess = false){
  188. this.deleting = true;
  189. this.showSuccess = showSuccess;
  190. this.DeleteById(item.UserRightsID)
  191. }

  192. // Deletes customer on the basis of id.
  193. DeleteById(id){
  194. this.rightsService.DeleteUser(id).subscribe(data => {
  195. if(this.showSuccess){
  196. this.showSuccess = false;
  197. alert(this.messages['msg_Delete']);
  198. this.loadUser();
  199. }
  200. this.deleting = false;
  201. });
  202. }

  203. // used to close entry mode.
  204. closeEntry(){
  205. this.AddNew();
  206. this.ViewRights(this.id,this.name);
  207. this.entryMode = false;
  208. }

  209. //Selected Users
  210. selected(item, params){
  211. debugger;
  212. const myArr: any = [item.length][5];
  213. if(params.checked)
  214. {

  215. this.selectedRows.push(item.UserRightsID + params.name);

  216. var arr = this.selectedRows;
  217. }
  218. else{
  219. this.selectedRows = this.selectedRows.filter(ele => { return ele != item.UserRightsID });
  220. }
  221. }

  222. // this will be used to delete multiple customers.
  223. DeleteSelected(){
  224. if(this.selectedRows.length == 0){
  225. alert(this.messages['msg_DeleteSelection']);
  226. return;
  227. }
  228. var con = confirm(this.messages['cnfm_Delete']);
  229. if(con){
  230. for(var i = 0; i < this.selectedRows.length; i++){
  231. this.showSuccess = i == this.selectedRows.length - 1;
  232. this.DeleteById(this.selectedRows[i]);
  233. }
  234. }
  235. }

  236. // This method would be used to get latest set of records when added, updated or deleted.
  237. reloadItems(params) {
  238. this.itemResource.query(params).then(items => this.items = items);
  239. }

  240. ngOnInit() {
  241. // Getting Client from Common Service.
  242. this.commonService.getClientList(this.client).subscribe(data =>{
  243. debugger;
  244. this.client = data as string [];
  245. this.itemToEdit = data;
  246. });

  247. // Getting Location from Service.
  248. this.commonService.getLocationList(this.location).subscribe(data =>{
  249. debugger;
  250. this.location = data as string [];
  251. this.itemToEdit = data;
  252. });

  253. // Getting Authentication type from Misc.
  254. this.miscService.getAuthenticationType().subscribe(data =>{
  255. this.authentication = data as string [];
  256. this.itemToEdit = data;
  257. });

  258. //Getting Employee from Common
  259. this.commonService.getEmploeeList(this.employee).subscribe(data =>{
  260. this.employee = data as string[];
  261. this.itemToEdit = data;
  262. });


  263. }

  264. //Allow to enter only Number
  265. numberOnly(event) : boolean {
  266. const charCode = (event.which) ? event.which : event.keyCode;
  267. if(charCode < 48 || charCode > 57)
  268. {
  269. return false;
  270. }
  271. return true;
  272. }

  273. //Allow to enter only Aplhabets
  274. alphabetOnly(event) : boolean {
  275. const charCode = (event.which) ? event.which : event.keyCode;
  276. if((charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122) && ( charCode != 32))
  277. {
  278. return false;
  279. }
  280. return true;
  281. }
  282. }
Userrights.service.ts
  1. import { Injectable } from '@angular/core';
  2. import {Router} from '@angular/router';
  3. import { Http, Headers } from '@angular/http';
  4. import { catchError, map } from 'rxjs/operators';
  5. import { environment } from '../environments/environment';

  6. @Injectable()
  7. export class RightsHeaderService {

  8. isSessionVaild = true;
  9. constructor(private http: Http, public router: Router) { this.isSessionVaild = true; }

  10. // Loads all User
  11. getUser(){
  12. debugger;
  13. let headers = new Headers();
  14. headers.append('Content-Type', 'application/json');
  15. headers.append('Access-Control-Allow-Origin', '*');
  16. headers.append('Access-Control-Allow-Methods', 'GET, POST, PUT');
  17. return this.http.get(environment.apiUrl + 'Rights', { headers: headers })
  18. .pipe(map(data => data.json()),
  19. catchError((error: any) => {
  20. throw error;
  21. }));
  22. }

  23. redirectToRights(url)
  24. {
  25. debugger;
  26. var myurl = `${url}`;
  27. this.router.navigateByUrl(myurl).then(e => {
  28. if (e) {
  29. console.log("Navigation is successful!");
  30. } else {
  31. console.log("Navigation has failed!");
  32. }
  33. });
  34. }

  35. // Loads all Details of Component and Authentication
  36. getDetails(){
  37. debugger;
  38. let headers = new Headers();
  39. headers.append('Content-Type', 'application/json');
  40. headers.append('Access-Control-Allow-Origin', '*');
  41. headers.append('Access-Control-Allow-Methods', 'GET, POST, PUT');
  42. return this.http.get(environment.apiUrl + 'Demo', { headers: headers })
  43. .pipe(map(data => data.json()),
  44. catchError((error: any) => {
  45. throw error;
  46. }));
  47. }

  48. // Loads User on the basis of id.

  49. getUserById(id){
  50. debugger;
  51. let headers = new Headers();
  52. headers.append('Content-Type', 'application/json');
  53. headers.append('Access-Control-Allow-Origin', '*');
  54. headers.append('Access-Control-Allow-Methods', 'GET, POST, PUT');
  55. return this.http.get(environment.apiUrl + 'Rights/'+id, { headers: headers })
  56. .pipe(map(data => data.json()),
  57. catchError((error: any) => {
  58. throw error;
  59. }));
  60. }

  61. // Saves/updates User.
  62. saveUser(Rights){
  63. debugger;
  64. let headers = new Headers();
  65. headers.append('Content-Type', 'application/json');
  66. headers.append('Access-Control-Allow-Origin', '*');
  67. headers.append('Access-Control-Allow-Methods', 'GET, POST, PUT');
  68. return this.http.post(environment.apiUrl + 'Rights', Rights, { headers: headers })
  69. .pipe(map(data => data.json()),
  70. catchError((error: any) => {
  71. throw error;
  72. }));
  73. }

  74. // Deletes Doctor on the basis of id.
  75. DeleteUser(id){
  76. debugger;
  77. let headers = new Headers();
  78. headers.append('Content-Type', 'application/json');
  79. headers.append('Access-Control-Allow-Origin', '*');
  80. headers.append('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
  81. return this.http.delete(environment.apiUrl + 'Rights/'+id, { headers: headers })
  82. .pipe(map(data => data.json()),
  83. catchError((error: any) => {
  84. throw error;
  85. }));
  86. }
  87. }

Answers (1)