In this article, see how to create a binding to edit a record from a grid used in a capture binding.
We cannot use the edit link in this case, as the edit button will not be available when we access the grid to capture a value.
Taking into account that the capture link is already configured, follow the steps below to configure the grid (used in the capture) and the form (used in editing the record)
Grid Application
In the grid application, we will create a link to edit the record using the
macro sc_link.
Adding the editing image to the application
First we must add the image that will be used in the grid to symbolize the edit button.
Why add images in the Application's Extra Images option?
We must always add images that will be used in codes. This ensures that when publishing, the image will be uploaded and is also stored in the same directory, both in the publication and in the development environment, ensuring that the image is not broken after publication.
1 - In the side menu in the grid application, go to: Application > Settings
2 - In the option Application Images click on the icon to add an image:
3 - In the image manager, click Project > Icons
4 - Click File > Upload and select the icon that will represent the edit record button.
The added images will be stored in this directory ../_lib/img/
When uploading, some prefixes are added to the image name, according to the directory where it will be stored.
Images can be saved in the following directories: Public, Project and User.
- Public - sys__NM__
- Project - grp__NM__
- User - usr__NM__
Within each directory there are some sub-directories: background images, button images, general images, icons and menu images.
- Background images - bg__NM__
- Button images - btn__NM__
- General images - img__NM__
- Icons - ico__NM__
- Menu images - menu_img__NM__
When saving an image with the name contract.png in the Project > Icons directory, as in the tutorial, the image name should look like this:
grp__NM__ico__NM__contract.png
Creating a virtual field
We must create a camp to receive the redirect link to the form.
1 - Click on the New Field option
2 - Define the field name which must be of type text.
3 - Remove the label from the field and set the title alignment as in the image below.
onScriptInit Event
In this event, we will reset the value of the global variable that will be passed to the form.
This way, we guarantee that the variable will always have no value if we click on the record that will be edited.
Code used in the event
- // Resets the global variable that will be passed as a parameter to the form.
- sc_reset_global([id_customer]);
onRecord Event
In the onRecord event, we will define the link using the sc_link macro and the image that will be displayed in the field created earlier, using the img tag.
Code used in the event
The images inserted in the Application Extra Images option will be stored in the ../scriptcase/app/projeto_name/_lib/img directory.
Thus, to assign an image to a field, we must use the img tag informing the image directory and the name like this:
../_lib/img/grp__NM__ico__NM__contract.png
- // Defines the image that will represent the button to edit the record.
- {link} = '<img src="../_lib/img/grp__NM__ico__NM__contract.png">';
- // Remembering that the id_customer parameter will be received as a global variable in the form.
- sc_link (link, form_customers.php, id_customer = {customerid}, "Click here to edit record", "_self");
In the form application, we will add a global variable in the application's SQL in addition to a validation in the onApplicationInit event.
SQL Configuration
In the SQL settings of the form, which will be called to edit the record, add a global variable. In the example, we are using the variable [where]
onApplicationInit Event
In this event, which is executed before the application's SQL assembly, the value of the variable [where] must be defined, so that the form is executed correctly.
It is necessary to validate whether the variable [id_customer], passed as a parameter in the sc_link macro in the grid's onRecord event, has a value.
There will be two possible situations:
- In case the variable [id_customer] is empty - In this situation, the variable [where] must receive an empty sing. This way, the form will return all records normally.
- If the variable [id_customer] has a value - This implies that some record was selected in the grid to be edited. Thus, the variable [where] must receive the query that will display only the record selected in the query. Remembering that we must use a key field to create this link.
Código utilizado no evento
- // Validates if the parameter passed by the query has a value
- if ( empty( [id_customer] )) {
- // If [id_customer] is empty, the variable [where] must receive an empty string, causing the form to display all its records.
- [where] = '';
- // If [id_customer] has a value, it enters else
- } else {
- // In this case, the variable [where] will receive the query to display only the record selected in the grid.
- [where] = "customerid = '[id_customer]' ";
- }
Grid in operation
Grid application, opened from a capture link with the record edit button.