update table using preEdit to set field value
update table using preEdit to set field value
I have a users table where the site permissions are set using an integer variable in a field userLevel.
The userLevel is set from a select list, superadmin=1, admin=2, member=3, contact=4.
The admin pages are accessible by userLevels 1 and 2
I have ensured that if superadmin is logged in, all items in the select list are available, but if admin is logged in, they can only set user levels of admin and below.
Problem comes with an admin editing a superadmin user, as I don't want the admin to be able to to change userlevel above their own, and this is compounded by the fact that the superadmin option is not available in the admin select list.
I feel my best option would be to check the existing userLevel and if it is 1, re-set it to 1 on preEdit.
I can successfully detect the existing user level, but cannot work out how to update the value
var existingUserLevel;
editor
.on( 'initEdit', function ( e, node, data ) {
existingUserLevel = data.contacts.UserLevel;
})
.on( 'preEdit', function ( e, json, data ) {
selectedlevel = data.contacts.UserLevel;
console.log(existingUserLevel);
console.log(selectedlevel);
if (existingUserLevel == 1) {
data.contacts.UserLevel = 1;
console.log('is a super admin, do nothing');
} else {
console.log('not a super admin, so update');
data.contacts.UserLevel = selectedlevel;
}
});
This question has accepted answers - jump to:
Answers
By the time
preEdit
is called on the client-side its already too late to change the saved data - that was done by the server and this even is triggered once the Ajax response has been sent back.Might another option to be to use the
user-select
event to disallow selection of rows that the user can't edit?Allan
I see,
Ok, cant see any examples using user-select, but you have given me an idea.
I have taken a slightly different approach, using editor.on 'preOpen', i am comparing the user level saved in the session, to that in the contacts table.
If the session value is higher than the table value, then the row being edited must have a higher security level than the person logged in, so in this case i can use the preOpen function to simply hide the editor fields and display a message
thanks to this thead for the idea
} );
Example here.
Good to hear you have it working though.
Allan
Thanks for the example.
I can see how it works when using cell values, but how could I use it for a hidden field?
For example, if my data object contains a specific rowid or userid, how could I use user-select to prevent the selection of an row in a DataTable ?
Thanks for the example.
I can see how it works when using cell values, but how could I use it for a hidden field?
For example, if my data object contains a specific rowid or userid, how could I use user-select to prevent the selection of an row in a DataTable ?
(sorry for the double click)
You'd get the data for the row in question using the
row().data()
method and then check whatever logic condition you need.Allan
sorry, being a bit thick here, my jquery isn't great
I have tried passing what i think are the correct parameters into console log to see the row data, but i cannot get the synyax right, i always get the same row, whatever row i click
You need to use
row()
to select the row. Just usingrow()
on its own will indeed just always select the same row - seerow()
for details on how to use the method to select rows.In this case
dt.row( cell.index().row )
would do it.Awesome.