Repopulate a combo before showing list of items
Hi - Any help with this would be v much appreciated.
I have a need to re-populated a combo box at the point I click on it. I have made this work in Firefox(3.5) and in Chrome/opera
but I cannot make IE do it without the list closing up again.
Currently I do the following:
- Handle click or mouse down event and make ajax call for new values based on those set in other combos on the page.
- call PopulateCombo() method on return of ajax response (see below)
-
function PopulateCombo(structuredDataField, rowZeroTotal)
{
var fieldIndex = structuredDataField.Index;
var currentSelection = $('#UiSDCombo' + fieldIndex).val().split(',')[0];
$('#UiSDCombo' + fieldIndex).empty();
var optionValue = structuredDataField.Name + '(any)';
$('#UiSDCombo' + fieldIndex).addOption(optionValue + "," + rowZeroTotal, optionValue, false);
var currentExists = false;
for (var n = 0; n < structuredDataField.Values.length; n++)
{
var displayText = structuredDataField.Values[n].DisplayText + "(" + structuredDataField.Values[n].Matches + ")";
var optionValue = structuredDataField.Values[n].DisplayText + "," + structuredDataField.Values[n].Matches;
$('#UiSDCombo' + fieldIndex).addOption(optionValue, displayText, false);
if (currentSelection == structuredDataField.Values[n].DisplayText)
{
currentExists = true;
currentSelection = optionValue;
}
}
if (currentExists)
{
$('#UiSDCombo' + fieldIndex).val(currentSelection);
}
}
For Chrome to be happy I have had to change the approach to the following
-
$('#UiSDCombo' + fieldIndex)[0].length = 0;
var options = '';
for (var n = 0; n < structuredDataField.Values.length; n++)
{
options += '<option value="' + optionValue + '">' + displayText + '</option>';
}
$('#UiSDCombo' + fieldIndex).html(options);
So. Ideas I have had:
1. Try and stop combo opening on mousedown, get data, populate then open it. - Can't work out how to stop it opening initially!
2. Float something else over the combo to trap the click, get the data then repopulate, then open combo - Can't work out how to open it programatically!
3. Do it all with divs - Means that combo is now inconsistent with rest of site and will not be styled in same manner
Anyone got any others or a way of making option 1 or 2 work above
Thanks in advance