This topic is locked

Enable and Disable "inserted button" based on field's value on Grid

5/6/2019 12:14:42 PM
PHPRunner Tips and Tricks
A
acpan author

Purpose: Show and Hide "inserted button" based on field's value on Grid



We will focus on how to show and hide the custom button based on the account_balance field value on each row of the Grid. (We will skip the custom button's events).
Step 1. Go to Page Designer, and insert a custom button [b]email_reminder_button to the row on the grid[/b]
The grid looks like this:
cust_id | cust_name | email | account_balance | email_reminder_button
Step 2. Take note of the item id of the inserted button:
Select the custom button by clicking on it once and take note of item id on the right panel.

In this example, our item id is email_reminder_button
Step 3: Copy the example codes to be used later in your event:
On the same right panel, click the ? mark on the right of "item id" to show how to hide and show the custom button on the grid, it will look similar to the below codes, copy it:



// Hide button:

$pageObject->hideItem("email_reminder_button", $recordId);

// Show button:

$pageObject->showItem("email_reminder_button", $recordId);


Step 4: In [b]After Record Processed event, paste the copied code above to conditionally hide the custom button:

[/b]



// for debugging: show the current row's data:

// echo "Debug row data: ".json_encode($data);
// Get the value of the condition data from current row, in our case, we use account_balance field:

$account_balance = $data["account_balance"];
// ### To add more condition checks from another table, run your SQL here. ###

/*

// Eg:

$id = $data["id"];

$sql = "select * from other_table where active=1 and other_foreign_key_id = $id";

$rs = DB::Query($sql);

$data_checked =$rs->fetchAssoc();

if($data_checked) { $active = 1; }

else { $active = 0; }

// Condition checks with other table field:

if ( $account_balance < 120 AND $active == 1)

*/

// ### To add more condition checks from another table, run your SQL here. ###
if ( $account_balance < 120 )

{

// paste the code from step 3 to show the button

$pageObject->showItem("email_reminder_button", $recordId);

}

else

{

// paste the code from step 3 to hide the button

$pageObject->hideItem("email_reminder_button", $recordId);

}


5. That's all you need to do. The custom buttons will now show and hide based on its row value on the list/grid page.

J
jonlp 8/8/2019

Greetings,
I have a similar needs and found a way to HIDE the edit, delete, add controls from the list page; however, I am struggling on how test for a specific condition to determine when to hide. I have a field in the table called "PERMS" which user permission values. When PERMS value < 50 then I want to hide the controls. I tried to simulate using the example you provided but had no luck.
Please advise...

A
acpan author 8/9/2019

Hi,
the example above was for custom button insert on the grid row.
To hide built-in grid buttons (Inlne_Add, etc):
After record processed event:


// Assuming PERMS is a field in each data row of your table.
if ( $data["PERMS"] < 50 )

{

// Optional, to use in javascript on load event, for hiding buttons on top of grid, can be set to your session value etc, or remove it.

$_SESSION["admin"] = 0;

$pageObject->setProxyValue("permission", $_SESSION["admin"]);

// Optional, to use in javascript on load event, for hiding buttons on top of grid, can be set to your session value etc, or remove it.
$record["edit_link"] = false;

$record["inlineedit_link"] = false;

$record["view_link"] = false;

$record["copy_link"] = false;

$record["edit_link"] = false;

$record["add_link"] = false;

}

else

{

$pageObject->setProxyValue("permission", 1);

$record["edit_link"] = true;

$record["inlineedit_link"] = true;

$record["view_link"] = true;

$record["copy_link"] = true;

$record["edit_link"] = true;

$record["add_link"] = true;

}


To hide the built-in add/edit/delete buttons on top of the grid, in Javascript onload events:

(Note: deleted selected button not able to hide, post here if you find the solution)


var editSelected = $('#edit_selected' + pageid);

var deleteSelected = $('#delete_selected' + pageid);

var inlineAdd = $('#inlineAdd' + pageid);

var addButton = $('#addButton' + pageid);
if (proxy['permission'] == 0)

{

editSelected.hide();

deleteSelected.hide();

inlineAdd.hide();

addButton.hide();

}

else

{

editSelected.show();

deleteSelected.show();

inlineAdd.show();

addButton.show();

}


Alternately, instead of javascript onload event, to hide buttons above the grid, at before display event:



if ($_SESSION["admin"] == 0)

{

// switch to html code at visual editor to find the block tags that contain the buttons or elements you want to hide
// Hide checkbox column

$xt->assign("checkbox_column",1>2);
// $xt->assign("firstAboveGridCell",1>2);
// hide delete selected button

$xt->assign("deleteselected_link",1>2);

}

----------

ACP

J
jonlp 8/14/2019

Hello ACP,
Thank you for assisting me on this. It was my mistake for not being clear on what I am asking. Here are the details:

a) We have a custom log-in (php codes) which the USER ID is obtained and $UID variable is used to store its value. This will be invoked prior to launching LISt PAGE

b)The records from USR table is what the LIST PAGE displays. UID and PERMS are two of the fields in a USR record. UID contains user ID and PERMS contains the numeric values which is used to determine level of access.

c) Since my last post, I also found a way to hide: ADD, EDIT, INLINE-EDIT, and DELETE controls from the LIST page by adding the following codes to the LIST page and was successfully "hide" the functions by adding the below codes to "LIST PAGE: BEFORE PROCESS" event:
$pageObject->hideItem("grid_edit");

$pageObject->hideItem("add");

$pageObject->hideItem("delete");

$pageObject->hideItem("update_selected");
The issue I am having is how to add 2 conditions to the list page: (1) compare the value of UID variable (from the custom login script) to the UID of the USR table, (2) if they match then check the PERMS value from the USR table to see of it is > 50. If it is then hide the edit controls by invoking the codes in (c).
Please advise...

A
acpan author 8/15/2019

Hi,
You are not clear as to user will only see his own data or also can see other people's data, if user can only see his own data,

there is no need to add the condition to check UID.
If user logs in and can see other people's data, it does not make sense, but it seems this is what you are trying to picture in your description.
Assumptions:
User logs in with custom script, and assume user loads the USR Table List page which also contains other people's UID and PERMS,

User will sees a list like this:
UID | PERMS

1 | 51

2 | 20

3 | 52

4 | 20

5 | 19
Assuming user logs in with UID = 3
=========================
custom login script:
After successful login, set session variables in custom login script, eg $_SESSION["UID"] = $UID;
==============
In App init (optional):

Query USR table with UID = $_SESSION["UID"] and obtain PERMS value;

store PERMS into $_SESSION["PERMS"];
============
After record processed event:
if ( $data["PERMS"] > 50 )

{
// hide edit buttons on top of grid

$_SESSION["hide"] = 1;
// hide grid edit controls

$record["edit_link"] = false;

$record["inlineedit_link"] = false;

$record["view_link"] = false;

$record["copy_link"] = false;

$record["edit_link"] = false;

$record["add_link"] = false;

}
Hide edit buttons above the grid, at before display event:
if ($_SESSION["hide"] == 1)

{

// switch to html code at visual editor to find the block tags that contain the buttons or elements you want to hide

// Hide checkbox column

$xt->assign("checkbox_column",1>2);

// $xt->assign("firstAboveGridCell",1>2);

// hide delete selected button

$xt->assign("deleteselected_link",1>2);

}
ACP
P.S. Please remove your other post in Tips and Tricks forum for the same question or post the question in General Forum.

A
acpan author 8/15/2019

Also it seems more logical you are trying to hide the edit controls in other pages of your app, based on PERMS of user (not at the USR page which you mentoned).
ACP