basic example with data table

basic example with data table

qwerfqwerf Posts: 3Questions: 0Answers: 0
edited March 22 in Free community support

It would be nice a single page example with everything I need to make it work.
because when you say javascript new DataTable('#example'); that doesn´t work.
css https://cdn.datatables.net/2.0.2/css/dataTables.dataTables.css doesn´t either.
I have tried

<link href="https://cdn.datatables.net/v/dt/dt-2.0.2/datatables.min.css" rel="stylesheet">
    <script src="https://cdn.datatables.net/v/dt/dt-2.0.2/datatables.min.js"></script>
    <script src ="https://code.jquery.com/jquery-3.7.1.js"></script>
    <script src=" https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.datatables.net/2.0.2/js/dataTables.js"></script>
    <script src="https://cdn.datatables.net/2.0.2/js/dataTables.bootstrap5.js"></script>
    <script src="https://cdn.datatables.net/responsive/3.0.0/js/dataTables.responsive.js"></script>
    <script src="https://cdn.datatables.net/responsive/3.0.0/js/responsive.bootstrap5.js"></script>
   <link herf="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
   <link herf="https://cdn.datatables.net/2.0.2/css/dataTables.bootstrap5.css" rel="stylesheet">
   <link herf="https://cdn.datatables.net/responsive/3.0.0/css/responsive.bootstrap5.css" rel="stylesheet">

It doest´t either.

thanks in advance.

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

