Combining Multiple Functions

Combining Multiple Functions

ericderoosericderoos Posts: 2Questions: 0Answers: 0
edited August 2022 in Free community support

Hello, First timer with DataTables and loving it. I'm also not fluent in JS so I'm sure this is an easy one to resolve. I appreciate any help combining these two functions. I can get either one of the them to work by themselves, just having a heck of a time trying to merge them so both conditional formatting AND sorting/ordering both work and won't throw up the error...

"DataTables warning: table id=myTable - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3"

Here's my custom.js below...

$(document).ready(function () {
  var table = $('#myTable').DataTable({
    "createdRow": function( row, data, dataIndex ) {
      if ( data[3] == "Cancelled" ) {        
         $(row).addClass('cancelled');
       }

      if ( data[3] == "Completed" ) {        
        $(row).addClass('completed');
      }

      if ( data[3] == "On Hold" ) {        
        $(row).addClass('onHold');
      }

      if ( data[3] == "In Progress" ) {        
        $(row).addClass('inProgress');
      }
    }
  });
}
);




//SORT
$(document).ready(function () {
  $('#myTable').DataTable({
      order: [[1, 'desc']],
  });
});

I also pasted the same code above into a text file here in case it helps...
https://site-proofs.com/MergedData.txt

Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide

Replies

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,735

    You merge them like this:

    $(document).ready(function () {
      var table = $('#myTable').DataTable({
        "createdRow": function( row, data, dataIndex ) {
          if ( data[3] == "Cancelled" ) {       
             $(row).addClass('cancelled');
           }
     
          if ( data[3] == "Completed" ) {       
            $(row).addClass('completed');
          }
     
          if ( data[3] == "On Hold" ) {       
            $(row).addClass('onHold');
          }
     
          if ( data[3] == "In Progress" ) {       
            $(row).addClass('inProgress');
          }
        },  // note the comma here to separate the options
        order: [[1, 'desc']],
      });
    }
    );
    

    Make sure to separate the options using a comma.

    Kevin

  • ericderoosericderoos Posts: 2Questions: 0Answers: 0

    Wow! That was easy. Really appreciate the help.

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin

    Yup, bloomin' genius is Kevin!

    See also this section of the manual which might be of some use.

    Allan

Sign In or Register to comment.