Monday, August 1, 2011

Clear cascaded Lookup DropDown controls

I think, the best way to implement cascaded lookups in SharePoint is SPCD. This is a JavaScript based solution, powerful and flexible.  However, by default, the child level DropDown controls contains all items defined in the list. These items is filtered once user change the value of the parent level DropDown control, but it still may confuse end users.

Below is a simple solution, which is also based on JavaScript.

<script type="text/javascript">
function clearDropDown(LookupFieldTitle)
{
    if(getField('select',LookupFieldTitle))
    {
        //if lookup has 19 or less items - SELECT
        theDropDown = getField('select',LookupFieldTitle);
theDropDown.options.length = 0;

var opt = document.createElement("option");
opt.text = '(None)';
        opt.value = '';
theDropDown.add(opt);
    }
else
{
theDropDown = getField('input',LookupFieldTitle);
theDropDown.choices = '(None)|0';
}
}

var ccd3 = new cascadeDropdowns('Category', 'Sub Category', 'refCategory', 'refSubCategory', 'Title');

clearDropDown('Sub Category');
</script>


The function cascadeDropdowns() and getField() come from spcd.js.

1 comment: