Deleting records with the run button

In this example we will see how to create a button in the application of Grid to delete all the selected records.

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 “Delete” 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]; Keeping the key in a local variable
[total_chked][$arr]={ContactName}; Assigning the Contact Name to the Array
[i]++; Increasing the key to access the next Array position
sc_exec_sql(“delete from customers where CustomerID = ‘{CustomerID}’”); sc_exec_sql ({field}/“sql command”, “connection”) 
This macro allows to condition the circumstances in which the SQL commands are executed.
The “connection” parameter is optional. Required only, if the command is executed in a data base different from the application.
Event Button: OnFinish
$tot = count([total_chked]); Counting the total of checked records.
$contacts = $tot." Selected Contacts: “; | Will keep the message to be displayed | | for($x=0;$x<$tot;$x++){ $contacts .= [total_chked][$x]; if($x == ($tot-1)){ $contacts .=”.“; }else{ $contacts .=”, “; } } | /* This code will check who is the last selected value to add a dot, else add a comma */ | | echo”<table width=‘300px’ border=‘1’ bordercolor=‘#000000’ cellpadding=‘0’ cellspacing=‘0’> Display the message with all selected contacts
<td colspan=‘2’><strong> ($tot) Selected Contacts: <strong></td>
<td><font color=‘#003366’><strong> $contacts </strong></font> </td>
</table>“; | |

6. Now we should access “Application>>Global Variables” to set [i] and [total_chked] to be “OUT” variables.

Tutorials in the same category

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 ...

Fixed Columns in the Summary

In this example, we will see the option to fix columns in the Summary module of the Grid applicatio...

Comment this post