Adding a row outside of (document).ready

Adding a row outside of (document).ready

PKrzywickiPKrzywicki Posts: 1Questions: 1Answers: 0

I apologize if this is out there somewhere, but I'm having a ton of problems getting this working and haven't been able to find an answer that works.

I have a table setup and working.

My problem comes up when I try to add a row. I'm using row.add(), but we're only executing this code for certain permissions on my site, so it cannot be in the (document).ready block.

I have to assume I'm missing something simple, but I've been working on this on-and-off for a few days, so I must throw myself at your mercy and hope someone can point me in the right direction.

Here is my table setup. Setting up some of the functionality we need:

<script type="text/javascript">

$(document).ready( function() {
    $('#testTable').DataTable( {
        "aoColumnDefs": [
            { "bSearchable": false, "aTargets": [ 1, 2, 3, 4, 5, 6 ] },
            { "bSortable": false, "aTargets": [ 6, 7 ] }
        ],
        "aoColumns": [
                null,
                { "orderSequence": [ "desc" ] },
                { "orderSequence": [ "desc" ] },
                { "orderSequence": [ "desc" ] },
                { "orderSequence": [ "desc" ] },
                { "orderSequence": [ "desc" ] },
                null,
                null
            ]
    } ); <!-- End dataTable --> 
} ); <!-- End Document.ready -->

</script>

And here is the code I'm trying to use to add a row. I get the "Cannot reinitialise DataTable" error, but haven't been able to figure out how to use row.add without it.

<script type="text/javascript">

$('#testTable').DataTable().row.add( [
    'A', 
    'B', 
    'C', 
    'D', 
    'E', 
    'F',
    'G',
    'H'
] ).draw().nodes().to$().addClass( 'mgr_row' );

</script>

If I move this code into the (document).ready block, it works flawlessly, but that's not an option for our setup. Moving it out of (document).ready adds the rows, but I get the error message. I assume I need to figure out how to use rows.add without reinitialising the table.

Any help would be immensely appreciated!

Answers

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    The issue you are having is that the document ready function runs second from the above code. The code without the document ready obviously runs first, thus when you then try to configure the DataTable you get the error.

    Can you not just put your row.add() call in another document ready function?

    Allan

This discussion has been closed.