I´m new to Servoy, just one week playing with it. I use to work with Visual Foxpro and I want to develop my new applications with Servoy.
I would like my forms work with the logic that I use to use. When the forms is shown the fields are empty and disabled, except the PK. The user can type a PK in it´s fields, then the form search for that PK. If it exists the record is shown, the PK beccomes disabled and the rest of the fields become enabled. After the user saves data or cancels edition, an empty record is shown again, the fields become disabled and the PK enabled.
I guess I know how to enable/disable fields depending on the status of the form (editing/not editing), but I do not know how to get an empty record to be shown.
If you go to find mode (controller.find()), you will get this behaviour. When you enter find mode, an empty record is shown, you should disable all the fields except the pk yourself in a script. If the user enters a pk and presses enter, the record is searched. You can use the onSearchCmd-event to enable your fields again.
Thanks for your reply. That way I get an empty record. Now I need to know the order that events are executed. I mean what happens first: the onChangeData of the element the command onSearch of the form? I ask this because before the search is performed I have to format the typed data (for example the PK is a text field that will allow only nums. it is formatted with zeros at the left but if the user types 43.1 I have to change the “.” with zeros to complete the lenght of the field resulting 4300000001). Then the search can be performed and depending on the result I can add a new record or edit the found one.
I guess that with a code like this:
var recFound = controller.search(false, false)
if (recFound=0){
controller.newRecord()
}
// Now enable the fields
enableFields()
controller.focusField(controller.getTabSequence[2],false)
The form is shown with an empty record and all fields disabled but the PK
The focus is set on the PK
After the user types a PK a search is performed
If no records are found means the user wants to add a new one
If a record exists it´s shown and the user can edit.
To do so I have this script for the form.
/**
function onShow(firstShow, event) {
if (firstShow)
{
// Disable fields of the record except the PK
_super.disableFields()
// Get an empty record and set focus to the first field (PK)
controller.find()
controller.focusFirstField()
}
}
function onSearch(clear, reduce, event) {
// Check if the user typed something like 4.2 and fills with 0
// to get a string like 4000000002
var pointPos=codigo.indexOf('.');
if (pointPos>=0){
var ceros=globals.Replicate('0',(10-codigo.length)+1)
codigo=codigo.substr(0,pointPos) + ceros + codigo.substr(pointPos+1)
}
// Format the string with 0 on the left
codigo=globals.Replicate('0',10-codigo.length)+codigo
// Perform the search
var found = controller.search(false, false)
if (found=0){
// If no records found then create a new one
controller.newRecord(false)
}
// Enable fields and disable the PK
_super.enableFields()
// Set the focus to the second field (the one after the PK)
var tabseq = controller.getTabSequence();
controller.focusField(controller.getTabSequence[2],false)
}
_super.enableFields and _super.disableFields are methods created on a super form that enables/disables the fields of the form. global.Replicate is a global method that returns a string with a replication of a character a number of times.
I do not know why but even no records are found, the lines inside the if are not being executed. I can see in the console the value of found and it is 0.0. Why is this? What am I missing?
Sorry Johan you are right. I did no explain myself.
I was meaning the lines inside the if found=0.
Anyway I got it working now. After reading exmaple code I did some modifications and it´s working. Here is the final code for the onSearch command:
function onSearch(clear, reduce, event) {
// Check if the user typed something like 4.2 and fills with 0
// to get a string like 4000000002
var pointPos=codigo.indexOf('.');
if (pointPos>=0){
var ceros=globals.Replicate('0',(10-codigo.length)+1)
codigo=codigo.substr(0,pointPos) + ceros + codigo.substr(pointPos+1)
}
// Format the string with 0 on the left
codigo=globals.Replicate('0',10-codigo.length)+codigo
var tmpCodigo=codigo
codigo='='+codigo
controller.newRecord()
// Perform the search
var foundRecords = controller.search(true, false)
if (foundRecords == 0){
// If no records found then create a new one
controller.newRecord(false)
}
codigo=tmpCodigo
databaseManager.setAutoSave(false)
// Enable fields and disable the PK
controller.focusField('fld_dni',true)
globals.enableFields()
// Set the focus to the second field (the one after the PK)
}
I do not understand the need of the sentence controller.newRecord() after assigning the string to search and before the ```
controller.search