简体   繁体   English

单击有角对象时如何从数据表中检索单个对象?

[英]How to retrieve the single object from the datatable when it is clicked in angular?

Actually i have used datatable to show the data in angular using jquery libraries. 实际上,我已经使用datatable使用jquery库以角度显示数据。

The user will search the name and suppose if the output comes this user will click on the body of datatable and i want to show this single JSON data in console.log(). 用户将搜索名称,并假设是否输出输出,则该用户将单击数据表的主体,而我想在console.log()中显示此单个JSON数据。 How to get this approach? 如何获得这种方法?

在此处输入图片说明

component.ts file component.ts文件

export class ProcessAssessmentComponent implements OnInit {

selections: Observable<Selection[]>;
dataTable: any;
clients: any[];


  constructor(private http: HttpClient,private selectionService: SelectionService,private processAssesstmentService:ProcessAssesstmentService,private chRef: ChangeDetectorRef) { }

  ngOnInit() {
     this.http.get('http://localhost:8080/api/selections')
      .subscribe((data: any[]) => {
        this.clients = data;


        this.chRef.detectChanges();

        // Now you can use jQuery DataTables :
        const table: any = $('table');
        this.dataTable = table.DataTable();
      });
  }


}

component.html file component.html文件

<table class="table table-bodered">

    <thead>
      <tr>
        <th>Mag No</th>
    <th>SelectionDate</th>
  <th> SelectedBy</th>
    <th>PanEximNumber</th>
    <th>Name</th>
    <th>Address</th>
    <th>PhoneNumber</th>
    <th>SelectionType</th>


      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let client of clients">
         <td>{{client.selectionId}}</td>
    <td>{{client.selectionDate}}</td>
  <td>{{client.selectedBy}}</td>
    <td>{{client.panEximNumber}}</td>
    <td>{{client.name}}</td>
    <td>{{client.address}}</td>
    <td>{{client.phoneNumber}}</td>
    <td>{{client.selectionType}}</td>


      </tr>


    </tbody>

  </table>

use (click) event on tr: 在tr上使用(点击)事件:

component.html component.html

        <table class="table table-bodered">

    <thead>
      <tr>
        <th>Mag No</th>
    <th>SelectionDate</th>
  <th> SelectedBy</th>
    <th>PanEximNumber</th>
    <th>Name</th>
    <th>Address</th>
    <th>PhoneNumber</th>
    <th>SelectionType</th>


      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let client of clients" (click)="selectedRow(client)">
         <td>{{client.selectionId}}</td>
    <td>{{client.selectionDate}}</td>
  <td>{{client.selectedBy}}</td>
    <td>{{client.panEximNumber}}</td>
    <td>{{client.name}}</td>
    <td>{{client.address}}</td>
    <td>{{client.phoneNumber}}</td>
    <td>{{client.selectionType}}</td>


      </tr>


    </tbody>

  </table>

component.ts component.ts

export class ProcessAssessmentComponent implements OnInit {

selections: Observable<Selection[]>;
dataTable: any;
clients: any[];


  constructor(private http: HttpClient,private selectionService: SelectionService,private processAssesstmentService:ProcessAssesstmentService,private chRef: ChangeDetectorRef) { }

  ngOnInit() {
     this.http.get('http://localhost:8080/api/selections')
      .subscribe((data: any[]) => {
        this.clients = data;


        this.chRef.detectChanges();

        // Now you can use jQuery DataTables :
        const table: any = $('table');
        this.dataTable = table.DataTable();
      });
  }

   selectedRow(client)
    {
    console.log(client); //selected row data
    }


}

Note: Avoid use of jquery in angular. 注意:避免在angular中使用jquery。 because angular have its on events. 因为角度有其on事件。

if you want pure angular datatable use ngx-datatable from https://www.npmjs.com/package/ngx-datatable . 如果您想要纯角度数据表,请使用https://www.npmjs.com/package/ngx-datatable中的 ngx-datatable。

You can write a function which gets triggered on clicking the row. 您可以编写一个单击该行时触发的函数。

<tr *ngFor="let client of clients" (click)="onClick(client)">
    ...
</tr>

In your js, 在您的js中,

onClick(client:any){
    console.log(client)
}

You can handle the click and get the data from there. 您可以处理点击并从那里获取数据。

export class ProcessAssessmentComponent implements OnInit {

  selections: Observable<Selection[]>;
  dataTable: any;
  clients: any[];

  constructor(
    private http: HttpClient,
    private selectionService: SelectionService,
    private processAssesstmentService:ProcessAssesstmentService,
    private chRef: ChangeDetectorRef
  ) { }

  ngOnInit() {
    this.http.get('http://localhost:8080/api/selections')
    .subscribe((data: any[]) => {
      this.clients = data;

      this.chRef.detectChanges();

      // Now you can use jQuery DataTables :
      const table: any = $('table');
      this.dataTable = table.DataTable();

      $('table tbody').on('click', 'tr', function () {
          var data = table.row(this).data();
          alert( 'You clicked on '+ data[0] +'\'s row' );
      });
    });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM