$('#autocomplete').ejAutocomplete({
dataSource: states,
fields: { key: "index" ,text: "countryName" },
watermarkText: "Select a Code",
width: "100%",
filterType: 'contains',
showPopupButton : true
}); |
$('#dropdownlist').ejDropDownList({
dataSource: states,
width: "100%",
watermarkText: "Select a Code",
fields: { text: "countryName", value: "index" },
enableFilterSearch: true
});
|
Hi Prince
thanks for the quick reply!
This does the trick for the filtering, however not quite yet for the sorting
in your example if you type G
the order of the filtered results should be
GB (United Kingdom)
DE (Germany)
i.e the sort order should first show the matches on the iso code ( sorted alphabetically) and then the matches on the country name
In select2 one can achieve this with a custom filter and a custom sort function. In the custom filter function one would determine the kind of match ( iso code or country name) and then in the custom sorter function use this to make the iso match come before the country match
something like this
is this possible with the dropdownlist control ?
<script type="text/javascript" class="jsScript">
ej.DropDownList.prototype._filterSearch = function (searchQuery, args) {
var flag = false;
this.resultList = args.result ? args.result : ej.DataManager(this._rawList).executeLocal(searchQuery);
if (this.resultList.length == 0) {
flag = true;
this.resultList.push(this._getLocalizedLabels("emptyResultText"));
}
this.resultList = customSort(this.resultList);
this.popupListItems = this.resultList;
this.ultag.empty();
this._isPlainType(this.popupListItems) ? this._plainArrayTypeBinding(this.resultList) :
this._objectArrayTypeBinding(this.resultList, "search");
if (flag && this.ultag.find("li").length == 1) {
this.ultag.find("li").eq(0).addClass("e-nosuggestion");
}
if (this.model.showCheckbox && !flag) {
this._appendCheckbox( this._getLi());
}
this._onSearch = true;
var value = this.value(), visibleText = this._visibleInput[0].value;
this._setValue(this.value());
var checkVal = typeof this.model.value === "function" ? this.model.value() : this.model.value;
if(checkVal != value){
this.element[0].value = value;
this._visibleInput[0].value = visibleText;
this.model.text = visibleText == "" ? null : visibleText;
if (this.value() != value && !(this.value() == null && value =="" )) {
this._updateValue(value);
}
}
this._onSearch = false;
this._updateSelectedIndexByValue(this.value());
this._refreshScroller();
this._setListPosition();
};
function customSort(data){
return data.sort((a, b) => {
if (a.index > b.index) return -1
if (a.index == b.index) {
if (a.countryName > b.countryName) return 1
if (a.countryName == b.countryName) return 0
return -1
}
return 1
});
}
</script> |