Total newbie, cannot make small exampple work :(

Total newbie, cannot make small exampple work :(

EraclesEracles Posts: 2Questions: 0Answers: 0
edited October 2018 in Free community support

Hi all, I'm a total newbie, I want to integrate ordering and search in my tables, but cannot make even the smallest example posible to work, dunno why.
This is it, a simple html. Note: the table appears without any styling, no row seperators.. none at all :(. What I'm doing wrong???

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">

    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.18/css/jquery.dataTables.min.css"/>
 
    <script> 
        $(document).ready(function() {
           $('#example').DataTable();
        } );
    </script>
    
    <title>Hello, world!</title>
  </head>
  <body>

    <table id="example" class="display" style="width:100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Age</th>
                    <th>Start date</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Tiger Nixon</td>
                    <td>System Architect</td>
                    <td>Edinburgh</td>
                    <td>61</td>
                    <td>2011/04/25</td>
                    <td>$320,800</td>
                </tr>
                <tr>
                    <td>Garrett Winters</td>
                    <td>Accountant</td>
                    <td>Tokyo</td>
                    <td>63</td>
                    <td>2011/07/25</td>
                    <td>$170,750</td>
                </tr>
            </tbody>
        </table>
    
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.18/js/jquery.dataTables.min.js"></script>
    
  </body>
</html>

EDIT: Updated code formatting to use Markdown code formatting.

Replies

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    I copied your code into this test case:
    http://live.datatables.net/befakeci/1/edit

    I see the following error in the browser's console:

    Uncaught ReferenceError: $ is not defined

    Moved your $(document).ready() code to the end of the code (after loading jQuery) and the code now works:
    http://live.datatables.net/lenalace/1/edit

    Typically when the Datatables formatting and functions don't appear or work you have an error stopping the page loading. The first place to look is the browser's console for errors.

    Kevin

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin

    Javascript is executed by the browser in order of the script tags (ignoring attributes such as defer, etc).

    So the code above is telling it to execute $(document).ready(...) before it has even loaded jQuery.

    Two options:

    1. Do as Kevin says and move the script tag you currently have in the header after the loading of jQuery and DataTables.
    2. Move the two include scripts int the header above your DataTables initialisation script block.

    Allan

  • EraclesEracles Posts: 2Questions: 0Answers: 0
    edited October 2018

    Ok, I see... Thanks! Now the example works. BUt.... the next sterp is to do the same into my webapp where I use templates for page layout and Bootstrap 4.

    All pages share a common template with CSS declarations, and some page title, footer and body conent (loading tiles for body content and footer content).

    It happens that I must have all javascript libraries loading at the right end of the <body> tag. If I move them to any other place, the page does not show correctly at all (and nothing is shown on Chrome console...). This is weird and driving me nuts, because in the previous html example I could move the loading sentences to the begining of the <body> without problems

    I'm stuck on where to put the call and where to put the .js loading :/

    <head>
    
      <title>
        <fmt:message key="${title}"/>    
      </title>
    
      <!-- BOOTSTRAP4 Meta tag and styling --> 
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>  
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"/>    
    
      <!-- DATA TABLES plugin styling -->
      <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.18/css/dataTables.bootstrap4.min.css"/>
    
      <!-- Our own styling -->  
      <c:url var="stylesURL" value="/css/styles.css" />
      <link rel="StyleSheet" href="${stylesURL}" type="text/css" media="all" />
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    
    </head>
    
        <!-- Start of HTML body. -->
        <body class="mybody">
    
        <!-- Window. -->
        <div class="mywindowdiv" id="mywindow">
    
             <!-- Page title. -->    
             <div class="mypagetitlediv" id="mypageTitle">
                 <tiles:get name="pageTitle"/>
             </div>
    
             <!-- Body content. HERE I WILL hVE MY TABLE!!! -->
             <div class="mycontentdiv" id="mycontent">
               <tiles:get name="content"/>
             </div>
    
             <!-- Body footer. -->
             <div class="myfooterdiv" id="myfooter">
               <tiles:get name="footer"/>
             </div>
    
        <!-- End of "window" id. -->
        </div>
    
        <!-- End of HTML body. -->
    
            <!-- JavaScript for BOOTSTRAP -->
            <!-- jQuery first, then Popper.js, then Bootstrap JS -->
            <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
            <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    
            <!-- JavaScript for DATA TABLES -->
            <script type="text/javascript" src="https://cdn.datatables.net/1.10.18/js/jquery.dataTables.min.js"></script>
            <script type="text/javascript" src="https://cdn.datatables.net/1.10.18/js/dataTables.bootstrap4.min.js"></script>
    
        </body>
    
This discussion has been closed.