如何根据单选按钮单击[活动/非活动/全部]获取雇员列表

时间:2019-09-13 06:26:34

标签: node.js asp.net-web-api angular-material angular7

我在仪表板上显示了一个包括(活动,非活动雇员)的雇员列表。因此,现在我想默认显示仅活动员工的列表。如果单击“非活动”单选按钮,则它应仅显示“非活动”列表。全部表示员工的活动非活动的所有列表。

HTML页面


  <div class="col-xs-12 col-sm-12 col-md-12 col-lg-4 mb-4 col-full" widget>
    <div class="silk-card border-success pb-0">
      <div class="silk-card-title d-flex justify-content-between">
        <h4><i class="fa fa-users fa-lg mx-1"></i>Employees List</h4>
        <mat-radio-group [(ngModel)]="isActive" (ngModelChange)="getUsersDataOfType($event)">
          <mat-radio-button [value]="1"><b>Active</b></mat-radio-button>
          <mat-radio-button [value]="0"><b>Inactive</b></mat-radio-button>
          <mat-radio-button [value]="2"><b>All</b></mat-radio-button>
        </mat-radio-group>
      </div>
      <div class="silk-card-content widget-body">
        <div class="table-responsive">
          <div class="mat-elevation-z8">
            <mat-table [dataSource]="listData" matSort *ngIf="isActive">

              <ng-container matColumnDef="employeeId">
                <mat-header-cell [ngClass]="'customWidthClassSmall'" *matHeaderCellDef mat-sort-header>Employee ID
                </mat-header-cell>
                <mat-cell [ngClass]="'customWidthClassSmall'" *matCellDef="let element">
                  <b>{{element.actualEmployeeCode}}</b></mat-cell>
              </ng-container>

              <ng-container matColumnDef="firstname">
                <mat-header-cell [ngClass]="'customWidthClassMedium'" *matHeaderCellDef mat-sort-header>Name
                </mat-header-cell>
                <mat-cell [ngClass]="'customWidthClassMedium'" *matCellDef="let element">
                  <b>{{element.firstname | titlecase }} {{element.lastname | titlecase }}</b></mat-cell>
              </ng-container>
              <!-- <ng-container matColumnDef="lastname">
                            <mat-header-cell *matHeaderCellDef mat-sort-header>Last Name</mat-header-cell>
                            <mat-cell style="text-align: center;" *matCellDef="let element">{{element.lastname}}</mat-cell>
                          </ng-container> -->
              <ng-container matColumnDef="emailaddress">
                <mat-header-cell [ngClass]="'customWidthClassLarge'" *matHeaderCellDef mat-sort-header>Email
                </mat-header-cell>
                <mat-cell [ngClass]="'customWidthClassLarge'" *matCellDef="let element">{{element.emailaddress}}
                </mat-cell>
              </ng-container>
              <!-- <ng-container matColumnDef="deptname">
                            <mat-header-cell *matHeaderCellDef mat-sort-header>Department</mat-header-cell>
                            <mat-cell *matCellDef="let element">{{element.deptname}}</mat-cell>
                          </ng-container> -->
              <ng-container matColumnDef="rolename">
                <mat-header-cell [ngClass]="'customWidthClassSmall'" *matHeaderCellDef mat-sort-header>Role
                </mat-header-cell>
                <mat-cell [ngClass]="'customWidthClassSmall'" *matCellDef="let element">{{element.rolename}}</mat-cell>
              </ng-container>

              <ng-container matColumnDef="status">
                <mat-header-cell [ngClass]="'customWidthClassSmall'" *matHeaderCellDef mat-sort-header>Status
                </mat-header-cell>
                <mat-cell [ngClass]="'customWidthClassSmall'" *matCellDef="let element">
                  <span *ngIf="element.isactive == 1" style="color: green; font-weight: bold;">Active</span>
                  <span *ngIf="element.isactive == 0" style="color: #E83F6F; font-weight: bold;">Inactive</span>
                </mat-cell>
              </ng-container>

              <!-- <ng-container matColumnDef="length_service">
                  <mat-header-cell [ngClass]="'customWidthClassSmall'" *matHeaderCellDef mat-sort-header>Length of Service</mat-header-cell>
                  <mat-cell [ngClass]="'customWidthClassSmall'" *matCellDef="let element">{{ element.length_service }}</mat-cell>
                </ng-container> -->

              <ng-container matColumnDef="actions">
                <mat-header-cell [ngClass]="'customWidthClassSmall'" *matHeaderCellDef>Actions</mat-header-cell>
                <mat-cell [ngClass]="'customWidthClassSmall'" *matCellDef="let row">
                  <button mat-icon-button>
                    <a [routerLink]="['/hr/editemployee',row.user_id]">
                      <mat-icon>mode_edit</mat-icon>
                    </a>
                  </button>
                  <!-- <button mat-icon-button color="warn" (click)="open(row)">
                              <a [routerLink]="['/hr/editemployee',row.user_id]"><mat-icon>delete_outline</mat-icon></a>  
                              </button> -->
                </mat-cell>
              </ng-container>
              <ng-container matColumnDef="loading">
                <mat-footer-cell *matFooterCellDef colspan="4">
                  Loading data...
                </mat-footer-cell>
              </ng-container>
              <ng-container matColumnDef="noData">
                <mat-footer-cell *matFooterCellDef colspan="4">
                  No data.
                </mat-footer-cell>
              </ng-container>
              <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
              <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
              <mat-footer-row *matFooterRowDef="['loading']" [hidden]="(listData!=null)"></mat-footer-row>
              <mat-footer-row *matFooterRowDef="['noData']" [hidden]="!(listData!=null && listData.data.length==0)">
              </mat-footer-row>
            </mat-table>
            <mat-paginator [pageSizeOptions]="[1,5, 10, 25, 100]" [pageSize]="10" showFirstLastButtons></mat-paginator>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>```





#TS file#

    getUsersDataOfType(value) {
    this.ser.Getmethod('api/Employee1/GetEmploymees').subscribe(data => {
      this.data = data;
      if (value == 1) {
        console.log('Value : ' + value);
        $(document).ready(function () {
          console.log('JQUERY');
          this.listData = new MatTableDataSource(this.data);
          this.listData.sort = this.sort;
          this.listData.paginator = this.paginator;
        });
      } else if (value == 0) {
        console.log('Value : ' + value);
        $(document).ready(function () {
          console.log('JQUERY');
          this.listData = new MatTableDataSource(this.data);
          this.listData.sort = this.sort;
          this.listData.paginator = this.paginator;
        });
      } else if (value == 2) {
        console.log('Value : ' + value);
        $(document).ready(function () {
          console.log('JQUERY');
          this.listData = new MatTableDataSource(this.data);
          this.listData.sort = this.sort;
          this.listData.paginator = this.paginator;
        });
      }
    },
      error => {
        this.errormethod(error);
      });
  }


  [![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/8nxD5.jpg

1 个答案:

答案 0 :(得分:0)

您不需要调用Api均匀时间,因为用户要求激活或不激活emp,只需根据事件过滤数据并显示结果 通过ngOnInit()方法获取所有员工,

ngOnInit(){
 //api get all employees
this.data = result;
this.listData = new MatTableDataSource(this.data);
this.listData.sort = this.sort;
this.listData.paginator = this.paginator;
}

getUsersDataOfType(value) {
var filteredData = [];
// filter this.data based on the value; and assign result to filteredData array
this.listData.data = filteredData;
}

并且您不需要将$(document).ready(function(){}函数与angular一起使用。