update table using preEdit to set field value

update table using preEdit to set field value

crush123crush123 Posts: 417Questions: 126Answers: 18
edited June 2016 in Editor

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

  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    Answer ✓

    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

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    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

    editor.on( 'preOpen', function ( e )  {
    var modifier = editor.modifier();  // Gets the selected row of the table
    if ( modifier ) {
        // Get data for this row
        var data = table.row( modifier ).data();
        console.log( data.contacts.UserLevel );
        if ( data.contacts.UserLevel < <?php echo $_SESSION['userLevel'];?> ) {
            editor.message('This row can not be altered as the user has a higher level of security');
            editor.hide();  // Hides all the fields
            editor.buttons( { label: 'Close', fn: function () { this.close(); } } );
        } else {
            editor.show();  // Show the fields
        }
    }
    

    } );

  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin

    Example here.

    Good to hear you have it working though.

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    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 ?

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited June 2016

    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)

  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin

    You'd get the data for the row in question using the row().data() method and then check whatever logic condition you need.

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    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

    table
        .on( 'user-select', function ( e, dt, type, cell, originalEvent ) {
            console.log(dt.row().data());
        } );
    
  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    Answer ✓

    You need to use row() to select the row. Just using row() on its own will indeed just always select the same row - see row() for details on how to use the method to select rows.

    In this case dt.row( cell.index().row ) would do it.

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited June 2016

    Awesome.

    table
    .on( 'user-select', function ( e, dt, type, cell, originalEvent ) {
            userlevel = dt.row( cell.index().row).data().contacts.UserLevel;
            console.log(userlevel);
        } );
    
This discussion has been closed.