Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Barb on Sep 02, 2009 00:41
open dhtmlx forum
Updating dhtmlxCombo Options from DB

3 questions.

1. the onChange event on my combos doesn't seem to trigger until I click on another element, is there some way I can make it trigger as soon as I move the mouse out of the combo input area similar to the OnBlur event?

2. I have multiple combos on a page which have the same option list.

I have successfully used dhtmlAjax to add any new options entered to my db.

Now I am trying to add the new option to the combo updated and the other duplicate combos.

I am building the array to rebuild the options in the php page I use to update the db, and have checked that loader.xmlDoc.responseText returns the following:

[[1,'Baking'],[2,'Beverage'],[3,'Breakfast'],[4,'Christmas'],[5,'Cleaning Product'],[6,'Confectionery'],[7,'Dessert'],[8,'Entree'],[9,'Main Meal'],[10,'Marinade'],[11,'Microwave'],[12,'Side Dish'],[13,'Snack'],[14,'Test'],[15,'Test1'],[16,'Test2'],[17,'Uncategorised']]

I thought I could plug this into addOption and rebuild the option list as in the code below, but it doesn't work. THe option list gets cleared, but the new options don't appear. Am I on the wrong track?

<table>
<tr width=100>
<td><strong>Category*</strong><br>
<select id=cat0 name=catarray[0][0]>
<option></option>
<option>Baking</option>
<option>Beverage</option>
<option>Breakfast</option>
<option>Christmas</option>
<option>Cleaning Product</option>
<option>Confectionery</option>
<option>Dessert</option>
<option>Entree</option>
<option>Main Meal</option>
<option>Marinade</option>
<option>Microwave</option>
<option>Side Dish</option>
<option>Snack</option>
<option>Test</option>
</select>
<script>
function outputResponse(loader){
c0.clearAll();
c0.addOption(loader.xmlDoc.responseText);
c1.clearAll();
c1.addOption(loader.xmlDoc.responseText);
}
var c0=dhtmlXComboFromSelect("cat0");
c0.enableFilteringMode(true);
c0.attachEvent('onChange',function(){
var value = c0.getActualValue();
var field = "category";
if (value!=null) dhtmlxAjax.post("ajax.php","field="+field+"&othervalue="+value,outputResponse);
})
</script>
</td>
</tr>
</table>



3. Can I omit the value field when using addOption, as I don't need it?




Answer posted by Alex (support) on Sep 02, 2009 04:32

>> the onChange event on my combos doesn't seem to trigger until I click on another element, is there some way I can make it trigger as soon as I move the mouse out of the combo input area similar to the OnBlur event?

onChange occurs when enter key is pressed or you press somewhere and combo loses focus.

There is onBlur event too.

>> THe option list gets cleared, but the new options don't appear

addOption method requires array, not string. Combo can be also loaded  from xml file or string: loadXML(url), loadXMLString(string)

>> Can I omit the value field when using addOption, as I don't need it?

No, you shouldn't do that

Answer posted by barb on Sep 02, 2009 18:06
Thanks.

If I want to use loadXMLString, what do I have to return from the php script and what JS code do I need to use to load the options?
Answer posted by Alex (support) on Sep 03, 2009 00:42

loadXMLString requires xml string. For example:

combo.loadXMLString("<complete><option value='1'>one</option><option value='2'>two</option></complete>")

Answer posted by barb on Sep 03, 2009 23:21
This is all working perfectly now -thanks!!

However, just to let you know - using the xml string format you provided I ended up with both the value and the text being added to my option list. To correct this I omitted the value parameters like this

combo.loadXMLString("<complete><option>one</option><option>two</option></complete>")

Answer posted by Alex (support) on Sep 04, 2009 03:19

value attribute is obligatory. So, you should add it to use combo API.

Answer posted by barb on Sep 04, 2009 18:35
That being the case, how can I stop the value being added to my option list as stated above?
Answer posted by Alex (support) on Sep 06, 2009 23:58

>> how can I stop the value being added to my option list as stated above?

the value attribute is obligatory. It must be used. There is no possibility to change this behaviour.