10

I'm am new in jQuery, so sorry if my question is too simple.

I am trying to do something like this:

$("#send-one").html('done. ');

var tableProgress= $("<table id='table-progress'><tr><td></td></tr></table>");

$("#send-one").empty().append(tableProgress);

tableProgress.dataTable({
    "bPaginate": false,
    "bLengthChange": false,
    "bFilter": true,
    "bSort": false,
    "bInfo": false,
    "bAutoWidth": false
});

All this occurs inside jQuery ui Dialog Box.

It does not work, I think it's because .dataTable() pluggin can't find the table so I am trying to use jQuery $.when.

The error is this

Uncaught TypeError: Cannot read property 'asSorting' of undefined

What I need is: use .datatable pluggin in a table that is inserted in $("#send-one").html('done. ' + tableProgress) but, using .datatable() directly may not be synchronous to the insertion.

I also tried:

$("#send-one").html('done. ' + tableProgress);
$('#table-progress').dataTable();
5
  • 2
    Doesn't look like you're using Deferred objects, which is pretty much required for this. What are you trying to accomplish? Commented Jun 26, 2013 at 13:51
  • 2
    Maybe you should explain what you want to achieve, instead of letting us guess based on your not-working code. Commented Jun 26, 2013 at 13:51
  • sorry. I made an edit. Commented Jun 26, 2013 at 13:58
  • Nothing yet.. please, help! Commented Jun 27, 2013 at 16:43
  • I had such issue when used outdated version of Datatable. Upgrading to 1.10.5 solved my problem. Commented Mar 10, 2015 at 11:45

6 Answers 6

38

This other stack overflow question had a much clearer answer, that it must have a <thead> and a <tbody> to work: Jquery datatable integration error?

Your's is missing both.

Sign up to request clarification or add additional context in comments.

1 Comment

Just a note: You dont need a <tbody> and <thead> in case you supply columns via init script
8

I did this and it works. apparently some problem with aoSorting of datatables. I don't know why.

$("#send-one").html('done. ');

var tableProgress= $("<table id='table-progress'><tr><th></th></tr><tr><td></td></tr></table>");

$("#send-one").empty().append(tableProgress);

tableProgress.dataTable( {
    "aoColumns": [
          null
        ]
});

Comments

1

If you are using dataTable columnFilter plugin, this one solved my problem.

Just change _fnColumnIndex like this:

function _fnColumnIndex(iColumnIndex) {
        return iColumnIndex;
}

Comments

0
function someAction() {
            var tableProgress;
            tableProgress = $("<table id='table-progress'><tr><td></td></tr></table>");
            $("#send-one").append(tableProgress);
            tableProgress.dataTable();                                       
}

adds the table to #send-one, on document ready, and then calls the dataTable on it. No sense using the id since you can just have it already in an jQuery object.

5 Comments

It has document ready, but #table-progress does not exists at the beginning.. only after some user interaction..
yea updated my answer. You could just put the table code into a jQuery object, and add it to the document and then call dataTable on it.
it does not work.. =( Uncaught TypeError: Object <table id='table-progress'>........</table> has no method 'dataTable'
then your dataTable plugin isnt loaded yet, you are executing that code before it has time to load.
it is loaded. I have a table that is cell is clickable and then open an dialog box (from jquery ui) Than I do what is above...
0

You do not call any asynchronous function (such as some AJAX calls) so the $.when function doesn't make sense here.

As you use functions which will be called after the last one has completed the following code should work.

var tableProgress;
tableProgress = "<table id='table-progress'><tr><td></td></tr></table>";
$("#send-one").html('done. ' + tableProgress);
$('#table-progress').dataTable();  

2 Comments

#table-progress does not exist when document ready... it starts to exist after a file upload...
I did it at first.. but it does not work. So I think about $.when... what do I suppose to test?
0

Make sure you load the plugin .js file correctly.

http://jsfiddle.net/CdRa5/6/

var tableProgress = $('<table id="table-progress"><thead><tr><th>heading</th><th>heading</th></tr></thead><tbody><tr><td>cell</td><td>cell</td></tr><tr><td>cell</td><td>cell</td></tr></tbody></table>');
$("#send-one").empty().append(tableProgress);
tableProgress.dataTable();

2 Comments

Thank you matty, but did not work... still same error.. it is inside a dialog box of jquery ui... could this be the error?
try changing third line to select the DOM node again: $('#table-progress').dataTable(); And don't forget about correct table structure, this plugin doesn't work on table without headings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.