Adding new column dynamically to the datatable

Adding new column dynamically to the datatable

4nil4nil Posts: 9Questions: 1Answers: 0

I am trying to add a new column to the datatable in not a very appropriate way(just a work around) but as of now I have this option only as Allan might be working on this.
Here is the fiddle demo,
http://jsfiddle.net/4nil/zq7j97c4/

Not sure what is issue, could you please help?

Note : reference link
https://datatables.net/forums/discussion/12270/dynamically-add-remove-columns-datatables-warning-requested-unknown-parameter

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    It is not currently possible - see github issue on the topic.

    Allan

  • 4nil4nil Posts: 9Questions: 1Answers: 0

    Thanks Allan for your quick response.
    I have modified my fiddle, now it could add the new column dynamically.
    http://jsfiddle.net/4nil/zq7j97c4/10/
    But I have no idea what could be the drawbacks doing it this way.
    But hope this could help others. :)

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Your method of destroying the existing table is correct. It isn't really "dynamically adding" the column, since the table has to be destroyed, but that is currently the best there is since there isn't (yet) an add column API.

    Allan

  • 4nil4nil Posts: 9Questions: 1Answers: 0

    Thanks for you comment.
    Additional to above, I am trying to add row/column immediately after the selected row/column but it is not working as expected though it is adding the row/column.

    http://jsfiddle.net/4nil/zq7j97c4/15/

    Could you please give me some pointers?

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Sounds like you probably want to use jQuery's $().insertAfter() on every cell in that column (i.e. iterate over the rows and get the cell for the index you want to insert after and insert after it).

    Allan

  • 4nil4nil Posts: 9Questions: 1Answers: 0

    but any ideas how could i make it possible using aocolumns and aoColdef?
    As I am updating these and then reinitializing it , hence I am expecting it to work.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Empty the table of tbody rows. That might be the problem you are encountering.

    Allan

  • 4nil4nil Posts: 9Questions: 1Answers: 0

    Hi Allan,

    Actually, I am not an expert in javascript.
    Hence, the code quality may not be as per your expectations :)
    but some how I have managed to implement add dynamic col/row into the datatable
    and inline editing
    http://jsfiddle.net/4nil/zq7j97c4/

    please let me know in case of any major changes.

    Thanks for your kind and continuous support!

This discussion has been closed.