Read only when especific value in field

QuestionsRead only when especific value in field
David Ramirez asked 5 years ago

Hi Abu, congratulations for your excelent work.

I need your help, I have a field called “Delegated to” wich only the admin can change:

$col = array();
$col[“title”] = “DELEGATED TO”;
$col[“name”] = “Deleg_to”; //field name in DB table
if ($level_acc==2){            //if user is administrator can change delegated to
  $col[“editable”] = true;
  $col[“edittype”] = “select”;
}
$col[“editoptions”] = array(“value”=>’1:Oswaldo;2:Rafael;3:Gaby;4:David’);
$cols[] = $col;

All others collumms can be edit by regular users (Oswaldo,Rafael,Gaby and David)

But now I want users to be able to edit the rows in which ones they are the delegates. I was traying with only one user but my attempts was unsuccsessful.

I am guiding me from demos/editing/column-access.php changing:

data.gender == ‘male’ from the example for  data.Deleg_to !=’David’ 

And deleting all others parts of script. But is unsuccessful.
I hope you can help me.

Kind regards.

David

3 Answers
Samsun answered 5 years ago

Hi David,

may you can change like this:

if ($level_acc==2){  $edit = “true”;}else{$edit=”false”;}

$col = array();
$col[“title”] = “DELEGATED TO”;
$col[“name”] = “Deleg_to”; //field name in DB table
  $col[“editable”] = $edit;
  $col[“edittype”] = “select”;
$col[“editoptions”] = array(“value”=>’1:Oswaldo;2:Rafael;3:Gaby;4:David’);
$cols[] = $col;

Regards,

Samsun

David Ramirez answered 5 years ago

Thank you Samsun, your code is more clean that my code; but, can you helpme with the second part?:

“I want users to be able to edit the rows in which ones they are the delegates. I was traying with only one user but my attempts was unsuccsessful.

I am guiding me from demos/editing/column-access.php changing:

data.gender == ‘male’ from the example for  data.Deleg_to !=’David’ 

And deleting all others parts of script. But is unsuccessful.”

Thank you again.

David

Abu Ghufran Staff answered 5 years ago

Assuming your logged in user id is available in $_SESSION[“user_id”], e.g. 1,2,3 or 4

You can put following in column Deleg_to:

$col[“editrules”] = array(“required”=>true, “readonly”=>true, “readonly-when”=>”check_client”);

And in HTML code, within script tag:

// readonly conditional function – when return true, field will be readonly
function check_client(formid)
{

var isAdmin = <?php echo ($level_acc==2) ?  “1” : “0” ?>;
var currentUser = <?php echo $_SESSION[“user_id”] ?>;

if (isAdmin) return false;

Deleg_to = jQuery(“input[name=Deleg_to]:last, select[name=Deleg_to]:last”,formid).val();
Deleg_to = parseInt(Deleg_to);

if (Deleg_to == currentUser)
return false;

// for all others make it readonly
return true;
}

_________________________
Abu Ghufran - Dev Team
Grid 4 PHP Framework
 
Your Answer

12 + 11 =

Login with your Social Id:

OR, enter

Attach code here and paste link in question.
Attach screenshot here and paste link in question.



How useful was this discussion?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate it.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?