confluenceconfluence-rest-api

Using Confluence REST API to create a table that sorts


I can create simple tables in Confluence using their existing API. It works great. This might be asking a lot, but one thing it doesn't do is sort like tables that are created in Confluence through the wysiwyg editor.

For example, I copy the HTML for a table from a hand-made Confluence document, and then create a new document using their api. I stick this html into the document:

    myHtml = '
    <div class="table-wrap">
        <table class="confluenceTable tablesorter tablesorter-default stickyTableHeaders" style="padding: 0px;">
            <thead class="tableFloatingHeaderOriginal">
                <tr class="tablesorter-headerRow">
                    <th class="confluenceTh sortableHeader tablesorter-headerAsc" data-column="0" tabindex="0" unselectable="on"><div class="tablesorter-header-inner">testcol</div></th>
                    <th class="confluenceTh sortableHeader" data-column="1" tabindex="0" unselectable="on"><div class="tablesorter-header-inner">&nbsp;</div></th>
                    <th class="confluenceTh sortableHeader" data-column="2" tabindex="0" unselectable="on"><div class="tablesorter-header-inner">&nbsp;</div></th>
                </tr>
            </thead>
            <thead class="tableFloatingHeader" style="display: none;">
                <tr class="tablesorter-headerRow">
                    <th class="confluenceTh sortableHeader" data-column="0" tabindex="0" unselectable="on"><div class="tablesorter-header-inner">testcol</div></th>
                    <th class="confluenceTh sortableHeader" data-column="1" tabindex="0" unselectable="on"><div class="tablesorter-header-inner">&nbsp;</div></th>
                    <th class="confluenceTh sortableHeader" data-column="2" tabindex="0" unselectable="on"><div class="tablesorter-header-inner">&nbsp;</div></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="confluenceTd">q</td>
                    <td class="confluenceTd">&nbsp;</td>
                    <td class="confluenceTd">&nbsp;</td>
                </tr>
                <tr>
                    <td class="confluenceTd">r</td>
                    <td class="confluenceTd">&nbsp;</td>
                    <td class="confluenceTd">&nbsp;</td>
                </tr>
            </tbody>
        </table>
    </div>
    '

Then, using curl to POST:

curl -u username:password -X POST -H 'Content-Type: application/json' -d'{"type":"page","ancestors":[{"type":"page","id":6358857}],"title":"new page 4","space":{"key":"~theuser"},"body":{"storage":{"value":myHtml,"representation":"storage"}}}' https://confluence.macsales.com/rest/api/content/ | python -mjson.tool

Everything looks great, except the table will not sort. I did notice some of the tags like 'data-column="2"' were stripped from the HTML. Is there a way to not strip these tags?

I saw another posts that suggests using Confluence CLI instead, but this technique is working just fine so far. It would just be nice to be able to sort.


Solution

  • It was a silly mistake on my part. I was copying the html that was generated post-rendering instead of the html source. By using Confluence's classes, sorting works:

    <table class="confluenceTable">
        <tbody>
            <tr>
                <th class="confluenceTh">test321</th>
                <th class="confluenceTh"></th>
            </tr>
            <tr>
                <td class="confluenceTd">h</td>
                <td class="confluenceTd"></td>
            </tr>
            <tr>
                <td class="confluenceTd">k</td>
                <td class="confluenceTd"></td>
            </tr>
        </tbody>
    </table>