How to add sortable columns and search field to a table in html output

Post here questions and problems related to editing and publishing DITA content.
kj_rk
Posts: 30
Joined: Thu Jul 15, 2021 10:01 am

How to add sortable columns and search field to a table in html output

Post by kj_rk »

Hi,
I am trying to figure out if there is a property for the table element that can render tables as sortable and searchable when transformed!

Just like the behavior obtained as a result of adding “sortable, searchable” values to the @class attribute of an HTML <table> element.

Thanks
kj_rk
kj_rk
Posts: 30
Joined: Thu Jul 15, 2021 10:01 am

Re: How to add sortable columns and search field to a table in html output

Post by kj_rk »

Any suggestions to accomplish this?
alin
Site Admin
Posts: 275
Joined: Thu Dec 24, 2009 11:21 am

Re: How to add sortable columns and search field to a table in html output

Post by alin »

Hello,

You can use the DataTables JQuery plugin to make your tables sortable and filterable.
The following procedure describes how to include Data Tables in the WebHelp Responsive output: https://www.oxygenxml.com/doc/versions/ ... s-amd.html

Here is an example of how the tables would look like:

Image

Regards,
Alin
Alin Balasa
Software Developer
<oXygen/> XML Editor
http://www.oxygenxml.com
kj_rk
Posts: 30
Joined: Thu Jul 15, 2021 10:01 am

Re: How to add sortable columns and search field to a table in html output

Post by kj_rk »

Thank you, Alin!
That worked!
LASSE_MLE
Posts: 38
Joined: Mon Dec 05, 2022 3:24 pm

Re: How to add sortable columns and search field to a table in html output

Post by LASSE_MLE »

Hello Alin!
Thanks you for this (now quite old) post, it made my day last Friday.
The output is fantastic!
One small question though, is it possible to apply the script only to specific tables ? I mean, in our WebHelp document there are a lot of tables but most of them only embed just a few lines. For these the filtering stuff is a bit useless.
My idea was to tag the specific table with something like

Code: Select all

<table frame="all" outputclass="FilteredTable">
But I do not understand how to apply the script only to the topics that have these kind of tag?
In my opt file, I've added the

Code: Select all

<fragment placeholder="webhelp.fragment.head" file="html-fragments/data-tables-script-fragment.xml"/>
line. But this apply for all the generated pages. And I do not know how to add data to limit application...
Hope you can help me with this!
LASSE_MLE
Posts: 38
Joined: Mon Dec 05, 2022 3:24 pm

Re: How to add sortable columns and search field to a table in html output

Post by LASSE_MLE »

I think I've found something. When I change the HTML fragment to:

Code: Select all

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="${oxygen-webhelp-template-dir}/resources/DataTables/datatables.css"/>
        <script src="${oxygen-webhelp-template-dir}/resources/DataTables/DataTables-1.13.7/js/jquery.dataTables.js" defer="true"/>
        <script>
        <!--
            window.addEventListener("DOMContentLoaded", (event) => {
                $('.table.FilteredTable').DataTable(
                {
                  order: [],
                  columnDefs: [
                    { className: "dt-head-center dt-body-left", targets: "_all" }
                  ]
                }
                );
            });
        //-->
        </script>
    </head>
</html>
Only the tables with outputclass="FilteredTable" take the template.
Is that the solution? Or is there something smarter ?
alin
Site Admin
Posts: 275
Joined: Thu Dec 24, 2009 11:21 am

Re: How to add sortable columns and search field to a table in html output

Post by alin »

Hello,

I would have chosen the same approach.

Regards,
Alin
Alin Balasa
Software Developer
<oXygen/> XML Editor
http://www.oxygenxml.com
LASSE_MLE
Posts: 38
Joined: Mon Dec 05, 2022 3:24 pm

Re: How to add sortable columns and search field to a table in html output

Post by LASSE_MLE »

Thank you for your feedback Alin!
Post Reply