API-Method seem to be very slow

API-Method seem to be very slow

OenselOensel Posts: 19Questions: 4Answers: 1

I just want to get all the data of a row, by mouseenter on special cells, for saving it in an array for easy access.
But this takes about 2,5 seconds, what is too much for opening popups with detailed informations.

Am I doing something wrong ?

    $('#'+tabellenname+' tbody .showTooltip').on('mouseenter', function(event)
    {        
        // Daten der Zelle, Zeile und Spalte auslesen
        var columnIndex =   tableApi.cell( this ).index().column;
        var rowIndex =  tableApi.cell( this ).index().row;  
        var spalte =        $(tableApi.column( columnIndex ).header()).text();    
        var data = new Array();  

        for(var index = 0; index < spaltenArray[tabellenname].length; index++)
        {                                               
            var title = spaltenArray[tabellenname][index];      
            if(tableApi.row(rowIndex).data() !== undefined)
            {   
                data[title] = tableApi.row(rowIndex).data()[index];                 
            }                               
        }
    });

Replies

  • allanallan Posts: 61,946Questions: 1Answers: 10,158 Site admin
    edited March 2015

    Your code looks a little over complicated just to get the data for the row I think - why not just use row().data() directly?

    $('#'+tabellenname+' tbody .showTooltip').on('mouseenter', function(event)
    {       
      var data = tableApi.row( $(this).closest('tr') ).data();
      // ... do something with 'data'
    });
    

    Allan

  • OenselOensel Posts: 19Questions: 4Answers: 1

    Hi Allan,
    thanks for your answer, but I need key/value pairs of the data, because I access the data by it's headers title in the following code.

    Getting the data by your code is very fast, so I will use this ;-)
    But now I need to get the title of each column as fast as possible, including hidden columns title.

    Then I can match it by the index.

  • OenselOensel Posts: 19Questions: 4Answers: 1

    OK, I got it myself:

        $('#myTable tbody .showTooltip').on('mouseenter', function(event)
        {        
            var columnIndex =   tableApi.cell( this ).index().column;
            var rowData =       tableApi.row( $(this).closest('tr') ).data();
            var data =      Array();
    
            $.each(tableApi.columns().header(), function(index, titleCell)
            {
                data[$(this).text()] = rowData[index];
            }); 
        )}; 
        console.log(data['myTitle']); // works fine
    
  • allanallan Posts: 61,946Questions: 1Answers: 10,158 Site admin

    Yes that sounds like a good way of doing it. You could also use each() rather than $.each to save a line or two if you wanted:

    $('#myTable tbody .showTooltip').on('mouseenter', function(event)
    {
        var data = {};
    
        tableApi.row( $(this).closest('tr') ).data().each( function ( val, idx ) {
          data[ $(tableApi.column( idx ).header() ).text() ] = val;
        } );
    )};
    console.log(data['myTitle']); // works fine
    

    Having said that, I think a better solution would probably be just to load objects into the table in the first place, rather than using arrays.

    Allan

This discussion has been closed.