Tutorial: Updating Fields with the Run Button in a Grid Application

This option in the Grid allows to create a button to process a PHP code in selected records (checkbox).


Creating a new button

1. Performing actions on multiple rows of a Grid application can be done creating a new button in the Grid.


2. We will name the field “Process” and select the type “Run”.


3. This kind of button has 2 events to process PHP Codes.

  • onRecord: runs to each record that was selected.
  • onFinish: runs after process all selected records.

4. In the code of our button we need some global variables, then, first, let’s create these variables accessing the onScriptInit event in the application menu…


…and use the code below

Event Grid: OnInti
[i] = 0; It will be our Array Key
[total_chked] = array(); Array where we will keep all selected values

5. Now we can use [i] and [total_chked] in the onRecord and onFinish of the Run button.

Event Button: Onrecord

$arr=[i]; 
[arr_vl][$arr]={ProductID}; 
[i]++; 
$vl_discount = {UnitPrice} - {UnitPrice}* 0.05; 
sc_exec_sql("UPDATE products SET TotalValue = $vl_discount WHERE ProductID = {ProductID}");

Event Button: OnFinish

[tot] = [arr_vl];

6. Access the menu “Layout >> Header and Footer” and enable the footer view, because we will use the application onFooter event.


7. Now let’s use the event onRecord of the application to change the color of the field


Event Grid: onRecord

if(!empty([tot])){ 
$total=[tot]; 
if (in_array({ProductID}, $total, true)) { 
sc_field_style('TotalValue', '#FF0000'); 
}else{ 
sc_field_style('TotalValue', ''); 
} 
}

8. And onFooter event of the application to clear the array.


Event Grid: onFooter

[tot]= array();

9. Now we must access the “Application >> Global Variables” menu to set [tot], [arr_vl] and [i] as variable “OUT”.


10. Run the application, select the records and click on the button “Discount”.

Tutorials in the same category

Tutorial: Evaluation Field in Forms

In this tutorial, you will see how to create a Form using the evaluation fields. Creating the Form ...

Tutorial: Grid fixed columns

In this sample we’ll see the option fixed column on the grid. This way we’ll be able to do a hor...

Tutorial: New Refined Search

In this sample we’ll develop one grid using the option refined search, that will allow us to searc...

Tutorial: Grid Mobile

In this sample, we’ll see the configuration of one grid application for mobile. Creating a ...

Comment this post