How to access server-side variable (logged in state) from DataTables initialization
How to access server-side variable (logged in state) from DataTables initialization
cserfoss
Posts: 11Questions: 0Answers: 0
Hi —
I've just started working with jQuery and DataTables and have what I hope is a straightforward question. I'm using a server-side environment (php/fmpro) and want to have a specific column display only if a user has been authenticated.
I've looked at two approaches:
- Using "aoColumnDefs" and setting the bVisible attribute
- Using oTable.fnSetColumnVis after the initialization
As the authentication is happening server-side, I've also appended the users login state to the returned json header for the DataTable:
[code]
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $totalrecords,
"iTotalDisplayRecords" => $found,
"bLoggedin" => $bLoggedin,
"aaData" => array()
);
[/code]
So, two questions. Does this method for passing the logged in state of the user make sense or is there another technique that's recommended?
If this is viable, how can I access the bLoggedin value from within my aoColumnDefs definition (or for the fnSetColumnVis call)?
Thanks for your time!
— Charles
I've just started working with jQuery and DataTables and have what I hope is a straightforward question. I'm using a server-side environment (php/fmpro) and want to have a specific column display only if a user has been authenticated.
I've looked at two approaches:
- Using "aoColumnDefs" and setting the bVisible attribute
- Using oTable.fnSetColumnVis after the initialization
As the authentication is happening server-side, I've also appended the users login state to the returned json header for the DataTable:
[code]
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $totalrecords,
"iTotalDisplayRecords" => $found,
"bLoggedin" => $bLoggedin,
"aaData" => array()
);
[/code]
So, two questions. Does this method for passing the logged in state of the user make sense or is there another technique that's recommended?
If this is viable, how can I access the bLoggedin value from within my aoColumnDefs definition (or for the fnSetColumnVis call)?
Thanks for your time!
— Charles
This discussion has been closed.
Replies
The other way of approaching the problem is to modify both your HTML table and the JSON return to know if the user is logged in or not. So your HTML table will reflect the correct number of columns with no visible columns. Personally that would be my approach, since it would need no redundant information is transmitted, but it does mean that your HTML will need to know the logged in state as well.
Allan
In my particular case, my data is server side so fnInitComplete doesn't have the json available to me. However, this helped me to find fnServerData in which I did the following:
[code]
"fnServerData": function(sSource, aoData, fnCallback) {
$.getJSON(sSource,aoData,function(json) {
// Additional Processing on "aTargets": [7]
oTable.fnSetColumnVis(7,json.bLoggedIn);
fnCallback(json);
});
},
[/code]
Which appears to be working fine. Your suggestion of modifying both the HTML table and the JSON return makes sense. I unfortunately simplified my scenario in the original post for clarity.
What I'm trying to put together is a process by which a user can mark rows (jobs) as "favorites". This function is only available to logged in users. Marking is done via a checkbox that I'm capturing the "clicks" for and updating the database accordingly.
It may be that your suggestion would still work, but it's beyond my current jQuery/DataTables abilities. Check in with me in a few weeks and we'll see. * grin *
Thanks again!