Skip to content

Instantly share code, notes, and snippets.

@dudelis
Last active December 2, 2017 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dudelis/24dee76d620ff454d7b3ba84f8b421f2 to your computer and use it in GitHub Desktop.
Save dudelis/24dee76d620ff454d7b3ba84f8b421f2 to your computer and use it in GitHub Desktop.
How to remove duplicates from a dropdown #k2 #k2-blackpearl #k2-smartforms #js
//In order to make it work you need to replace YourControlName with the name of your dropdown.
//This piece of code assumes, that you have only 1 dropdown on the form with this name. In any case only the first instance will be taken.
var dropdownControlPath = "Controllers/Controller/Controls/Control[@Name='YourControlName']";
var dropDownControlControl = $sn($xml(__runtimeControllersDefinition), dropdownControlPath);
var contrId = dropDownControlControl.getAttribute('ID');
var ddl = $('#' + contrId + '_DropDown');
var items = $(ddl).dropdown('option', 'items');
for( var i = 0; i < items.length; i++){
for( var x = i+1; x < items.length; x++){
if( items[x].text == items[i].text ){
items.splice(x,1);
--x;
}
}
}
$(ddl).dropdown('option', 'items', items);
$(ddl).dropdown('refresh');
//For this code to work, you need to send it to a Literal DataLabel after your control is populated.
//Certainly, the piece of code needs to be wrapped into `<script>...</script>` tags.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment