This topic is locked

Hide Inline and Edit Buttons in a grid page conditionally based on a field value

1/7/2020 5:17:08 PM
PHPRunner Tips and Tricks
A
acpan author

Purpose: To disallow user edit of the data row, using "Is Record Editable" event, based on record row's data and user id
Is Record Editable event, when conditions return as FALSE, will automatically:

  1. Hide the edit buttons and prevent Inline Edit, Edit, Delete of the affected rows
  2. Redirect user to List page if user tries to access Edit URL directly
    Step 1: Create a field name called "record_locked" in your table

    You can use any field eg. Certified which will store 1 (Disallow edit) and 0 (Allow Edit).
    Step 2: Go to [b]Is Record Editable Event and enter the code below[/b]



    Is Record Editable Event:



// First, get the current logged in user data from the user table,

// you can use any field of your user table as long as it is unique.

// We use the id field and id=1 is our admin in this case.
$userData = Security::currentUserData();

$user_id = $userData["id"];
// Start the condition checks

if ($values["record_locked"] == 1 AND $user_id != 1)

return false;

else

return true;


That's all you need to do. If you want to also show a lock icon, proceed to Step 3.
Step 3 (Optional step): To display a lock icon when record_locked = 1
Go to Page Designer and click the record_locked field's View As/Edit As, and set the custom code below:



if ($value == 1 )

{

$value = "<li class='fa fa-lock'></li>";

}

else

{

$value = "";

}


Then, drag the record_locked field to the edit buttons box and remove the record_locked column:



Below is the final screenshot



Note: This can also extend to pages where you want the admin to have edit and not other people.

In this case, it's even easier, just use "Is Record Editable" to check for user id as the condition.

fhumanes 1/9/2020

Hi:
In the specific case of editing the information of a record, PHPRunner has a "perfect" solution and is the "Is Record Editable" event, in which the condition required to deactivate or activate this functionality can be clearly and easily established. .
An example of code can be this:



function IsRecordEditable($values, $isEditable)

{

if ( $values['projects_project_id'] == 2 ) {

$isEditable = false;

} else {

$isEditable = true;

}

return $isEditable;

}


Regards,

A
acpan author 1/10/2020

Thank you, that is one of the ways too, which i was not aware. Nice! Thanks for your input.
This tip originally focuses on GUI presentation with a lock icon in place of edit buttons,

to indicate to user that the record is locked.
It is now modified based on your input to prevent edit not just hide the buttons.

M
mhollibush 1/11/2020

Works Like A Charm.
Perfect explanation
Thank You So Much