Adding datatable to confluence

I'm attempting to add a searchable table to confluence using jquery data tables inside an html module. My original code which runs properly outside confluence is:

<!DOCTYPE html>
<html>
<head>
    <title>Thumbnail Collection</title>
    <link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.css">
    <script type="text/javascript" language="javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" language="javascript" src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" class="init">
$(document).ready(function() {
    $('#thumb_table').dataTable();
} );
    </script>
</head>
<body>
    <table id="thumb_table" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>ID</th>
                <th>Last Modified</th>
                <th>Thumbnail</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>img_02</td>
                <td>0x000000000046895e</td>
                <td data-order=1354509177>2012-12-03 04:32:57</td>
                <td><img src="https:/www.example.org/confluence/download/attachments/286745484/img_02?version=6&modificationDate=1408049585000&api=v2"></td>
            </tr>
        </tbody>
    </table>
</body>
</html>

I've tried using variations of:

AJS.$(document).ready(function() {
    $('#thumb_table').dataTable();
} );

without any luck.

Can anyone tell me how to reformat my html so it works properly inside confluence?

4 answers

1 accepted

The problem was I didn't read all my error messages. I was using a confluence page on an http"S" server, while the datatable js were on a regular http server. As a result the datatable js were being blocked. This was my final solution that is working properly.

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.2/css/jquery.dataTables.css">
<script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>

<script type="text/javascript">
AJS.toInit(function(){
    $('#thumb_table').dataTable();
});
</script>

    <table id="thumb_table" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>ID</th>
                <th>Last Modified</th>
                <th>Thumbnail</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>img_02</td>
                <td>0x000000000046895e</td>
                <td data-order=1354509177>2012-12-03 04:32:57</td>
                <td><img src="https://www.example.org/confluence/download/attachments/286745484/img_02?version=6&modificationDate=1408049585000&api=v2"></td>
            </tr>
        </tbody>
    </table>

Thank you David for the toInit function!

I think the best approach here would be the following:

1) Create macro that renders a table via a separate plugin.

You can refer to teh following guidelines for creating user macros via plugins :

https://developer.atlassian.com/display/CONFDEV/Creating+a+New+Confluence+Macro

2) All the javascript must go in as a web resource , as specified in the confluence plugin development guidelines

3) All the HTML you have can go in a velocity file and you must pass the table data from your macro code.

You can use the following example from data tables as reference w.r.t html , css, and js :

http://www.datatables.net/examples/api/multi_filter.html

On a side note there are many plugins that are already available in the atlassian market place that enable searchable tables and they are pretty customizable, so you might wanna have a look at them instead of reinventing the wheel again.

https://marketplace.atlassian.com/plugins/Addteq.Excellentableis an example.

0 vote
Davin Studer Community Champion Aug 15, 2014

I believe you would want to do something like this. You would need to make sure that the data tables scripts are included on the page ... probably via the custom html section.

AJS.toInit(function(){
    AJS.$('#thumb_table').dataTable();
});

By chancwe have you looked into the Table Filter Plugin? It might get you what you want as well.

I'm still getting:

Failed to run init function: TypeError: undefined is not a function

Failed toInit function is: function (){ AJS.$('#thumb_table').dataTable(); }

It looks like the problem is that the dataTable is undefined. What do I need to do to create a custom html section. I'm doing everything within an html module. My current function in confluence is:

<link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.css">
<script type="text/javascript" language="javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" language="javascript" src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>

<script type="text/javascript">
AJS.toInit(function($){
    AJS.$('#thumb_table').dataTable();
});
</script>
 
    <table id="thumb_table" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>ID</th>
                <th>Last Modified</th>
                <th>Thumbnail</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>img_02</td>
                <td>0x000000000046895e</td>
                <td data-order=1354509177>2012-12-03 04:32:57</td>
                <td><img src="https://ww.example.org/confluence/download/attachments/286745484/img_02?version=6&modificationDate=1408049585000&api=v2"></td>
            </tr>
        </tbody>
    </table>

With everything inside a single html module.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Published 12 hours ago in Confluence

Think you know shares vs. @mentions in Confluence? Take this collab quiz.

To anyone who doubts that Atlassians are a little too obsessed with collaboration, and tools related thereto, let me describe a recent discussion we had (which took place on our internal Confluence, ...

95 views 2 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you