Display problems on Joomla 4 site

Display problems on Joomla 4 site

nigelfnigelf Posts: 7Questions: 1Answers: 0
edited February 27 in Free community support

I am trying to use DataTables on a Joomla 4 site, but there are a couple of problems with the display of the data.

As recommended on other threads on this forum, I am using the Joomla Sourceror plugin to facilitate the input of code on the relevant page. I have also selected the required DataTables components from your "Download" page, and have included them in the code for the relevant page, using the "CDN" method. I have also added the following PHP code, within the <source> tags on the page:-

    header("Content-Type:application/json");

    try {
            $database_name     = '****';
            $database_user     = '*****';
            $database_password = '*****';
            $database_host     = 'localhost';

            $pdo = new PDO('mysql:host=' . $database_host . '; dbname=' . $database_name, $database_user, $database_password);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
            $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);          

            $sql = 'select
                    id,
                    number,
                    registration,
                    date            
                    from details_table';           

            $stmt = $pdo->prepare($sql);
            $stmt->execute();
           
            $data = [];

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {          
                 $data[] = $row;  
            }

           $response         = [];
           $response['data'] =  $data;

           echo json_encode($response, JSON_PRETTY_PRINT);

        } catch (PDOException $e) {
            echo 'Database error. ' . $e->getMessage();
        }

The result is that the correct data is shown, but in the format shown on the "Ajax" tab on this page (and not in the properly formatted version shown above on the page):-

https://datatables.net/examples/ajax/objects.html

So, I conclude that the mechanics of obtaining the data are functioning correctly, but the presentation is faulty.

The second problem also relates to the presentation. On the site pages, the main content occupies the left column of the display (about two thirds of the total width), with the site menu in the right hand column. However, on the page that contains the DataTable, the whole of the right hand column has been placed at the top left of the page (directly under the page header), compressed to a very narrow width; with the main content below. The result naturally looks like a mess!

Does anybody have any suggestions for resolving these problems?

Thanks in advance for any assistance or suggestions.

Nigel

