Error When Adding Row to Table

Error When Adding Row to Table

anthony89anthony89 Posts: 26Questions: 8Answers: 0

I get this error when trying to add a row to table, but not sure how to solve it.

Uncaught Error: DataTables warning: table id=eventtime_details - Requested unknown parameter 'eventtime' for row 2, column 0. For more information about this error, please see http://datatables.net/tn/4

The table I am attempting to add a row to is as follows:

            var oEventSchedulerDetailsTable = $('#eventtime_details').DataTable( {
                info: false,
                paging: false,
                select: {
                    "info": false
                },
                "language": {
                    "emptyTable": "No times scheduled"
                },
                searching: false,
                processing: true,
                columns: [
                    { data: 'eventtime', title: 'Time' }, // Row 0
                    { data: 'execute_on', title: 'Execute on' }, // Row 1
                    { data: 'id', visible: false, searchable: false }, // Row 2
                    { data: 'eventid', visible: false, searchable: false }, // Row 3
                    { data: 'eventdate', visible: false, searchable: false }, // Row 4
                    { data: 'eventday', visible: false, searchable: false }, // Row 5
                    { data: 'recurring', visible: false, searchable: false } // Row 6
                ]
            });

And the code I am using to add the row:

                oEventSchedulerDetailsTable.row.add( [
                    eventtime,
                    execute_on,
                    '',
                    $('#eventid').val(),
                    $('#eventdate').val(),
                    $('#eventday').val(),
                    $('#eventrecurring').val()
                ] ).draw();

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    You are using columns.data because you have object structured data. When using -api row.add()` you need to match that structure meaning you need to add an object not an array.

    Kevin

  • anthony89anthony89 Posts: 26Questions: 8Answers: 0

    So, this may look a little ugly but just testing. I changed the row.add() to this but still no success:

    var data = '{"data":[{"eventtime":"'+eventtime+'","execute_on":"'+execute_on+'","id":"","eventid":"'+$("#eventid").val()+'","eventdate":"'+$("#eventdate").val()+'","eventday":"'+$("#eventday").val()+'","recurring":"'+$("#eventrecurring").val()+'"}]}';
     
    var obj = $.parseJSON(data);
    oEventSchedulerDetailsTable.row.add( obj.data ).draw();
    
    
  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    edited October 2020 Answer ✓

    This part of the statement is creating an array:

    [{"eventtime":"'+eventtime+'","execute_on":"'+execute_on+'","id":"","eventid":"'+$("#eventid").val()+'","eventdate":"'+$("#eventdate").val()+'","eventday":"'+$("#eventday").val()+'","recurring":"'+$("#eventrecurring").val()+'"}]
    

    The row.add() API expects one row of data. In your case that needs to be an object. The rows.add(), plural rows, expects an array of one or more rows where each row is an array or object. For the above you need to use rows.add(). See the examples in the docs for both API's.

    Kevin

  • anthony89anthony89 Posts: 26Questions: 8Answers: 0

    Thanks for the help. For whatever the reason, I just was not seeing it yesterday, but with a night of rest, I finally saw what I needed to do to get it working.

  • anthony89anthony89 Posts: 26Questions: 8Answers: 0

    This works:

    data = {
        eventtime: event_time,
        execute_on: executeOn,
        id: $("#eventid").val(),
        eventid: $("#eventid").val(),
        eventdate: $("#eventdate").val(),
        eventday: $("#eventday").val(),
        recurring: $("#eventrecurring").val()
    };
    
    oEventSchedulerDetailsTable.row.add( data ).draw();
    
    
This discussion has been closed.