Show datatable based on stored user id
Show datatable based on stored user id
jennyong
Posts: 1Questions: 1Answers: 0
Hi,
I want to show datatable for current logged in user only. The datatable should be hiding all the column data if no user data. When user create new data using editor, the user id is stored in $_SESSION['id']. Then in server side query the data using the stored $_SESSION['id']. Can I know how to use $_SESSION['id'] to query the data so that the user view own data only?
Javascript:
var editor;
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
ajax: "user.php",
table: "#userData",
fields: [ {
label: "Name:",
name: "wp_user.name"
},{
label: "Car:",
name: "wp_user.car"
}
]
});
var userTable = $('#userData').DataTable( {
order: [[1, "desc"]],
dom: "Brtip",
ajax: {
"url": "user.php",
"type": "POST"
},
columns: [
{ data: "wp_user.id" },
{ data: "wp_user.name" },
{ data: "wp_user.car" }
],
buttons: [
{ extend: "create", editor: editor },
{ extend: "edit", editor: editor },
{ extend: "remove", editor: editor }
]
} );
Server Script:
$user = Editor::inst( $db, 'wp_coupon' )
->fields(
Field::inst( 'wp_user.id' ),
Field::inst( 'wp_user.name' ),
Field::inst( 'wp_user.car' )
)
->on( 'preCreate', function ( $editor, $values ) {
$editor
->field( 'wp_user.id' )
->setValue( $_SESSION['id'] );
} )
->process( $_POST )
->json();
I'm still new in datatable. Hope someone can help me.
Thank you.
Answers
I see you're using Editor in your example, but our accounts aren't showing that you have a license. Is the license registered to another email address? Please can let us know so we can update our records and provide support.
Thanks,
Colin