Adding datatable to confluence
Asked by Brian McKinnon:

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?


Answers:
author: Brian McKinnon
body: 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!


author: Yagnesh Bhat
body: 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)<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)<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.Excellentable)<https://marketplace.atlassian.com/plugins/Addteq.Excellentable>is an example.


author: Davin Studer
body: 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](https://marketplace.atlassian.com/plugins/com.stiltsoft.confluence.plugin.tablefilter.tablefilter)? It might get you what you want as well.


author: weweave UG
body: 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.


author: Brian McKinnon
body: 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.


Tags: table