PDA

View Full Version : Checkbox problem



meathome
May 6th, 2010, 10:25
Hi,

I am using the default checkbox template and run into some problems.

The idea is that I have a number value under witch sime checkboxes are displayed. When the value of a checkbox is changed it either adds or substracts 5 from the value depending if its checked or unchecked.

The 3 problems I have:

1.) The onValueChanged() function of the checkbox is called when first opening the checkbox.

2.) getState always return null no matter what state the checkbox is in

3.) the checkbox value is always saved in a databasenode called the same as the checbox and as a child of the charsheet.id-xxxx node even if i set a different source

Heres my code:


<sheetnumber name="ws" source="characteristics.ws.score">
<anchored>
<to>wsframe</to>
<position>insidebottomleft</position>
<offset>18,10</offset>
<size>
<width>36</width>
<height>25</height>
</size>
</anchored>
</sheetnumber>

<checkbox name="wstrained1" source="characteristics.ws.trained1">
<anchored>
<to>ws</to>
<position>belowleft</position>
<offset>0,0</offset>
<size>
<width>12</width>
<height>12</height>
</size>
</anchored>
<script>
function onValueChanged()
if wstrained1.getState()==1 then
window.ws.setValue(window.ws.getValue()+5);
else
window.ws.setValue(window.ws.getValue()-5);
end
end
</script>
</checkbox>


What am I doing wrong? I think this should be a simple matter but I cant figure it out.

Another question: Is there a way to set the value of a numberfield without it calling onValueChanged? Because I want to reset all the checkboxes if the user enters a value into the numberfield and I dont want the resulting change in the checkboxes casue a change in the value.

Zeus
May 6th, 2010, 13:42
I could be mistaken but I think the getState() method for the checkbox control (built on an iconcylcer) returns true or false as opposed to a 1 or 0.

try replacing:



function onValueChanged()
if wstrained1.getState()==1 then
window.ws.setValue(window.ws.getValue()+5);
else
window.ws.setValue(window.ws.getValue()-5);
end
end

with


function onValueChanged()
if wstrained1.getState() then
window.ws.setValue(window.ws.getValue()+5);
else
window.ws.setValue(window.ws.getValue()-5);
end
end


As for changing the value of a numberfield without it calling an onValueChanged handler, I believe you may only be able to do this if you unhook the onValueChanged handler to a dummy method that does nothing. Having said that this would disable onValueChanged events for the numberfield entirely. Not what you want I believe.

How about checking the value of the of the numberfield for said value inside of onValueChanged.

meathome
May 6th, 2010, 17:22
Thanks for your suggestion.

I managed to solve it in a different way. I added the database nodes where the checkboxes stored their value as source to the numberfield.

Then I can simply call onSourceUpdate() and it now works like I wanted.