Replies

  • qwerfqwerf Posts: 3Questions: 0Answers: 0
    edited March 22

    this is the rest and it doesn´t work:

    <script>
            new DataTable('#example', {
            responsive: true
            });
        </script>
    </head>
    
    <body>
        <table id="example" class="table table-striped nowrap" style="width:100%">
            <thead>
                <tr>
                    <th>First name</th>
                    <th>Last name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Age</th>
                    <th>Start date</th>
                    <th>Salary</th>
                    <th>Extn.</th>
                    <th>E-mail</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Tiger</td>
                    <td>Nixon</td>
                    <td>System Architect</td>
                    <td>Edinburgh</td>
                    <td>61</td>
                    <td>2011-04-25</td>
                    <td>$320,800</td>
                    <td>5421</td>
                    <td>t.nixon@datatables.net</td>
                </tr>
                <tr>
                    <td>Garrett</td>
                    <td>Winters</td>
                    <td>Accountant</td>
                    <td>Tokyo</td>
                    <td>63</td>
                    <td>2011-07-25</td>
                    <td>$170,750</td>
                    <td>8422</td>
                    <td>g.winters@datatables.net</td>
                </tr>
                <tr>
                    <td>Ashton</td>
                    <td>Cox</td>
                    <td>Junior Technical Author</td>
                    <td>San Francisco</td>
                    <td>66</td>
                    <td>2009-01-12</td>
                    <td>$86,000</td>
                    <td>1562</td>
                    <td>a.cox@datatables.net</td>
                </tr>
                <tr>
                    <td>Cedric</td>
                    <td>Kelly</td>
                    <td>Senior Javascript Developer</td>
                    <td>Edinburgh</td>
                    <td>22</td>
                    <td>2012-03-29</td>
                    <td>$433,060</td>
                    <td>6224</td>
                    <td>c.kelly@datatables.net</td>
                </tr>
                <tr>
                    <td>Airi</td>
                    <td>Satou</td>
                    <td>Accountant</td>
                    <td>Tokyo</td>
                    <td>33</td>
                    <td>2008-11-28</td>
                    <td>$162,700</td>
                    <td>5407</td>
                    <td>a.satou@datatables.net</td>
                </tr>
                <tr>
                    <td>Brielle</td>
                    <td>Williamson</td>
                    <td>Integration Specialist</td>
                    <td>New York</td>
                    <td>61</td>
                    <td>2012-12-02</td>
                    <td>$372,000</td>
                    <td>4804</td>
                    <td>b.williamson@datatables.net</td>
                </tr>
                <tr>
                    <td>Herrod</td>
                    <td>Chandler</td>
                    <td>Sales Assistant</td>
                    <td>San Francisco</td>
                    <td>59</td>
                    <td>2012-08-06</td>
                    <td>$137,500</td>
                    <td>9608</td>
                    <td>h.chandler@datatables.net</td>
                </tr>
                <tr>
                    <td>Rhona</td>
                    <td>Davidson</td>
                    <td>Integration Specialist</td>
                    <td>Tokyo</td>
                    <td>55</td>
                    <td>2010-10-14</td>
                    <td>$327,900</td>
                    <td>6200</td>
                    <td>r.davidson@datatables.net</td>
                </tr>
                <tr>
                    <td>Colleen</td>
                    <td>Hurst</td>
                    <td>Javascript Developer</td>
                    <td>San Francisco</td>
                    <td>39</td>
                    <td>2009-09-15</td>
                    <td>$205,500</td>
                    <td>2360</td>
                    <td>c.hurst@datatables.net</td>
                </tr>
                <tr>
                    <td>Sonya</td>
                    <td>Frost</td>
                    <td>Software Engineer</td>
                    <td>Edinburgh</td>
                    <td>23</td>
                    <td>2008-12-13</td>
                    <td>$103,600</td>
                    <td>1667</td>
                    <td>s.frost@datatables.net</td>
                </tr>
                <tr>
                    <td>Jena</td>
                    <td>Gaines</td>
                    <td>Office Manager</td>
                    <td>London</td>
                    <td>30</td>
                    <td>2008-12-19</td>
                    <td>$90,560</td>
                    <td>3814</td>
                    <td>j.gaines@datatables.net</td>
                </tr>
                <tr>
                    <td>Quinn</td>
                    <td>Flynn</td>
                    <td>Support Lead</td>
                    <td>Edinburgh</td>
                    <td>22</td>
                    <td>2013-03-03</td>
                    <td>$342,000</td>
                    <td>9497</td>
                    <td>q.flynn@datatables.net</td>
                </tr>
        
     
            </tbody>
        </table>
    </body>
    
    </html>
    

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

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    edited March 22

    There are at least three issues I see off the bat. First you are loading datatables.js twice. Once in line 2 then again in line 5 of the first code snippet. Second is line 2 is loading before jquery.js so you should see an error in your browser's console stating something like this:

    Uncaught ReferenceError: jQuery is not defined

    Third is the script tag initializing Datatables occurs before the tbody so its executed before the table is placed in the document. Eith move that script tag at the end of the page before the closing body tag. See this technote. Or place the initialization insidedocument.ready().

    If you still have troubles please build a test case showing the issues so we can help debug the code.
    https://www.datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • qwerfqwerf Posts: 3Questions: 0Answers: 0
    edited June 6

    thanks a lot for your answer, I solved like this:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>DataTables Example</title>
        
        <!-- DataTables CSS -->
        <link href="https://cdn.datatables.net/2.0.3/css/dataTables.dataTables.css" rel="stylesheet">
        
        <!-- jQuery -->
        <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
        
        <!-- DataTables JS -->
        <script src="https://cdn.datatables.net/2.0.3/js/dataTables.js"></script>
    
        
     
    
    </head>
    <body>
        <h1>DataTables Example</h1>
        
        <table id="example" class="display" style="width:100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Age</th>
                    <th>Country</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>John Doe</td>
                    <td>30</td>
                    <td>USA</td>
                </tr>
                <tr>
                    <td>Jane Smith</td>
                    <td>25</td>
                    <td>Canada</td>
                </tr>
                <tr>
                    <td>Alice Johnson</td>
                    <td>35</td>
                    <td>UK</td>
                </tr>
            </tbody>
        </table>
        
        <script>
            $(document).ready(function() {
                $('#example').DataTable();
            });
        </script>
    </body>
    </html>
    
  • DilapidusDilapidus Posts: 1Questions: 0Answers: 0

    The original point from the OP is valid in my opinion. A simple one page solution to get me started has in fact eluded me as well. A working file I can drop onto my system would be a wonderful start.

    I realize that what is here already should be it or close to it, but for each example, at least the simple ones, I think many people would find a completed solution a very big help

  • allanallan Posts: 63,531Questions: 1Answers: 10,475 Site admin
    edited June 6

    Thanks for the feedback. Part of the issue is that there are so many options. And for each option, there are a multitude of styles (Bootstrap 3/4/5, Bulma, etc) and light / dark mode. And many people (most? Unsure) aren't including libraries directly from the CDN now, but rather using npm and builder such as vite. So it wouldn't apply to them.

    I don't really want to end up creating a web dev 101 course, that isn't in the scope of what I want to achieve with DataTables. But I do appreciate that it can be hard to get started (particularly with the mess that Javascript tooling is currently in).

    Perhaps I need to do a "quick start from nothing tutorial" in the manual and build up options. I'll look into that - thanks.

    Allan

Sign In or Register to comment.