Integrate DataTables into page?

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
severtki
Forum Members
Forum Members
Posts: 29
Joined: Tue Aug 11, 2009 1:33 pm

Integrate DataTables into page?

Post by severtki »

I have some sample code for inserting a DataTable (https://datatables.net), using AJAX to populate a table, below. Where (and how) can I embed the script so that it properly executes? I've tried enclosing it in the source code for the main content area within {literal} {/literal} but it seems to get stripped out anyway. The script doesn't belong in the <head>, and putting it in "Smarty data or logic that is specific to this page:" doesn't seem to work, either, since that's server-side (right?).

I'm guessing there's some easy way but I haven't stumbled on the right thread or tutorial for how to do it. Pointers are welcome, please!
Kirk

Code: Select all

<!DOCTYPE html>
<__html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>DataTable Example</title>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css">
  <__script__ src="https://code.jquery.com/jquery-3.6.0.min.js"></__script>
  <__script__ type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></__script>
</head>
</__body>
  <h1>My DataTable</h1>
  <table id="myTable" class="display">
    <thead>
      <tr>
        <!-- Headers will be dynamically filled -->
      </tr>
    </thead>
    <tbody>
      <!-- Data rows will be dynamically filled -->
    </tbody>
  </table>

  <__script__>
    $(document).ready(function() {
      // AJAX request to fetch data
      $.ajax({
        url: 'YOUR_GOOGLE_APPS_SCRIPT_URL',
        dataType: 'json',
        success: function(data) {
          // Create the table headers based on the first row (keys) in the data
          var headers = Object.keys(data[0]);
          headers.forEach(function(header) {
            $('#myTable thead tr').append('<th>' + header + '</th>');
          });

          // Populate table rows
          data.forEach(function(row) {
            var rowData = '<tr>';
            headers.forEach(function(header) {
              rowData += '<td>' + row[header] + '</td>';
            });
            rowData += '</tr>';
            $('#myTable tbody').append(rowData);
          });

          // Initialize DataTables
          $('#myTable').DataTable();
        }
      });
    });
  </__script>
<__body>
</__html>
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1743
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Integrate DataTables into page?

Post by DIGI3 »

The easiest method is to put it in your page template (not page content) just before the closing <__body> tag. Wrapping it in {literal} tags will prevent the use of curly braces in the script from being parsed by Smarty.

There are other methods that are a bit more advanced and/or cleaner, but best to get it working the simplest way first.
Not getting the answer you need? CMSMS support options
webform
Power Poster
Power Poster
Posts: 494
Joined: Sat Nov 25, 2006 3:39 pm
Location: Copenhagen, Denmark

Re: Integrate DataTables into page?

Post by webform »

Here is how i do it:

At the top (after {process_pagedata}) in my page template, i insert a custom text block to hold my JS:

Code: Select all

{$custom_js="{content block='custom_js' label='Custom JS' wysiwyg='false' tab='Extra'}" scope=global}
And before closing <__body> i insert:

Code: Select all

{$custom_js}
That way I can, for example, insert data tables or other scripts on a page basis where necessary without having to load the script on every single page.
severtki
Forum Members
Forum Members
Posts: 29
Joined: Tue Aug 11, 2009 1:33 pm

Re: Integrate DataTables into page?

Post by severtki »

That looks like a good idea. I was also trying with a UDT but couldn't find an example with scripts.
Post Reply

Return to “The Lounge”