Search code examples
bootstrap-4thymeleaf

Bootstrap Sorting doesn't work ( mb problem with imports )


I wanted to sort my table but I am completely new to Bootstrap and frontend in general and was using this guide https://mdbootstrap.com/docs/b4/jquery/tables/sort/ Help me please to understand what I am doing wrong I would appreciate this so much!

<!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
    
    <head>
      <title>SpringMVC + Thymeleaf + Bootstrap 4 Table Example</title>
      <meta charset="utf-8"/>
      <meta name="viewport" content="width=device-width, initial-scale=1"/>
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
      <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
      <style>table.dataTable thead .sorting:after,
      table.dataTable thead .sorting:before,
      table.dataTable thead .sorting_asc:after,
      table.dataTable thead .sorting_asc:before,
      table.dataTable thead .sorting_asc_disabled:after,
      table.dataTable thead .sorting_asc_disabled:before,
      table.dataTable thead .sorting_desc:after,
      table.dataTable thead .sorting_desc:before,
      table.dataTable thead .sorting_desc_disabled:after,
      table.dataTable thead .sorting_desc_disabled:before {
        bottom: .5em;
      }</style>
    </head>
    
    
    <body>
    <button type="button" class="btn btn-primary">Primary</button>
    
    <div class="container">
      <h1>Price List</h1>
      <div class="row col-md-7 table-responsive">
        <table id="dtBasicExample" class="table table-striped table-bordered table-sm" cellspacing="0" width="100%">
          <thead>
          <tr>
            <th>Name</th>
            <th>Price</th>
          </tr>
          </thead>
          <tbody>
          <tr th:each="procedure:${procedures}">
    
            <td th:text="${procedure.getName()}"></td>
            <td th:text="${procedure.getPrice()}"></td>
    
          </tr>
          </tbody>
        </table>
    
      </div>
    </div>
    <script>
      $(document).ready(function () {
        $('#dtBasicExample').DataTable();
        $('.dataTables_length').addClass('bs-select');
      });
    </script>

Solution

  • You are missing the datatable library. Datatables are not part of the jquery library, so you will need to add them separately.

    Add the following in your code just after the entry of the jquery library since the datatable depends on the jquery library.

    <script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
    <link href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css"></link>
    

    If you are new to frontend dev, it is worth pointing out the order of the scripts in a file matters a lot! I had lot of issues because of this in my early days.

    Check the official datatables website for more info.