Search code examples
javascriptpdftabular

Sortable table by clicking on headings


Is it possible to create a table in TeX and compile into PDF, which would have its column headers so, that clicking on them would sort the (numerical) table content by that column?

I understand that you can have Javascript somehow in the PDF, which could potentially allow something like this.

Has anyone done such a thing?


Solution

  • After my comment, that that should be possible with OCG layers, i just found out that the ocg-p package offers the ocgtabular environment, which does exactly what you want.

    Example taken from the ocg-p doc:

    \documentclass{article}
    \usepackage[ocgtabular]{ocg-p}
    \usepackage{datatool} % will be needed for this example
    \usepackage{booktabs} % will be needed for this example
    \DTLnewdb{sdata}
    \DTLnewrow{sdata}
    \DTLnewdbentry{sdata}{Firstname}{John}
    \DTLnewdbentry{sdata}{Lastname}{Doe}
    \DTLnewdbentry{sdata}{Grade}{5}
    \DTLnewrow{sdata}
    \DTLnewdbentry{sdata}{Firstname}{Paul}
    \DTLnewdbentry{sdata}{Lastname}{Bauer}
    \DTLnewdbentry{sdata}{Grade}{1}
    \DTLnewrow{sdata}
    \DTLnewdbentry{sdata}{Firstname}{Peggy}
    \DTLnewdbentry{sdata}{Lastname}{Sue}
    \DTLnewdbentry{sdata}{Grade}{3}
    \DTLnewrow{sdata}
    \DTLnewdbentry{sdata}{Firstname}{Ever}
    \DTLnewdbentry{sdata}{Lastname}{Last}
    \DTLnewdbentry{sdata}{Grade}{4}
    \DTLnewrow{sdata}
    \DTLnewdbentry{sdata}{Firstname}{Werner}
    \DTLnewdbentry{sdata}{Lastname}{Moshammer}
    \DTLnewdbentry{sdata}{Grade}{1}
    \begin{document}
    This table can be sorted by clicking on the headers:
    
    \begin{ocgtabular}{llc}{sdata}{}
    \toprule%
    \bfseries \setocgtabularheader{Firstname}{First name}
    & \bfseries \setocgtabularheader{Lastname}{Last name}
    & \bfseries \setocgtabularheader{Grade}{Grade}
    \DTLforeach{sdata}{\first=Firstname, \last=Lastname,\grade=Grade}{%
    \DTLiffirstrow{\\ \midrule}{\\}
    \first & \last & \grade
    }
    \\ \bottomrule%
    \end{ocgtabular}
    \end{document}
    

    sorted by Firstname sorted by Lastname sorted by Grade