Hard filter upon initialization

Hard filter upon initialization

gordyrgordyr Posts: 35Questions: 0Answers: 0
edited October 2011 in DataTables 1.8
Hi,

I am trying to perform a hard filter upon table initialization.

I.e. In this case I would like to only display records relating to the current logged in user. My table contains a list of projects and user_id's.

How do I pass the user ID direct into the datatables initialization so that I only get results returned with the user ID?

I hope that makes some sense?

I was under the impression that fnServerData could do this? I am certain the answer is stupidly simple but it I am clearly misunderstanding something here.



[code]
$('#project_list').livequery(function() {

var userid = $('#loggedin_user_id').text();


$(this).dataTable( {
"bProcessing": true,
"bServerSide": true,
"iDisplayLength": 5,
"bLengthChange": false,
"aaSortingFixed": [[2,'desc']],
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push( { "name": "user_id", "value": '"' + userid + '"' } );

$.getJSON( sSource, aoData, function (json) { fnCallback(json) } );
},

"sAjaxSource": "/wp-content/project_list.php"
} );
} );

[/code]

Alternatively, can I do this in my server-side processing script? As I am currently getting the user ID via PHP.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    The looks correct to me (although I'd remove the quotes from the userid value) :-).

    Does your server-side processing script have some code to add a condition on $_GET['"user_id"']?

    Allan
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Btw - DataTables 1.8.2 introduced fnServerParams: http://datatables.net/ref#fnServerParams . This makes it a little easier to just add a parameter as you don't need to do your own Ajax call this way.

    Allan
  • gordyrgordyr Posts: 35Questions: 0Answers: 0
    edited October 2011
    Sigh...

    How annoyingly stupid of me. lol.

    It works perfectly now thanks. And yes the fnServerParams does indeed make it slicker thanks for that. :-)

    However... Now that I have got that to work, it turns out that in order to make it do exactly what I would I would need to do, the filtering would need to be performed from within the server side script as I would like the results to show the total number of results per user and not the total number of entries within the total database.

    Sadly my SQL knowledge is extremely limited and would consider myself very much a learner in this department. For this table I am currently using your example PHP script for server side processing.

    The user_id is actually stored in sSearch_0

    I have clearly been getting confused :-P

    How would I go about performing this filtering within the php script itself? Many thanks for your help so far.
This discussion has been closed.