【发布时间】:2020-06-17 21:20:48
【问题描述】:
我为我的数据表添加了内联编辑数据功能。我正在使用服务调用来获取最新数据,并使用 dtOptions 绑定到数据表。我正在使用数据表“”。最初为空的数据变量绑定。一旦我从服务中获取数据,我就会绑定到绑定(显示)良好的 dtOptions。但内联编辑不起作用。我不确定从服务中获取数据后如何将数据添加到编辑器。如果我添加 $.fn.dataTable.Editor 的访问实例。它只是不工作。请帮忙解决这个问题。
HTML
<table id='dtGrid' *ngIf="dtRendered" datatable [dtOptions]="dtOptions" class="row-border hover"></table>
脚本
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!');
});
}
【问题讨论】:
-
您能否为您的问题创建一个 stackblitz 实例?
-
@Prince - 我已经在 stackblitiz 中添加了它,但它不起作用,但如果你看到我的代码,你就会明白我在问什么。我也在获取数据后重新初始化。这是链接stackblitz.com/edit/…
-
@bagya 我会要求你至少用虚拟数据制作一个工作实例。
-
投反对票,因为这个问题不符合How To Ask 标准。没有人能够回答它。也没有重现该问题的实际代码,我们不是能够回答这个问题的读者。此外,您的
stackblitz示例使用ViewChildchihd 对不存在元素的引用-@ViewChild('dtGrid')将在模板中搜索类似于<table #dtGrid...的元素,但在您的示例中它只有一个id属性@tftd - 我将使用 stackblitz 重现该问题,然后更新您。
标签: javascript angular datatable angular-datatables