Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Adding datatable to confluence

Brian McKinnon August 14, 2014

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?

 

 

5 answers

1 accepted

0 votes
Answer accepted
Brian McKinnon August 17, 2014

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!

 

2 votes
Yagnesh Bhat
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 15, 2014

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.

1 vote
Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 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.

0 votes
weweave UG
Atlassian Partner
April 18, 2020

Although this post is quite old, we would like to let you know that we developed the app "DataTables for Confluence" (see https://marketplace.atlassian.com/apps/1220302/datatables-for-confluence) for Confluence Cloud which adds DataTables features (such as filtering / full-text search) to native Confluence tables.

0 votes
Brian McKinnon August 15, 2014

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
TAGS
AUG Leaders

Atlassian Community Events