dhtmlxCombo forEachOption has a bug

there seems to be a bug in the foreachoption() if we do a delete on the option inside it

Please see the code

    DACombo.forEachOption(function(avgOption){ //remove any option user does not have permission on
                           if(userAvg.indexOf(parseInt(avgOption.value)) == -1){

In a certain scenario, there were 2 options. The userAvg Array did not have both of them.
So both the options were to be deleted, but it looks like the forEachOption loop exited immediately after the first item was deleted.So the other item remained though as per logic of iteration it should be deleted. Correct ?

What OS, dhtmlx and browser versions do you use?

Hi Darya,

OS = Windows 8.1 ,
Browser = Chrome Version 43.0.2357.134 m (latest)
dhtmlxSuite v.4.3 Professional

Actually i used a workaround.
I generated the combo options array via php (outside the loop) and
used the combo.addoption([array]);
to finally overcome the issue because when i tried to do it via a for(;:wink: loop I understood that each time we delete an option the getoptionscount() will also return 1 less count so the loop will exit without processing the last element(thats why it processed only 1 out of the 2 options). This issue is a unique case of forEachOption() if a deleteoption is done inside the the function because it causes the loop to terminate prematurely because of the option count reduced within the loop(don’t know if there is a bug like this if someone tries to add an option inside forEachOption(). Possibly it might cause an infinite loop ???.


correct. just correct all opts and delete them after loop

var opts = []; combo.forEachOption(function(opt){ if (...) opts.push(opt.value); }); for (var q=0; q<opts.length; q++) combo.deleteOption(opts[q]);