Filling two table with one datatable code in one page

Filling two table with one datatable code in one page

VuqarVuqar Posts: 2Questions: 1Answers: 0

Hello,
Can I fill two table in one asp mvc 5 page with one datatable filling code. My code like this:

jQuery(document).ready(function ($) {
createDataTable("#table1");
createDataTable("#table2");
})

function createDataTable(tableName) {
dbObjectsTableContainer = $(tableName);

dbObjectsTable = dbObjectsTableContainer.dataTable({
    "sPaginationType": "bootstrap",

...
...
}

If i delete one of the createDataTable("#tableX"); function caller there are not any exception, but when I call function two times I get exception:

jquery.dataTables.js?v=20190712095952:4107 Uncaught TypeError: Cannot read property 'nTr' of undefined
at _fnGetTdNodes (jquery.dataTables.js?v=20190712095952:4107)
at _fnNodeToColumnIndex (jquery.dataTables.js?v=20190712095952:640)
at m.fn.init.DataTable.fnGetPosition (jquery.dataTables.js?v=20190712095952:5120)
at ResponsiveDatatablesHelper.createExpandIcon (datatables.responsive.js?v=20190712095951:351)
at m.fn.init.fnRowCallback (dbmigrationview.js?v=20190725174010:97)
at _fnCallbackFire (jquery.dataTables.js?v=20190712095952:4274)
at _fnDraw (jquery.dataTables.js?v=20190712095952:1287)
at _fnAjaxUpdateDraw (jquery.dataTables.js?v=20190712095952:1805)
at Object.success (jquery.dataTables.js?v=20190712095952:1663)
at j (jquery-1.11.3.min.js:2)

Please help...

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,150Questions: 26Answers: 4,919
    Answer ✓

    You can use jQuery to determine if the element exists before you initialize the Datatable. Something like this:

    if ( $(tableName).length > 0 ) {
      // init the Datatable
    }
    

    Kevin

  • VuqarVuqar Posts: 2Questions: 1Answers: 0

    It works, thanks...

This discussion has been closed.