probably simple, but I'm stuck

probably simple, but I'm stuck

RamarCRamarC Posts: 2Questions: 0Answers: 0
edited November 2012 in General
I'm creating a page that allows user to find and select different "things" (such as people, places, vehicles, etc). The available items and selected items are both in tables rendered via .DataTable() with a JSON array. As the user switches between and searches for "things", the available and selected items tables should be cleared and refreshed with data appropriate to that "thing". All "things" have the same columns with only the labels and grouping differing.

Both tables are loaded using the same method:
[code]
loadTable: function (mode, json) {
var cMode = mode.substring(0, 1).toLowerCase();
var $table;
if (cMode == 'a')
$table = Page.$available;
else
$table = Page.$selected;
if (!$table)
$table = $(String.format("#tbl-{0}", mode));
else
$table.fnClearTable().fnDestroy();

Page.setItemCount(mode, json.data.length);

$table = $table.dataTable({
aaData: json.data,
aoColumns: Page.aoColumns,
bInfo: false,
bProcessing: true,
bPaginate: false,
bLengthChange: false,
bFilter: false,
bScrollInfinite: true,
bScrollCollapse: false,
sScrollY: "300px",
bDeferRender: (json.data.length > 300),
bDestroy: true,
aaSorting: [[Page.DIMENSION, 'asc'], [Page.MEMBER_DESCRIPTION, 'asc']],
dummy: 0 // makes it easier to add/remove options
});
if (Page.context == 'any')
$table.rowGrouping({
bExpandableGrouping: true,
iGroupingColumnIndex: Page.DIMENSION_DESCRIPTION
});

$(String.format("#{0}-area", mode)).removeClass('wait');
if (cMode == 'a')
Page.$available = $table;
else
Page.$selected = $table;
},
[/code]

Everything works fine for the first load regardless of type of "thing"; but any subsequent loads fail due to an error deep within dataTables. If I don't try to destroy the existing table, I get "aoData[...]._aData" is null or not an object even if I pass it the exact same json.data as the initial load. If I try to reset the table, I get fnClearTable() is null so it seems Page.$available/$selected are not really datatables.

How can I save a reference to and reuse a table rendered with DataTables?

Replies

  • RamarCRamarC Posts: 2Questions: 0Answers: 0
    Nevermind. Once I replaced $table.fnClearTable().fnDestroy(); with $table.fnDestroy(); reloading works fine.
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Yeah - DataTables doesn't chain methods at the moment. That will change with the introduction of the new API in 1.10... :-) (the old API will still be supported btw ;-) ).

    Allan
This discussion has been closed.