Last active
September 10, 2015 14:13
-
-
Save nkabrown/30317612b64cee955346 to your computer and use it in GitHub Desktop.
Reusable function to create data agnostic select filters
This file contains hidden or 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
// on function call autocreate data agnostic filters | |
function createFilter(name, data) { | |
var items = []; | |
for (i in data) { | |
items.push(data[i][name]); | |
} | |
var list = items.filter(uniqueValues); | |
list.sort(); | |
$.each(list, function(i, item) { | |
$('#filter-' + name).append($('<option class="options">' + item + '</option>').val(item)); | |
}); | |
// return only unique values | |
function uniqueValues(value, index, self) { | |
return self.indexOf(value) === index; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment