Javascript 从服务获取数据后,如何刷新datatable内联编辑器?

Javascript 从服务获取数据后,如何刷新datatable内联编辑器?,javascript,angular,datatable,angular-datatables,Javascript,Angular,Datatable,Angular Datatables,我已经为我的datatable添加了内联编辑数据功能。我使用服务调用获取最新数据,并使用dtOptions绑定到datatable。我正在使用数据表。初始空数据变量绑定。一旦我从服务中获得数据,我就会绑定到dtOptions,这一绑定显示得很好。但是内联编辑不起作用。我不知道从服务获取数据后如何向编辑器添加数据。如果我添加$.fn.dataTable.Editor的访问实例。它只是不起作用。请帮助解决这个问题 HTML 剧本 提供一个dtTrigger,可用于手动触发表的呈现 您需要做的是调用d

我已经为我的datatable添加了内联编辑数据功能。我使用服务调用获取最新数据,并使用dtOptions绑定到datatable。我正在使用数据表。初始空数据变量绑定。一旦我从服务中获得数据,我就会绑定到dtOptions,这一绑定显示得很好。但是内联编辑不起作用。我不知道从服务获取数据后如何向编辑器添加数据。如果我添加$.fn.dataTable.Editor的访问实例。它只是不起作用。请帮助解决这个问题

HTML

剧本

提供一个dtTrigger,可用于手动触发表的呈现

您需要做的是调用dtTrigger来手动呈现表

例如:

HTML:


你能为你的问题创建一个stackblitz实例吗?@Prince-我已经添加了stackblitz,虽然它不起作用,但如果你看到我的代码,你就会明白我的要求。我也在获取数据后重新初始化。这是@bagya的链接,我要求你至少用虚拟数据创建一个工作实例。向下投票,因为这个问题不符合标准。没有人能回答这个问题。也没有实际的代码来复制这个问题,我们也没有读心术的人能够回答这个问题。此外,stackblitz示例使用ViewChild chihd引用不存在的元素-@ViewChild'dtGrid'将在模板中搜索类似@tftd的元素-我将使用stackblitz再现问题,然后将更新您。
 data = [];

renderDatatable(dtColumns, modelObjAttributes) {
    console.log('dtEditor', this.dtEditor);
    const colLenth = dtColumns.length;
    this.dtRendered = false;
    this.dtOptions = {
        dom: 'Bfrtip',      
        data: this.data,
        pageLength: 100,
        columns: dtColumns,
        columnDefs: [ {
          targets: colLenth,
          defaultContent: '',
          title: '<i class="fa fa-plus plusIcon"></i>',
          orderable: false
        }],
        paging: true,
        searching: true,
      //  ordering: true,
        info:     false,
        responsive: true,
        drawCallback: () => {
          const btnElement = this.dtTableId.nativeElement.querySelector('i.plusIcon');
          this.renderer.listen(btnElement, 'click', ($event) => {
              this.onClickPlusIcon(modelObjAttributes);
           });
        },
        scrollY: '500px',
      //  bSort: false,
        scrollCollapse: true,
        select: {
          style:    'os',
          selector: 'td:first-child'
         },
        buttons: [
          { extend: 'create', editor: this.dtEditor },
          { extend: 'edit',   editor: this.dtEditor },
          { extend: 'remove', editor: this.dtEditor }
          // { extend:  'pageLength', editor: this.dtEditor}
        ]
      };
    this.cdr.detectChanges();
    this.dtRendered = true;
    this.cdr.detectChanges();
    this.attachPlusIconClickEvent(modelObjAttributes);
    this.attachDtClickEvent();
}

// This method used to initialize the data table dyanamically
initializeDtEditor(dtColumns, modelObjAttributes) {
    this.dtEditor = new $.fn.dataTable.Editor({  
    data: this.data,
    table: '#dtGrid',
    idSrc: this.uniqueField,
    fields: this.dataTableFields,
    formOptions: {
      inline: {
        onBackground:  'blur',
        onBlur:        'close',
        onComplete:    'close',
        onEsc:         'close',
        onFieldError:  'focus',
        onReturn:      'submit',
        submit:        'changed',
        scope:         'row'
      }
    }
   });
   // this.cdr.detectChanges();
    this.renderDatatable(dtColumns, modelObjAttributes);
}


// This method to get the data from service if you see i'm binding the reponseData to dtOptions. It adding to the datatable but it's not allowing to edit(inline edit). with buttong edit it's working.

getData(modelName) {
  this.dtServiceService.readData(modelName).subscribe(responseData => {
   // console.log(JSON.stringify(data));   
   this.dtOptions['data'] = responseData;
   this.dtRendered = false;
   this.cdr.detectChanges();
   this.dtRendered = true;
   this.cdr.detectChanges();

  },
  error => {
    console.log('data is not getting!');
  });
}
<table datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" class="row-border hover">
<thead>
  <tr>
    <th>ID</th>
    <th>First name</th>
    <th>Last name</th>
  </tr>
</thead>
<tbody>
  <tr *ngFor="let person of persons">
    <td>{{ person.id }}</td>
    <td>{{ person.firstName }}</td>
    <td>{{ person.lastName }}</td>
  </tr>
</tbody>
</table>
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Subject } from 'rxjs';
import { Person } from '../person';

import 'rxjs/add/operator/map';

@Component({
  selector: 'app-angular-way',
  templateUrl: 'angular-way.component.html'
})
export class AngularWayComponent implements OnDestroy, OnInit {
  dtOptions: DataTables.Settings = {};
  persons: Person[] = [];
  // We use this trigger because fetching the list of persons can be quite long,
  // thus we ensure the data is fetched before rendering
  dtTrigger: Subject = new Subject();

  constructor(private http: Http) { }

  ngOnInit(): void {
    this.dtOptions = {
      pagingType: 'full_numbers',
      pageLength: 2
    };
    this.http.get('data/data.json')
      .map(this.extractData)
      .subscribe(persons => {
        this.persons = persons;
        // Calling the DT trigger to manually render the table
        this.dtTrigger.next();
      });
  }

  ngOnDestroy(): void {
    // Do not forget to unsubscribe the event
    this.dtTrigger.unsubscribe();
  }

  private extractData(res: Response) {
    const body = res.json();
    return body.data || {};
  }
}