Creating A Person And Updating Left Column Display
Now that we can create and view projects and to-do items let's look at creating people. People are going to be displayed in the left column along with projects so we will make some changes to the uiPopulateProjectsAndPeople handler in order to accommodate that.
What We Are Going To Do
In order to display both Projects and People in the left column we will create headings (1). Underneath the headings the list of projects or people will appear (2).
Update uiCreatePerson Handler
Replace the uiCreatePerson command in the card script with the script below. There is nothing going on in this handler that you haven't seen before.
----------
Copy & Paste The Following Code
----------
command uiCreatePerson
## Create a SQL Record object for 'people' table
put sqlrecord_createObject("people") into theRecordA
## Set name property of object
sqlrecord_set theRecordA, "name", "New Person"
## Create record in the database
sqlrecord_create theRecordA
put the result into theError
if theError is empty then
## Refresh list, select new record and open field editor
## so that user can change name.
lock screen
RefreshProjectsPeopleList
set the uSelectedPersonID of group "ProjectsPeople" to theRecordA["id"]
set the dgTemplateFieldEditor["select text"] of group "ProjectsPeople" to true
dispatch "EditKeyOfIndex" to group "ProjectsPeople" with "name", the dgHilitedIndex of group "ProjectsPeople"
unlock screen
end if
if theError is not empty then
answer "Error creating person:" && theError & "."
end if
end uiCreatePerson
Test Record Creation In The Database
After compiling the card script you can click on the Add Person button (1). You should see a new record in your database manager for the people table (2). YOU WON'T GET ANY VISUAL FEEDBACK AT THIS POINT.
Update the uiPopulateProjectsAndPeople Handler
Replace the uiPopulateProjectsAndPeople command in the card script with the RevTalk code below. After you insert the code we will go through the relevant parts.
----------
Copy & Paste The Following Code
----------
command uiPopulateProjectsAndPeople
##
## Projects Title
##
put "Projects" into theDataA[1]["name"]
##
## Project Records
##
## Create a SQL Query object
put sqlquery_createObject("projects") into theQueryA
## Specify how results should be sorted
sqlquery_set theQueryA, "order by", "name"
## Retrieve Projects data from database and convert to
## a numerically indexed array. First project record
## will start at theDataA[2] since theDataA[1] is already
## filled in.
sqlquery_retrieveAsRecords theQueryA, theDataA
put the result into theError
##
## People Title
##
if theError is empty then
## item 2 of the extents is the current number of records
## already in the theDataA array variable.
put item 2 of line 1 of the extents of theDataA + 1 into theNextIndex
put "People" into theDataA[ theNextIndex ]["name"]
end if
##
## People Records
##
if theError is empty then
## Retrieve People data from database and convert to
## a numerically indexed array. First person record
## will start at theDataA[ theNextIndex + 1]
put sqlquery_createObject("people") into theQueryA
sqlquery_set theQueryA, "order by", "name"
sqlquery_retrieveAsRecords theQueryA, theDataA
put the result into theError
end if
if theError is empty then
## Assign the array directly to a Data Grid
set the dgData of group "ProjectsPeople" to theDataA
end if
if theError is not empty then
answer "Error populating projects and people:" && theError & "."
end if
end uiPopulateProjectsAndPeople
Aggregating Static Data And Multiple Database Queries Into a Single Array
Since we are going to display both projects and people in the same Data Grid we need to update the uiPopulateProjectsAndPeople handler. SQL Yoga makes it easy to aggregate results across multiple queries when retrieving data as an array or as SQL Record objects.
If you pass a numerically indexed array to sqlquery_retrieveAsRecords, SQL Yoga will append the results to the array. For example, before retrieving projects, the theDataA array variable has a nested array assigned to index [1] (2). Because theDataA already has a numeric index of 1 sqlquery_retrieveAsRecords will start inserting records into the array starting with [2].
The People title string is then appended to the end of the theDataA array variable (3) and then the people records are finally appended as well (4).
In the end you have a single, numerically indexed array that you can assign to a Data Grid.
Test
Open the Message Box and execute the command uiPopulateProjectsAndPeople (1) to see the result (2).
0 Comments
Add your comment