DataTables warning: Requested unknown parameter 'id' for row 3, column 0

DataTables warning: Requested unknown parameter 'id' for row 3, column 0

ama1981ama1981 Posts: 9Questions: 2Answers: 0
edited March 2018 in Free community support

I am trying to implement add row when the user clicks on add row button.
I tried to follow this example: https://datatables.net/examples/api/add_row.html

But it gives me error as:
DataTables warning: table id=example - Requested unknown parameter 'id' for row 4, column 0. For more information about this error, please see http://datatables.net/tn/4

When I press OK to the error it adds row but no data is added.

I have created my sample at this fiddle:

https://jsfiddle.net/aman1981/x7jstfw1/26/

Thanks for looking into.

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,736
    Answer ✓

    Since you are using column.data you are telling Datatables to use objects. You need to change this:

                table.row.add([
                    "2",
                    "Tiger"
                ]).draw(false);
    

    Which is adding an array to be an object, like this:

                table.row.add({
                    id: "2",
                    name: "Tiger"
                }).draw(false);
    

    Kevin

  • ama1981ama1981 Posts: 9Questions: 2Answers: 0

    Thanks for the reply.

    This gives me error :
    "Expected an assignment or function call" and Uncaught SyntaxError: Unexpected token :
    on the line
    id: "2"

  • ama1981ama1981 Posts: 9Questions: 2Answers: 0

    Never mind I got it.
    I needed to pass as an object rather than array

    table.row.add({
    "id": counter +'.1',
    "name": counter +'.2'
    }).draw(false);

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Good spot ;)

  • ama1981ama1981 Posts: 9Questions: 2Answers: 0

    Sorry colin not related to this, but I am using knockout to render my grid. I want to get rid of the subscribe method and just bind to the grid. As by using the subscribe it refreshes and the new added row is lost since its on client side currently. Whats the simplest code that I have to use to bind back to my array/json. The data would be from ajax call to my controller.
    You have my jsfiddle as:
    https://jsfiddle.net/aman1981/x7jstfw1/26/

    Let me know if I need to open a new post for this.
    Thanks

  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin
    Answer ✓

    If you want to use Knockout observables inside DataTables, there are a few posts on that topic that you could refer to.

    There isn't anything in DataTables core that is specific to Knockout though.

    If you want to remove that extra complexity and bypass Knockout, the Ajax section of the manual is where to start.

    Allan

  • ama1981ama1981 Posts: 9Questions: 2Answers: 0

    Thanks allan I will look through.

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Hi ama1981,

    Take a look at this fiddle - the initial table is loaded now in the initComplete, meaning that the subscribe can be entirely removed.

    https://jsfiddle.net/koj64ozu/14/

    Cheers,

    Colin

This discussion has been closed.