\documentclass[a4paper]{ltxdoc}

\usepackage[ocgtabular]{ocg-p}
\usepackage{tikz}
\usepackage{datatool} 
\usepackage{booktabs}

%\usepackage{array}

\newcommand*{\pkg}[1]{\textsf{#1}}

\title{\pkg{OCG-P} package, example 4}

\begin{document}

% generate database with data for the table 
\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}

\maketitle

\section{Without using the ocgtabular environment}
This table can be sorted by clicking on the headers: 

\begin{tikzpicture}
\begin{ocg}{First Name}{ocfirstid}{1}
  \node[] (p1)  {
\begin{tabular}{llc}
\toprule
\bfseries \setocgs{}{ocfirstid}{oclastid ocgradeid}{First name} 
& \bfseries \setocgs{}{oclastid}{ocfirstid ocgradeid}{Last name} 
& \bfseries \setocgs{}{ocgradeid}{ocfirstid oclastid}{Grade}
\DTLsort*{Firstname}{sdata}% sorted on the first name
\DTLforeach{sdata}{\first=Firstname, \last=Lastname,\grade=Grade}{%
\DTLiffirstrow{\\ \midrule}{\\}
\first & \last & \grade
}
\\ \bottomrule
\end{tabular}
};
\end{ocg}

\begin{ocg}{First Name}{oclastid}{0}
  \node[overlay]  (p2)  {
\begin{tabular}{llc}
\toprule
\bfseries \setocgs{}{ocfirstid}{oclastid ocgradeid}{First name} 
& \bfseries \setocgs{}{oclastid}{ocfirstid ocgradeid}{Last name} 
& \bfseries \setocgs{}{ocgradeid}{ocfirstid oclastid}{Grade}
\DTLsort*{Lastname}{sdata}% sorted on the last name
\DTLforeach{sdata}{\first=Firstname, \last=Lastname,\grade=Grade}{%
\DTLiffirstrow{\\ \midrule}{\\}
\first & \last & \grade 
}
\\ \bottomrule
\end{tabular}
};
\end{ocg}

\begin{ocg}{First Name}{ocgradeid}{0}
  \node[overlay] (p3)  {
\begin{tabular}{llc}
\toprule
\bfseries \setocgs{}{ocfirstid}{oclastid ocgradeid}{First name} 
& \bfseries \setocgs{}{oclastid}{ocfirstid ocgradeid}{Last name} 
& \bfseries \setocgs{}{ocgradeid}{ocfirstid oclastid}{Grade}
\DTLsort*{Grade}{sdata}% sorted on the grade
\DTLforeach{sdata}{\first=Firstname, \last=Lastname,\grade=Grade}{%
\DTLiffirstrow{\\ \midrule}{\\}
\first & \last & \grade
}
\\ \bottomrule
\end{tabular}
};
\end{ocg}

\end{tikzpicture}

\section{Using the ocgtabular environment}

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}