How to access server-side variable (logged in state) from DataTables initialization

How to access server-side variable (logged in state) from DataTables initialization

cserfosscserfoss Posts: 11Questions: 0Answers: 0
edited April 2012 in DataTables 1.9
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

Replies

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    Yes you can do it that way if you want (use fnInitComplete to get your bLoggedin state and change the column visibility) but the thing to remember is that the data will still be available on the client-side, so you might want to ensure that you only return nulls (or similar) when the user isn't logged in, our they would easily be able to show the data. I'd suggest if you do do it this way, have the column as default hidden and make it visible only if the user is logged in.

    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
  • cserfosscserfoss Posts: 11Questions: 0Answers: 0
    Hi and thanks very much for the response. It's greatly appreciated as is this amazing tool you've put together.

    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!
This discussion has been closed.