oTable Initialisation Problem

oTable Initialisation Problem

lyndonwillyndonwil Posts: 40Questions: 5Answers: 0
edited February 2014 in DataTables 1.9
I am playing around with the highlighting example (http://www.datatables.net/release-datatables/examples/api/highlight.html) and would like the same functionality within my program.

I have a strange issue in that, when i set oTable to the datatable, i can't seem to access it.

[code]
var oTable;
$(document).ready(function() {

oTable = $('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "tablephp.php"
});



oTable.$('td').hover( function() {

var iCol = $('td', this.parentNode).index(this) % 7;
$('td:nth-child('+(iCol+1)+')', oTable.$('tr')).css('background-color','red' );
}, function() {

oTable.$('td.highlighted').css('background-color','white' );
} );
} );


[/code]

The strange thing though, is that, if I place an alert after i initialise the table, it starts to work. but only in firefox. I am guessing that this is a question of timing in the DOM ?

I have a live link at http://cloud-contractor.co.uk/test.php

Replies

  • lyndonwillyndonwil Posts: 40Questions: 5Answers: 0
    I've just run this through the datatables debugger.. Allan, if you did get chance to look at it, it's ikureb
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    The problem is that you are using a static event. The event listener is being added to the document immediately after the table has been initialised, but the data hasn't been loaded at that point (the "asynchronous" part of ajax), so the event isn't being added to any rows in the table.

    Really want you want is to use a mouseevent and mouseleave event in a delegated event:

    [code]
    $('example')
    .on( 'mouseenter', 'tbody td', function () {
    ...
    } )
    .on( 'mouseleave', 'tbody td', function () {
    ...
    } );
    [/code]

    Sorry I don't have time to fill in the gaps at the moment, in a bit of a rush!

    Thinking about it it might be more optimal to have the just a mouse enter handler, which would remove the old highlight and a mouse leave on the whole table to remove any highlighting that that point.

    Although worth pointing out that it will be a heck of a lot easier with 1.10's API :-)

    Allan
  • lyndonwillyndonwil Posts: 40Questions: 5Answers: 0
    Allan

    thank you for your reply. That makes perfect sense.

    love the current version and looking forward to 1.10.. :-)
This discussion has been closed.