jQuery how to fix Cannot set property '_DT_CellIndex' of undefined?
When manually appending rows to a table initialized with DataTables, you often encounter the Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined. This error occurs because DataTables maintains an internal cache of row data. When you use .append() directly on the DOM element without triggering a redraw, DataTables loses track of the new cells. Subsequently, when you call .ajax.reload(), the plugin attempts to iterate over its cached collection, which now contains references to elements that no longer exist or have been detached from the document flow. This causes the internal _DT_CellIndex property access to fail on an undefined object.
The root cause in your specific scenario is the direct manipulation of the table's tbody. Your code constructs a string containing a <tr> element and appends it to #tablebqup. While this works visually, DataTables expects you to use its API methods for DOM interaction or explicitly tell the instance that the data has changed.
Why Direct Appending Breaks DataTables
DataTables operates on two modes: client-side processing (where data is stored in memory and mapped to DOM nodes) and server-side processing (where data is fetched via AJAX). Even when using server-side rendering, if you manually inject HTML into the table body, you break the synchronization between the JavaScript object model and the DOM.
When you call $('#tablebqup').DataTable().ajax.reload(), DataTables performs a full refresh. However, during the reload process, it iterates through its internal row collection to update cell indices. Because your manually added row was not part of the original data source or the initial table configuration, it exists in a state where the plugin's internal index tracker is desynchronized. The error happens at line 36 of datatables.min.js when the library tries to assign an index to a cell that DataTables believes should exist but doesn't in its current view model.
The Correct Approach: Re-Render or Use table().row().add()
There are two primary ways to resolve this, depending on your workflow.
Option 1: Re-render the Table
If you have added data via AJAX and successfully inserted it into the database, the cleanest approach is to destroy the existing table instance and re-initialize it with fresh data. This ensures perfect synchronization between the DOM and DataTables' internal cache.
You can achieve this by passing a callback to your AJAX request or calling destroy() and init() after the data update:
$.post(basUrl+'views/component/updtbq.php', {
action:'add_bq',
nomb:nombc,
abrvb:abrv,
sigsocial:sigsoc,
tel:telf,
fax:fx,
}, function(data) {
alert(data);
// Option A: Destroy and re-init (Simplest for full refresh)
$('#tablebqup').DataTable().destroy();
$('#tablebqup').DataTable({
// Your existing DataTable configuration here
ajax: basUrl + 'views/component/tbbanqueupd.php', // Ensure you point to the correct data source
// ... other options
});
// Option B: Clear and redraw if you want to keep current config but refresh data
$('#tablebqup').DataTable().clear().draw();
});
Option 2: Use DataTables Row API
If you must append rows dynamically without destroying the instance, you should use the table().row().add() method instead of .append(). This tells DataTables to manage the new row. However, since you are posting data to a PHP endpoint that then renders the whole table, Option 1 is generally preferred unless you are doing purely client-side updates.
Addressing the "Add" Button Logic
In your provided code, you are handling two distinct operations: adding a new row manually and updating existing records. The update logic works because it likely triggers a full page reload or re-fetches the entire dataset from the server (based on your PHP include 'C:/wamp/www/Mini_Prj/views/component/tbbanqueupd.php'; line). When the whole table is regenerated by the server, DataTables rebuilds its internal cache correctly.
The issue arises only when you attempt to add a row client-side and then reload only via .ajax.reload(). If your PHP endpoint returns just the data rows as JSON (not HTML), .ajax.reload() will work perfectly because it replaces the entire tbody content with fresh data from the server, fixing the desynchronization.
If your PHP endpoint returns an HTML fragment or redirects to a view that includes the table, ensure that you are not mixing client-side DOM manipulation with server-side full renders in a way that leaves stale references behind.
Best Practice for Dynamic Tables
To avoid this error in future projects, adhere to these principles:
- Never manually append rows to a DataTable-initialized table unless you use
table().row().add(). - Prefer full redraws after server-side updates. Use
$('#tablebqup').DataTable().clear().draw();or re-initialize
