Hard filter upon initialization
Hard filter upon initialization
gordyr
Posts: 35Questions: 0Answers: 0
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.
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.
This discussion has been closed.
Replies
Does your server-side processing script have some code to add a condition on $_GET['"user_id"']?
Allan
Allan
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.