views:

367

answers:

2

I am getting a JS error and what to know what it means and how to solve it. (JS noob here)

Error: "tId is not defined"

Line of JS with error: "if (s[i].sInstance = tId) { "

More Information

I am using the Data Table (http://datatables.net) jQuery plugin.

I have a two tables with a class of "dataTable" loaded on a page (inside of jQuery UI tabs). The tables render as expected but I get the error above in Firebug.

Attached is my Data Table config file...

$(document).ready(function() {

//Take from: http://datatables.net/forums/comments.php?DiscussionID=1507
// before creating a table, make sure it is not already created. 
// And if it is, then remove old version before new one is created        
var currTable = $(".dataTable");
if (currTable) {
  // contains the dataTables master records
  var s = $(document).dataTableSettings;
  if (s != 'undefined') {
    var len = s.length;
    for (var i=0; i < len; i++)
    {   
        // if already exists, remove from the array
        if (s[i].sInstance = tId) {
            s.splice(i,1);
        }
    }

  }
}

oTable = $('.dataTable').dataTable({

        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bFilter": false


}); 
});

What does the error mean and how do I resolve it?

A: 

It means what it says: the tId variable has never been assigned to. Therefore you cannot assign it to another variable.

A few things wrong with that code.

Firstly:

var currTable = $(".dataTable");
if (currTable) {

... currTable will always be truthy with jQuery. This is because even an empty array is truthy. The intended test may have been if (currTable.length > 0)

It looks like this is testing for whether it's a string or an array:

if (s != 'undefined') {

You can instead do if ($.isArray(s)) {

Lastly, when you splice an array that you're iterating over, you'll run into trouble with indexes.

Instead I believe that what you want to do is iterate over the tables returned by $('.table') and test those element's ids against s.

wombleton
A: 

I have a two tables with a class of "dataTable" loaded on a page (inside of jQuery UI tabs). The tables render as expected but I get the error above in Firebug.

I'm thinking you need to use a different class name for each table...

var oFirstTable;
var oSecondTable;

$(document).ready(function() {

    // class = FirstTable
    oFirstTable = $('.FirstTable').dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bFilter": false
    }); 

    // class = SecondTable
    oSecondTable = $('.SecondTable').dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bFilter": false
    }); 
});

I currently use 2 DataTables on a few pages in my application and they work well.

Solburn