Answers

  • allanallan Posts: 64,059Questions: 1Answers: 10,559 Site admin

    Hi Nigel,

    Can you give me a link to the page showing the issue please? I'm not sure what you mean that the correct data is shown (i.e. the DataTable displays as expected?), but that the format is shown differently?

    I'd need a link to a page showing the issue for the second one as well. Sounds like a CSS layout issue.

    Allan

  • nigelfnigelf Posts: 7Questions: 1Answers: 0

    Hello Allan,

    Thanks for your very prompt response!

    The site is:-

    https://j4.sax1g.com/index.php

    The page with the problem is:-

    https://j4.sax1g.com/index.php/about-red-white/fleet-details-3

    When I say that "..the correct data is shown.." I mean that the values extracted from the database, and displayed on the page, are correct - but the display is in "Ajax format". For example:-

    { "data": [ { "id": 1, "fleet_no": "R165", "registration": "GAX1C", "date_new": "9\/65" }, ...

    The display issue only affects that page (Fleet details 3).

    Thanks again for your help.

    Nigel

  • kthorngrenkthorngren Posts: 21,722Questions: 26Answers: 5,027

    That second link ahs this error:

    Uncaught ReferenceError: jQuery is not defined

    Datatables requires jquery.js. Also whole page is formatted incorrectly. For example:

    You might be missing more than just jquery.js.

    Kevin

  • allanallan Posts: 64,059Questions: 1Answers: 10,559 Site admin
    edited February 27

    Looking at the HTML source for the page, you do (now?) have jQuery being loaded, but you do so after loading DataTables. Do it before.

    Also, you are loading the DataTables Javascript twice:

    <script src="https://cdn.datatables.net/v/dt/jszip-3.10.1/dt-2.2 ...
    

    and

    <script src="https://cdn.datatables.net/2.2.2/js/dataTables.js"></script> 
    

    Loose the second one (same with the CSS).

    Next you have:

    <script> $(document).ready( function () {  $('#example').DataTable(); } ); </script> 
    

    But there is no element with an id of #example. There doesn't actually appear to be a <table> tag anywhere on the page.

    Allan

  • nigelfnigelf Posts: 7Questions: 1Answers: 0

    Hello Alan, hello Kevin,

    Thank you both for your very quick advice.

    I have now amended the code on the page, so that the declarations before the PHP code are as follows:-

     {source}
    
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    
    <!-- Additional CSS files linked, according to selections available from DataTables Download page  -->
    
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js" integrity="sha384-VFQrHzqBh5qiJIU0uGU5CIW3+OWpdGGJM9LBnGbuIH2mkICcFZ7lPd/AAtI7SNf7" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js" integrity="sha384-/RlQG9uf0M2vcTw3CX7fbqgbj/h8wKxw7C3zu9/GxcBPRKOEcESxaxufwRXqzq6n" crossorigin="anonymous"></script>
    <script src="https://cdn.datatables.net/v/dt/jszip-3.10.1/dt-2.2.2/b-3.2.2/b-html5-3.2.2/b-print-3.2.2/cr-2.0.4/fc-5.0.4/fh-4.0.1/r-3.0.4/datatables.min.js" integrity="sha384-WTKWkqAiDx34EbDQyZni78+3ZbkQ/4+zTt1GOfb8xYO+xq5cgP7zMo7+iuvk8JUa" crossorigin="anonymous"></script>
    
    
    <link href="https://cdn.datatables.net/v/dt/jszip-3.10.1/dt-2.2.2/b-3.2.2/b-html5-3.2.2/b-print-3.2.2/cr-2.0.4/fc-5.0.4/fh-4.0.1/r-3.0.4/datatables.min.css" rel="stylesheet" integrity="sha384-ZMLyS6coQ2lJEwXMGzfk5AcB5D4Ga/uR9Qvr7GjsUcLnNR0EVw1uMaMMpqMO9OkU" crossorigin="anonymous">
    
    
    <?php  
    

    Unfortunately, this has not resolved the problem. As far as I can tell, there is no change to the appearance of the page. I am now wondering if this occurs because the code that is added via Sourceror is all within the main body of the page, and not in the <head> section, where most of the stylesheets and script files are defined?

    If that is the case, I guess that I may need to examine the possibility of adding those file references to the Joomla theme. Would that make sense?

    Thanks again for your help.

    Regards,

    Nigel

  • kthorngrenkthorngren Posts: 21,722Questions: 26Answers: 5,027
    edited February 27

    I'm not familiar with Joomia nor Sourcer but, as Allan mentioned, there is no table tag and I don't see where you are initializing Datatables. Possibly you have the init code in a .js file., If you do which file contains the init code?

    I also see this:

    <code data-spm-anchor-id="a2c65.11461447.0.i3.790f3aeaXBIKYy">{
        "data": [
            {
                "id": 1,
                "fleet_no": "R165",
                "registration": "GAX1C",
                "date_new": "9\/65"
            },
    ....
    

    Which is why you are seeing JSON output on the page.

    Kevin

  • nigelfnigelf Posts: 7Questions: 1Answers: 0

    Hello Kevin,

    Thanks again for your advice. I must confess that I had omitted to include the init code and the

    <

    table> tag, etc, despite previously reading the references to them.

    I should perhaps explain that this project is a form of "proof of concept" for some other sites with much larger databases than the 145 records in this case. As such, I am looking for a method that will facilitate accessing the records direct from the database; and I am unsure if Ajax is appropriate. I will therefore re-examine Allan's extensive documentation to try to clarify that point.

    Regards,

    Nigel

  • kthorngrenkthorngren Posts: 21,722Questions: 26Answers: 5,027
    edited February 28

    See the data docs for the supported data sources. Likely Ajax will be your best options as the request can be sent to a URL that will route to a function to execute the data query. The response can be sent as JSON data. See the ajax docs for more details.

    See this Ajax example.

    Kevin

  • nigelfnigelf Posts: 7Questions: 1Answers: 0

    Hello,

    Just a simple question for clarification:-

    On this page:

    https://datatables.net/examples/server_side/simple.html

    ...the Javascript tab shows a line :

    ajax: 'scripts/server_processing.php',

    I presume this php file should contain the content shown on the adjacent "Server-side script" tab?

    That is the approach that I am trying currently.

    Thanks in advance.

    Regards, Nigel

  • kthorngrenkthorngren Posts: 21,722Questions: 26Answers: 5,027

    The server side processing examples use a simple/sample script called ssp.class.php. Its not maintained but you can use it to learn from. The recommended option is to use the Editor server side libraries. You may or may not want to use the client side Editor functionality, which is licensed, but you can use the server side libraries which is open source. See this blog for more details.

    Antoher optino is to write your own server side processing library. See the SSP protocol docs for details.

    Kevin

  • allanallan Posts: 64,059Questions: 1Answers: 10,559 Site admin

    Worth noting as well that server side processing is only worth pursuing if you are working with tens if thousands of rows or more. It adds complexity and if you don't need it, just Ajax load the data and allow DataTables to process it on the client side.

    Allan

  • nigelfnigelf Posts: 7Questions: 1Answers: 0

    Thanks for the quick and helpful responses again. I will examine those possibilities in more detail.

    I would also add that one of the sites that I would potentially switch to Joomla has a table with 180,000 records, with more being added at regular intervals.

    Thanks again.

    Regards, Nigel

  • nigelfnigelf Posts: 7Questions: 1Answers: 0

    I have downloaded the ssp.class.php file, and commented lines 20-23 out, as recommended in the comments directly above those lines. I have not made any further changes to the ssp.class.php file.

    That file is now called in the "require" statement in the server_processing.php file. The server_processing.php file is placed in a folder named "server_side/scripts". I have placed this file link in the "ajax:" line of the script, as shown on the "Javascript" tab on the documentation page ( https://datatables.net/examples/server_side/simple.html ).

    The HTML code for the table header and footer elements is included in the php file for the web page. For development purposes I have set this up on a plain (non-Joomla) site at:-

    https://www.mhsoftware.co.uk/test4.php

    Is my understanding of the instructions and examples correct? The resulting page contains the correct data, but not properly placed within the table.

    Thanks again.

    Nigel

  • kthorngrenkthorngren Posts: 21,722Questions: 26Answers: 5,027
    edited March 2

    The problem is the Datatables initialization code is executing before the table element is added to the document. So #example is not found and there is no ajax request sent to the server. Relevant code snippet:

        <script> 
        
        new DataTable('#example', {
        ajax: 'https://mhsoftware.co.uk/server_side/scripts/server_processing.php',
        processing: true,
        serverSide: true
        });
        
        </script>
        
        
    </head>
    
        
    <body>
        
    <h3>Testing Datatables on plain website (without Joomla)</h3>
        
    <p>Based on: https://datatables.net/examples/server_side/simple.html</p>
        
    id: 1 - Fleet no: R165 GAX1C<br>id: 2 - Fleet no: R265 GAX2C<br>id: 3 - Fleet no: R365 GAX3C<br>id: 4 - Fleet no: R465 GAX4C<br>id: 5 - Fleet no: R565 GAX5C<br>id: 6 - Fleet no: R665 GAX6C<br>id: 7 - Fleet no: R765 GAX7C<br>id: 8 - Fleet no: R865 GAX8C<br>id: 9 - Fleet no: R965 GAX9C<br>id: 10 - Fleet no: R1065 GAX10C<br>id: 11 - Fleet no: R1165 GAX11C<br>id: 12 - Fleet no: RC166 GWO1D<br>id: 13 - Fleet no: RC266 GWO2D<br>id: 14 - Fleet no: RC366 GWO3D<br>id: 15 - Fleet no: RC466 GWO4D<br>id: 16 - Fleet no: RC566 GWO5D<br>id: 17 - Fleet no: RC666 GWO6D<br>id: 18 - Fleet no: RC766 GWO7D<br>id: 19 - Fleet no: RC866 GWO8D<br>id: 20 - Fleet no: RC966 GWO9D<br>id: 21 - Fleet no: RC1066 GWO10D<br>id: 22 - Fleet no: RC1166 GWO11D<br>id: 23 - Fleet no: RS167 LAX101E<br>id: 24 - Fleet no: RS267 LAX102E<br>id: 25 - Fleet no: RS367 LAX103E<br>id: 26 - Fleet no: RS467 LAX104E<br>id: 27 - Fleet no: RS567 LAX105E<br>id: 28 - Fleet no: RS667 LAX106E<br>id: 29 - Fleet no: RS767 LAX107E<br>id: 30 - Fleet no: RS867 LAX108E<br>id: 31 - Fleet no: RS967 LAX109E<br>id: 32 - Fleet no: RS1067 LAX110E<br>id: 33 - Fleet no: RS1167 LAX111E<br>id: 34 - Fleet no: RS1267 LAX112E<br>id: 35 - Fleet no: RS1367 LAX113E<br>id: 36 - Fleet no: RS1467 LAX114E<br>id: 37 - Fleet no: RS1567 LAX115E<br>id: 38 - Fleet no: RS1667 LAX116E<br>id: 39 - Fleet no: RS1767 LAX117E<br>id: 40 - Fleet no: RS1867 LAX118E<br>id: 41 - Fleet no: RS1967 LAX119E<br>id: 42 - Fleet no: RS2067 LAX120E<br>id: 43 - Fleet no: RS2167 LAX121E<br>id: 44 - Fleet no: R167 LAX122F<br>id: 45 - Fleet no: R267 LAX123F<br>id: 46 - Fleet no: R367 LAX124F<br>id: 47 - Fleet no: R467 LAX125F<br>id: 48 - Fleet no: R567 LAX126F<br>id: 49 - Fleet no: RC168 OAX1F<br>id: 50 - Fleet no: RC268 OAX2F<br>id: 51 - Fleet no: RC368 OAX3F<br>id: 52 - Fleet no: RC468 OAX4F<br>id: 53 - Fleet no: RC568 OAX5F<br>id: 54 - Fleet no: RC668 OAX6F<br>id: 55 - Fleet no: RC768 OAX7F<br>id: 56 - Fleet no: RC868 OAX8F<br>id: 57 - Fleet no: RC968 OAX9F<br>id: 58 - Fleet no: RC1068 OAX10F<br>id: 59 - Fleet no: R168 RAX1G<br>id: 60 - Fleet no: R268 RAX2G<br>id: 61 - Fleet no: R368 RAX3G<br>id: 62 - Fleet no: R468 RAX4G<br>id: 63 - Fleet no: R568 RAX5G<br>id: 64 - Fleet no: R668 RAX6G<br>id: 65 - Fleet no: R768 RAX7G<br>id: 66 - Fleet no: R868 RAX8G<br>id: 67 - Fleet no: R968 RAX9G<br>id: 68 - Fleet no: R1068 RAX10G<br>id: 69 - Fleet no: R1168 RAX11G<br>id: 70 - Fleet no: R1268 RAX12G<br>id: 71 - Fleet no: R1368 RAX13G<br>id: 72 - Fleet no: R1468 RAX14G<br>id: 73 - Fleet no: R1568 RAX15G<br>id: 74 - Fleet no: R1668 RAX16G<br>id: 75 - Fleet no: R1768 RAX17G<br>id: 76 - Fleet no: R1868 RAX18G<br>id: 77 - Fleet no: R1968 RAX19G<br>id: 78 - Fleet no: R2068 RAX20G<br>id: 79 - Fleet no: RC169 SAX1G<br>id: 80 - Fleet no: RC269 SAX2G<br>id: 81 - Fleet no: RC369 SAX3G<br>id: 82 - Fleet no: RC469 SAX4G<br>id: 83 - Fleet no: RC569 SAX5G<br>id: 84 - Fleet no: RC669 SAX6G<br>id: 85 - Fleet no: RC769 SAX7G<br>id: 86 - Fleet no: RC869 SAX8G<br>id: 87 - Fleet no: RC969 SAX9G<br>id: 88 - Fleet no: RC1069 SAX10G<br>id: 89 - Fleet no: RC1169 SAX11G<br>id: 90 - Fleet no: RD171 XWO171J<br>id: 91 - Fleet no: RD271 XWO172J<br>id: 92 - Fleet no: RD371 XWO173J<br>id: 93 - Fleet no: RD471 XWO174J<br>id: 94 - Fleet no: R171 XWO450J<br>id: 95 - Fleet no: R271 XWO451J<br>id: 96 - Fleet no: R371 XWO452J<br>id: 97 - Fleet no: R471 XWO453J<br>id: 98 - Fleet no: R571J XWO454J<br>id: 99 - Fleet no: RC171 XWO936J<br>id: 100 - Fleet no: RC271 XWO937J<br>id: 101 - Fleet no: RC371J XWO938J<br>id: 102 - Fleet no: RC471J XWO939J<br>id: 103 - Fleet no: RS171 YAX594J<br>id: 104 - Fleet no: RS271 YAX595J<br>id: 105 - Fleet no: RS371 YAX596J<br>id: 106 - Fleet no: RS471 YAX597K<br>id: 107 - Fleet no: RS571 YAX598K<br>id: 108 - Fleet no: RS671 YAX599K<br>id: 109 - Fleet no: R671J YWO691K<br>id: 110 - Fleet no: R771 YWO692K<br>id: 111 - Fleet no: R871 YWO693K<br>id: 112 - Fleet no: R971 BWO101K<br>id: 113 - Fleet no: R1071 BWO102K<br>id: 114 - Fleet no: R1171 BWO103K<br>id: 115 - Fleet no: RS771 CAX307K<br>id: 116 - Fleet no: RS871 CAX308K<br>id: 117 - Fleet no: RS971 CAX309K<br>id: 118 - Fleet no: RS1071 CAX310K<br>id: 119 - Fleet no: RD172 CWO284K<br>id: 120 - Fleet no: RD272 CWO285K<br>id: 121 - Fleet no: RD372 CWO286K<br>id: 122 - Fleet no: RD472 CWO287K<br>id: 123 - Fleet no: RD572 CWO288K<br>id: 124 - Fleet no: RD672 CWO289K<br>id: 125 - Fleet no: RD772 CWO290K<br>id: 126 - Fleet no: RD872 CWO291K<br>id: 127 - Fleet no: RD972 CWO292K<br>id: 128 - Fleet no: RD1072 CWO293K<br>id: 129 - Fleet no: RD1172 CWO294K<br>id: 130 - Fleet no: RD1272 CWO295K<br>id: 131 - Fleet no: RD1372 CWO296K<br>id: 132 - Fleet no: RD1472J CWO297K<br>id: 133 - Fleet no: RD173J HAX305L<br>id: 134 - Fleet no: RC173J NWO102M<br>id: 135 - Fleet no: RC273 NAX3M<br>id: 136 - Fleet no: RC373 NAX4M<br>id: 137 - Fleet no: RC473 NAX5M<br>id: 138 - Fleet no: RC573 NAX6M<br>id: 139 - Fleet no: RC174 OWO303M<br>id: 140 - Fleet no: RC274 OWO304M<br>id: 141 - Fleet no: RC374 OWO305M<br>id: 142 - Fleet no: RC474 OWO306M<br>id: 143 - Fleet no: RC574 OWO307M<br>id: 144 - Fleet no: RC674 OWO308M<br>id: 145 - Fleet no: RD174J OWO309M<br>
    
    
    <div class="container"> 
        <table id="example" class="display" style="width:100%">
                <thead>
    

    Typically the script tags are placed towards the bottom of the HTML code for proper execution order. Also you might consider placing the Datatables init code in $( document ).ready(). See this tutorial for more info about script tag placement.

    Datatables won't use the data in line 21.

    Kevin

Sign In or Register to comment.