... to make it possible to not select (or unselect) a value.
If you want to add a blank to a list that is populated from a service you have 2 options:
1. Add blank to your source list
2. Add the blank option after the service is executed. To do this we use an onCallFinished handler that looks something like the following. The code typically goes in the form onLoad event. Replace the "SC_GetAllStates" with the ID of the service that you are calling, then change the other IDs (i.e. P_NewPage1 and F_DropDown1 as appropriate).
var srv = form.getServiceConfiguration('SC_GetAllStates');
srv.connectEvent("onCallFinished", function(success)
{
if(success) {
try {
var listFld = form.getPage("P_NewPage1").F_DropDown1;
var opts = listFld.getOptions(); //gets the current list of options from the item that were assigned from the service
opts.unshift({title:'',value:''}); //adds to the beginning of the list, use push(...) to add to the end
listFld.setOptions(opts); //sets the options back with the newly added blank
} catch(err) {
alert(err);
}
}
});
Would it not be a usefull option t have some tick box that enables to add a blank option out of the box to any pull down or options lost ? Would make citizen developer's life much easier and the UI better.
Hmmm.... I cannot make that work...
I've tried to add "debugger;" before the getServiceConfiguration() call and inside the "success" block of code - but it never stops for me to see what is going on...
I added the code (similar to your example) in the onLoad of the form and captured the service name from the details of the service...
How can I troubleshoot this issue?
adding the debugger before the getServiceConfiguration does nothing in this case. Remember that this is a listener that gets evaluated and setup when the form is loaded. If you want to debug this then you must place the "debugger" inside the if(success) code block.
Ok, but as I mentioned I also added "debugger;" inside the "success" block - and it didn't trigger. So I must have misplaced something....
And it doesn't throw any errors on the browser JS console. But Ok, I'll have to rip it apart and build it up step by step to see where it breaks.
If you have any other onCallFinished handlers make sure that each uses a unique variable, otherwise they will trample over each other. Also verify that the service ID is correct, it may be that the handler is not properly connected.
Secondly, setting up the handler is not the same thing as triggering the service. Make sure that some event in the form is actually calling the service. Depending on what item is calling it, you may select it from the call service dropdown or you can invoke it with code:
form.getServiceConfiguration(...).callService();
Not sure what was confusing the system last I looked at this. But implemented it today - and it works!
Thanks!