Last active
December 8, 2015 23:17
-
-
Save raykendo/6418dbc9750b22e2069c to your computer and use it in GitHub Desktop.
David Walsh's Display Unique Combobox (https://davidwalsh.name/unique-combobox) updated for AMD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An example using the UniqueComboBox defined above. This one creates a combobox with a datastore defined by | |
* dojo/store/Memory and dojo/store/Observable. The UniqueComboBoxMenu provides a way to remove duplicate values | |
* from the list of results returned. | |
*/ | |
require([ | |
"dojo/store/Memory", | |
"dojo/store/Observable", | |
"dijit/form/ComboBox", | |
"davidwalsh.form._UniqueComboBoxMenu", | |
"dojo/domReady!" | |
], function (Memory, Observable, ComboBox, _UniqueComboBoxMenu) { | |
var dataStore = new Observable(new Memory({ | |
idProperty: "id", | |
data: [] | |
})); | |
var combo = new ComboBox({ | |
store: dataStore, | |
dropDownClass: _UniqueComboBoxMenu | |
}, "myNode"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An update on David Walsh's Unique Combobox | |
* Updated for Dojo AMD (tested at v. 1.10) | |
* Originally posted: https://davidwalsh.name/unique-combobox | |
*/ | |
define("davidwalsh.form._UniqueComboBoxMenu", | |
["dojo/_base/declare", "dojo/_base/array", "dijit/form/_ComboBoxMenu"], | |
function (declare, arrayUtils, _ComboBoxMenu) { | |
return declare([_ComboBoxMenu], { | |
createOptions: function (results, dataObject, labelFunc) { | |
// Cycle through to find uniques | |
var uniqueKeys = {}, uniqueItems = []; | |
arrayUtils.forEach(results,function(result,index) { | |
var label = labelFunc(result); | |
if(typeof label != "string") { | |
label = label.label; | |
} | |
if(!uniqueKeys[label]) { | |
uniqueKeys[label] = result; | |
uniqueItems.push(result); | |
} | |
}); | |
// Update arguments arr | |
arguments[0] = uniqueItems; | |
// Call inheritance chain for this method, return result | |
return this.inherited(arguments); | |
} | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment