Avatar of Peter Kroman
Peter KromanFlag for Denmark

asked on 

Data Tables Select

Hi,

I am trying to get Data Tables Select working on this page http://kroweb.dk/gfdev/kancelli_raw2/kancelli.php

I have downloaded the Select extension (Select - 1.2.4) and uploaded it to my page, and I have set select to true in the scripts. But it is not working. Can anybody help with this :)

I have this main file which I believe is the one we should look into :)
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>


<!DOCTYPE html>
<html lang="da">
<head>
  <!-- Check for title--> 
  <title>Dansk Kancelli</title>

  <meta charset="utf-8" lang="da">

  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="style.css">    
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

  

  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.16/datatables.min.css" />

  <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.16/datatables.min.js"></script>

  <script type="text/javascript">
  $(document).ready(function() {
    $('#tdata').dataTable( {
      responsive:true,
      select: true,
      
    } );
  } );
  </script>

</head>


<body>
 <!-- Page content start-->        
 <div class="w3-row" style="overflow: auto">

  <!-- Main page start-->  

  <div class="w3-row">
    <!-- Column1 start-->         
    <div class="w3-col w3-mobile" style="width:80%; margin-top: 10px; margin-left: 20px; margin-bottom:10px; padding-bottom: 10px;"> <br>

      <div id="KcList" class='list' ></div>


      <hr style= 'border-color: #9F5584; margin-bottom:0px; margin-top:0px;'>
      <br>
      <div class="w3-row"> <!-- for content inside this column -->
      </div>
    </div>
    <!-- Column1, end-->

    <!-- Column3 start--> 
    <div class="w3-col w3-mobile" style="width:15%; margin-top: 10px; margin-left: 10px; margin-bottom:10px;"> 
      <div class="container" style="border: 1px solid #666666; margin-top: 20px; margin-bottom: 20px;"><h5>Interessante og meget anvendte arkivalier, herunder fra Dansk Kancelli, Suppliker og Registre og Tegnelser</h5>
      </div>
      <img src= "Praest_der_laeser.png" alt="Slægtsforsker" style="width:100%; border: solid 1px #cccccc;"> 
    </div>
    <!-- Column3 end--> 

    <script>
    $('#KcList > table').remove();    
    $.ajax({
     url: 'ajaxFtListHtml.php',
       //data: $('#search').serialize(),
       method:'POST',
       dataType: 'html'
     }).done(function (res){             
      $('#KcList').html(res);
      $('#tdata').DataTable({select: true, searching: true, paging: false, info: false, "dom": '<"top"<if>', order: []});
      
    });         

     </script>  
   </div>
   <!-- Main page end--> 
   

 </body>   
 </html>
</div>  
<!-- Page content end-->  

Open in new window

CSSHTML* tables

Avatar of undefined
Last Comment
Chris Stanyon
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

I don't see a table defined in your HTML (where is the element with id tdata ?)
Also, line 78 references "DataTable" which is the OLD version of dataTable.  It should be dataTable, not DataTable.

Also, why do you init dataTable twice?  Line 30 & 78.

I have a basic demo here: https://jsfiddle.net/zephyr_hex/h13awjns/1/
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hey Peter,

You say you've downloaded the Select extension and uploaded it to your page, but there's nothing in your code that loads the Select extension - looks like you're probably missing that part

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/select/1.2.4/css/select.dataTables.min.css" />
<script type="text/javascript" src="https://cdn.datatables.net/select/1.2.4/js/dataTables.select.min.js"></script>

Open in new window

Avatar of Peter Kroman
Peter Kroman
Flag of Denmark image

ASKER

Thanks,

I have edited line 78, and removed the script around line 30.

And  I have added the lines (again) that you suggest Chris.

It is still not working :(

I paste in the edited code, and the code that defines the table too.

<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>


<!DOCTYPE html>
<html lang="da">
<head>
  <!-- Check for title--> 
  <title>Dansk Kancelli</title>

  <meta charset="utf-8" lang="da">

  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="style.css">    
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/select/1.2.4/css/select.dataTables.min.css" />
  <script type="text/javascript" src="https://cdn.datatables.net/select/1.2.4/js/dataTables.select.min.js"></script>
  

  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.16/datatables.min.css" />

  <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.16/datatables.min.js"></script>

  

</head>


<body>
 <!-- Page content start-->        
 <div class="w3-row" style="overflow: auto">

  <!-- Main page start-->  

  <div class="w3-row">
    <!-- Column1 start-->         
    <div class="w3-col w3-mobile" style="width:80%; margin-top: 10px; margin-left: 20px; margin-bottom:10px; padding-bottom: 10px;"> <br>

      <div id="KcList" class='list' ></div>


      <hr style= 'border-color: #9F5584; margin-bottom:0px; margin-top:0px;'>
      <br>
      <div class="w3-row"> <!-- for content inside this column -->
      </div>
    </div>
    <!-- Column1, end-->

    <!-- Column3 start--> 
    <div class="w3-col w3-mobile" style="width:15%; margin-top: 10px; margin-left: 10px; margin-bottom:10px;"> 
      <div class="container" style="border: 1px solid #666666; margin-top: 20px; margin-bottom: 20px;"><h5>Interessante og meget anvendte arkivalier, herunder fra Dansk Kancelli, Suppliker og Registre og Tegnelser</h5>
      </div>
      <img src= "Praest_der_laeser.png" alt="Slægtsforsker" style="width:100%; border: solid 1px #cccccc;"> 
    </div>
    <!-- Column3 end--> 

    <script>
    $('#KcList > table').remove();    
    $.ajax({
     url: 'ajaxFtListHtml.php',
       //data: $('#search').serialize(),
       method:'POST',
       dataType: 'html'
     }).done(function (res){             
      $('#KcList').html(res);
      $('#tdata').dataTable({select: true, searching: true, paging: false, info: false, "dom": '<"top"<if>', order: []});
      
    });         

     </script>  
   </div>
   <!-- Main page end--> 
   

 </body>   
 </html>
</div>  
<!-- Page content end-->  

Open in new window



            <?php

            $con = mysqli_connect("db credentials");


        // Check connection
            if($con === false){
                die("ERROR: Could not connect. " . mysqli_connect_error()); 
            }
            mysqli_select_db($con,"genealogisk_dk_db5");

            mysqli_set_charset($con,"utf8");


            $raw_results = mysqli_query($con, "SELECT * FROM danske_kancelli")
            or die(mysql_error());

//var_dump($raw_results);

            if(mysqli_num_rows($raw_results) > 0){ ?>

            <div class="w3-row w3-mobile" style="width:100%; font-size: 12px"> 
  

               <table id="tdata" style="width:100%;" class="dataTable">
      <thead>
         <tr>
            <th class="sorting" aria-controls="tdata">Sted</th>
            <th class="sorting" aria-controls="tdata">Fra</th>
            <th class="sorting" aria-controls="tdata">Til</th>
            <th class="sorting" aria-controls="tdata">Register</th>
            <th class="sorting" aria-controls="tdata">Journal</th>            

         </tr>
      </thead>

     <?php while($row = $result=mysqli_fetch_array($raw_results)): ?>
         <tr>
            <td><?php echo $row["sted"] ?></td>
            <td><?php echo $row["fra_aar"] ?></td>
            <td><?php echo $row["til_aar"] ?></td>
            <td><?php echo "<a href=\"".$row["url"]."\"target=\"_blank\">" . $row["register"] ?></td>
            <td><?php echo "<a href=\"".$row["infourl"]."\"target=\"_blank\">" . $row["journal"] ?></td>
         </tr>
      <?php endwhile; ?>
      
 
   </table>
</div>

<?php
               

            mysqli_close($con);
        }
        ?> 

Open in new window

Avatar of Peter Kroman
Peter Kroman
Flag of Denmark image

ASKER

But - is some kind of installation of Select not needed to get it working?

I have just downloaded the extension  - a folder named "Select-1.2.4" - from datatables.nnet and uploaded that folder to my page.
Is there anything else I need to do?
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hey Peter,

The script at line 30 has no effect so removing it makes sense, but it wasn't part of the problem. And you can use DataTable() with an upper case D - that's actually correct. You can also use it with a lower case d but that returns a different object. Go with the upper case version.

To use the select plugin you need to reference it in your page. You can either download the package and reference that local copy or you can reference the online copy from the DataTables CDN.  You are referencing the online copy so there is no need to download the plugin to a folder.

There are a coupe of reasons for your problem. Firstly, you need to make sure you load the scripts in the correct order: jQuery -> DataTables -> DataTables Select. You are trying to load the Select plugin before the DataTables plugin. Load them in this order:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.16/datatables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/select/1.2.4/js/dataTables.select.min.js"></script>

Open in new window

You also look to have a slight error in your "dom" property. It should be this:

"dom": '<"top"if>t'

Open in new window

You have a mismatch in the brackets and no T element.
Avatar of Peter Kroman
Peter Kroman
Flag of Denmark image

ASKER

Hi Chris,

I have put in your suggestions. Sadly it still does not work. You ca see it here http://kroweb.dk/gfdev/kancelli_raw2/kancelli.php

Code as it is now:
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>


<!DOCTYPE html>
<html lang="da">
<head>
  <!-- Check for title--> 
  <title>Dansk Kancelli</title>

  <meta charset="utf-8" lang="da">

  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="style.css">    
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.16/datatables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/select/1.2.4/js/dataTables.select.min.js"></script> 

</head>


<body>
 <!-- Page content start-->        
 <div class="w3-row" style="overflow: auto">

  <!-- Main page start-->  

  <div class="w3-row">
    <!-- Column1 start-->         
    <div class="w3-col w3-mobile" style="width:80%; margin-top: 10px; margin-left: 20px; margin-bottom:10px; padding-bottom: 10px;"> <br>

      <div id="KcList" class='table' ></div>


      <hr style= 'border-color: #9F5584; margin-bottom:0px; margin-top:0px;'>
      <br>
      <div class="w3-row"> <!-- for content inside this column -->
      </div>
    </div>
    <!-- Column1, end-->

    <!-- Column2 start--> 
    <div class="w3-col w3-mobile" style="width:15%; margin-top: 10px; margin-left: 10px; margin-bottom:10px;"> 
      <div class="container" style="border: 1px solid #666666; margin-top: 20px; margin-bottom: 20px;"><h5>Interessante og meget anvendte arkivalier, herunder fra Dansk Kancelli, Suppliker og Registre og Tegnelser</h5>
      </div>
      <img src= "Praest_der_laeser.png" alt="Slægtsforsker" style="width:100%; border: solid 1px #cccccc;"> 
    </div>
    <!-- Column2 end--> 

    <script>
    $('#KcList > table').remove();    
    $.ajax({
     url: 'ajaxFtListHtml.php',
       method:'POST',
       dataType: 'html'
     }).done(function (res){             
      $('#KcList').html(res);
      $('#tdata').DataTable({responsive: true, searching: true, paging: false, info: false, select: true, "dom": '<"top"if>t', order: []});
      
    });         

     </script>  
   </div>
   <!-- Main page end--> 
   

 </body>   
 </html>
</div>  
<!-- Page content end-->  

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Actually, in the code you posted above it is correct, but on your live page it's wrong. Maybe you haven't uploaded the page changes :)
Avatar of Peter Kroman
Peter Kroman
Flag of Denmark image

