Search code examples
jquerydatatableprototypejs

Jquery Datatables conflict with Prototype


I am using jquery datatables to load a table. To preface this, I need to have prototype loaded for mini-cart application. I am putting my jquery into no-conflict mode with this following code:

<script type="text/javascript">
    var $j = jQuery.noConflict();
    $j(document).ready(function() {
        $j('#warranty_claim').DataTable({"bFilter": true});
    });
</script>

I get the following console errors:

  (anonymous function) (index):4747
  (anonymous function) jquery-1.2.6.min_noConflict.min.js:27
  (anonymous function) jquery-1.2.6.min_noConflict.min.js:27
  jQuery.extend.each   jquery-1.2.6.min_noConflict.min.js:21
  jQuery.extend.ready  jquery-1.2.6.min_noConflict.min.js:27

However I know my jquery works when I perform this quick test:

<script type="text/javascript">
     var $j = jQuery.noConflict();
     $j(document).ready(function() {
        $j("#foo").show();
    });
</script>

Any help to getting the datatable working would be greatly appreciated.


Solution

  • There is a possibility that the datatables plugin was not written with no-conflict in mind. So if you need to use the plugin you should be able to easily wrap the plugin source code in a closure to fix it.

    (function($){
      /* Rest of plugin source here
       *
       *
       *
       *
       */
    }(jQuery))
    

    And actually looking at the datatables source code it looks like you can just modify the outermost closure

    near the top of the code

    (/** @lends <global> */function( window, document, undefined ) {
    

    becomes

    (/** @lends <global> */function( window, document, $, undefined ) {
    

    and

    }(window, document));
    

    becomes

    }(window, document, jQuery));
    

    try that out and see what happens