Add dataTable and javascript function (2 posts)

  1. madebyj
    Posted 1 year ago #

    Dear experts,

    I am having trouble trying to use the javascript library 'dataTable'.

    I have done this :
    1 - build a child theme
    2 - build functions.php in the child theme
    3 - call javascript datatable libraries and a specific library with enqueue function.

    A test function is working, so it seems OK but the rest is not working.

    Here's my code on my page :

    <div class="tableau">
    <script type="text/javascript"><!--//--><![CDATA[//><!--
    <A href="javascript:;" onClick="test();">Texte1</A>

    Here's my code on my specific library :

    // Fonction de mise en forme de tableau
    function tableau() {
    	$(document).ready( function () {
    	$('#sommets').dataTable( {
    			"dom": 'T<"clear">lfrtip',
    			"tableTools": {
    				"sSwfPath": "/DataTables/extensions/TableTools/swf/copy_csv_xls_pdf.swf"
    		} );
    		} );
    function test() {

    Firebug keeps saying my function tableau is not defined except when I erase all spaces like this :
    <div class="tableau"><script type="text/javascript"><!--//--><![CDATA[//><!--tableau();//--><!]]></script></div>
    ...but then it is doing nothing.

    I have tried to apply the best practice of this page https://codex.wordpress.org/Using_Javascript but I do not know how to make it work.

    The function test is working but the other one is doing nothing. Do someone use datatable and could show me an example ?

    Thank you very much for your help.


  2. bcworkz
    Posted 1 year ago #

    Sorry, I'm not using dataTable, so cannot provide tried and tested code.

    Please post the code you used to enqueue the libraries, these can be tricky to get right, though obviously your specific library loaded correctly. I don't see the point in the tableau() function. Even if there is a point, try removing it for now so that the code executes as soon as the document loads and not when the tableau() call is encountered, just to simplify things as much as possible. You can try adding it back if need be once things are working.

    If you specified the WP jquery library as a dependency (which is the correct way), your jQuery code needs to be inside a noConflict wrapper or you cannot use the $ shortcut. This alone could be the entire problem.

Topic Closed

This topic has been closed to new replies.

About this Topic