ASKER

Well - I am making changes all the time, and I must have made some wrong "change-back" here.

IT IS WORKING NOW :)

Thanks a lot, Chris.
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

No worries Peter - we've all done that :)
Avatar of Peter Kroman
Peter Kroman
Flag of Denmark image

ASKER

Just posting a new question that I hope you will help mw with too :)
Avatar of Peter Kroman
Peter Kroman
Flag of Denmark image

ASKER

Thanks again Chris,

I can not tell you how much I am learning from your great help.
I really hope that I am not tearing your patience with me apart :) :) :)
Just a note for anyone who comes across this down the road.  DataTable with the uppercase D refers to current DataTables object.  dataTable with lower case d refers to the legacy version of jQuery datatables.  I had it backwards earlier.

So, this is legacy usage:
$('#example').dataTable();

Open in new window


And this is current usage (v 1.10+):
$('#example').DataTable();

Open in new window

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Megan,

I did notice that, but actually, you can use both in the current version - the difference is in what gets returned by each call. DataTable() returns an instance of the DataTable API, whereas dataTable() returns an instance of a jQuery object. For most users, it makes sense to use the newer DataTable(), but occassionally, you'll need the dataTable() method because you'll want access to the table as a jQuery object.

:)
HTML
HTML

HTML (HyperText Markup Language) is the main markup language for creating web pages and other information to be displayed in a web browser, providing both the structure and content for what is sent from a web server through the use of tags. The current implementation of the HTML specification is HTML5.

62K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo