Updating a To-Do Items Completed State

We are going to look at one more example of updating a record before moving on. This lesson will show how to update the completed property for a to-do item and explains how boolean values work with SQL Record objects.

Edit ToDo Data Grid Behavior

Select the ToDo Data Grid and open the Object Inspector. Click on the Row Behavior... button to open the behavior script for the Data Grid.

Locate the mouseUp Handler

You will find a mouseUp handler in the script that looks like this. Basically the Data Grid looks for clicks on the checkbox button and updates an internal Data Grid value to the current value of 'the hilite' of the button. This is where we will add the SQL Yoga code.

Update the mouseUp Handler

Replace the mouseUp handler in the behavior script with the following RevTalk code.

----------

Copy & Paste The Following Code

----------

on mouseUp pMouseBtnNum

if pMouseBtnNum is 1 then

## See if the user clicked on the "completed" button

if the short name of the target is "completed" then

## Find to-do item in database that was clicked on

sqlrecord_find "todo_items", the uSelectedID of the dgControl of me, \

theRecordA

put the result into theError

if theError is empty then

## Update "completed" value for record

put the hilite of button "completed" of me into theStatus

sqlrecord_set theRecordA, "completed", theStatus

sqlrecord_update theRecordA

put the result into theError

end if

## Update Data Grid

if theError is empty then

SetDataOfIndex the dgIndex of me, "completed", theStatus

## If UI is hiding completed items and this is complete

## then refresh display

if the hilite of button "HideCompleted" and theStatus then

## Since this row is going to disappear and it is

## executing code we have to use send in time or

## the engine will complain.

send "RefreshToDoList" to me in 0 seconds

end if

end if

end if

end if

if theError is not empty then

answer "Error updating completed status:" && theError & "."

end if

pass mouseUp

end mouseUp

The Code

The only thing I want to point out in the code you will paste in is in relation to how Boolean values are handled when you work with SQL Record objects.

In the database the completed field in the todo_items table is of type Boolean and is stored as 1 or 0. Note, however, that the code sets the "completed" field to the hilite of button "completed". The hilite property of a button returns 'true' or 'false'. When modifying records in the database SQL Yoga will convert 'true' or 'false' to 1 or 0 respectively. When retrieving records from the database SQL Yoga will convert values to 'true' or 'false'. This is done because Revolution uses the true/false strings for boolean values. Note that this is only done when working with SQL Record objects.

Test the UI

Click on the checkbox next to the to-do item.

Verify in the Database

Look at the todo_items records in the database manager. completed should now have a value of '1'.

0 Comments

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.