How to add empty data row in Datatable using angular2+?

痞子三分冷 提交于 2020-02-22 05:31:52

问题


I want to add empty data row. My requirement is like columns are dynamic. I have tried using dtInstance to add the row. it's throwing some error.

here is the link

https://stackblitz.com/edit/how-to-replace-all-the-columns-dynamically-in-data-table-gvfose?file=app/app.component.ts

Please let me know if more details required.


回答1:


You gave the Data table wrong model. you are mixing between different models in your code.

I've fixed it as well adding two empty rows, make sure to align with the model defined by columnsDataObj

https://stackblitz.com/edit/how-to-replace-all-the-columns-dynamically-in-data-table-zemuq2?file=app/app.component.ts




回答2:


I dont think that's possible with Angular-datatables out of the box, but you can use Angular-xeditable to achieve that as in this question. A better way to do it in my humble opinion would be, when the user clicks the 'add' button, we show a pop-up, they fill the data, we update the back-end or the reference table and rerender the table.




回答3:


There is a simple demo for your requirement that puts the other button to save the empty column you want to access the data.

For easy to use, add DATA button that would be shown the blank row with input, the style you desire to set up the CSS and don't forgot the CSS scope for your by Encapsulation.

DEMO

If the operation field is superfluous, rid it off and place the event for three columns after entering with your logical condition.


Annoying Button

The add DATA could be replaced with the below code, setup the life hook instead for insert the method in the constructor.

ngAfterViewInit (){
  this.addData();
}


来源:https://stackoverflow.com/questions/60082566/how-to-add-empty-data-row-in-datatable-using-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!