我看了一下,起初认为它应该与其他帖子相似,但是您不按 ID 或名称(文本)排序,而是按其他值排序。为了促进这一点,我创建了一个包含所有国家和城市的对象,然后将它们放入其中。然后我使用 sortOrder 进行排序,该对象作为data-sort 被推送到选项列表中,并使用一个函数进行排序。
我看到了您的currentCity,但是这可能会随着国家/地区的变化而改变?因此,我通过在城市上放置 isDefault:true 来为每个国家/地区设置“默认值”。
我也将国家/地区从列表中推了进来,那里有一个默认值。
这对你的情况来说可能有点矫枉过正,但你可以从中吸取教训。
创建命名空间
var myApp = myApp || {};// create a namespace to use.
添加国家对象:
myApp.countries = [{
"name": "United Kingdom",
isDefault: true,
id: 1,
"cities": [{
id: 1,
name: "Bedfordshire",
"sortOrder": 1,
"isCapital": false
}, {
id: 2,
name: "Berkshire",
"sortOrder": 2,
"isCapital": false
}, {
id: 3,
name: "Birmingham",
"sortOrder": 3,
"isCapital": false
}, {
id: 4,
name: "Brighton",
"sortOrder": 4,
"isCapital": false,
isDefault:true
}, {
id: 5,
name: "Buckinghamshire",
"sortOrder": 6,
"isCapital": false
}, {
id: 6,
name: "Appleton",
"sortOrder": 5,
"isCapital": false
}, {
id: 37,
name: "London",
"sortOrder": 0,
"isCapital": true
}]
}, {
"name": "Hackem",
id: 2,
"cities": [{
id: 1,
name: "Somecity",
"sortOrder": 1,
"isCapital": false
}, {
id: 2,
name: "Waterton",
"sortOrder": 2,
"isCapital": false
}, {
id: 3,
name: "Acre City",
"sortOrder": 3,
"isCapital": false
}, {
id: 4,
name: "Jackson",
"sortOrder": 4,
"isCapital": false
}, {
id: 5,
name: "Tolkenshire",
"sortOrder": 6,
"isCapital": false
}, {
id: 6,
name: "Capital City",
"sortOrder": 0,
"isCapital": true
}, {
id: 37,
name: "Paris",
"sortOrder": 4,
"isCapital": false
}]
}, {
"name": "NewCountry",
id: 3,
"cities": [{
id: 1,
name: "Skycity",
"sortOrder": 1,
"isCapital": false
}, {
id: 2,
name: "DirtCity",
"sortOrder": 2,
"isCapital": false
}, {
id: 3,
name: "Airville",
"sortOrder": 3,
"isCapital": false
}, {
id: 6,
name: "Cape Town",
"sortOrder": 0,
"isCapital": true
}, {
id: 37,
name: "Walla Walla",
"sortOrder": 4,
"isCapital": false
}]
}];
将一些函数添加到命名空间以供使用:
myApp.arrayObj = myApp.arrayObj || {
lookup: function(myArray, searchTerm, property, firstOnly) {
var found = [];
for (var i = 0; i < myArray.length; i++) {
if (myArray[i][property] === searchTerm) {
found.push(myArray[i]);
if (firstOnly) break; //if only the first
}
}
return found;
},
updateCityOptions: function(countries) {
var $cityAll = $("#ListingCity");
// lookup by text
var $countryText = $("#ListingCountry option:selected").text();
var ac = this.lookup(countries, $countryText, "name", true)[0].cities;
// lookup by id (alternate approach)
// var $countryId = $("#ListingCountry option:selected").val();
// var ac = this.lookup(countries, $countryId , "id", true)[0].cities;
if (ac) {
$('#ListingCity option:gt(0)').remove();
var opt = '<option></option>';
var newOptions = "";
$.each(ac, function(k2, v2) {
// var sel = currentCity == v2.name ? ' selected="selected" ' : "";
var sel = v2.isDefault ? ' selected="selected" ' : "";
opt = '<option data-sort="' + v2.sortOrder + '"' + sel + ' value="' + v2.id + '">' + v2.name + '</option>';
newOptions += opt;
});
$cityAll.append(newOptions); //hit the DOM just once
$cityAll.sortOptions(); // sort what we put in
$(".city").fadeIn(300);
} else {
$cityAll.find('option:gt(0)').remove();
$(".city").fadeOut(300);
}
$cityAll.trigger('change');// because it changed
},
setCountries: function(nation) {
var $countries = $("#ListingCountry");
$countries.find('option:gt(0)').remove();
var opt = '';
var newOptions = "";
$.each(nation, function(k2, v2) {
var sel = v2.isDefault ? ' selected="selected" ' : "";
opt = '<option data-sort="' + v2.sortOrder + '"' + sel + ' value="' + v2.id + '">' + v2.name + '</option>';
newOptions += opt;
});
$countries.append(newOptions); //hit the DOM just once
}
};
创建键排序函数:
// sort the select
$.fn.sortOptions = function() {
$(this).each(function() {
var op = $(this).children("option");
op.sort(function(a, b) {
return $(a).data('sort') > $(b).data('sort') ? 1 : -1;
})
return $(this).empty().append(op);
});
}
启动代码:
myApp.arrayObj.setCountries(myApp.countries);
// could be done is way but we trigger the country change which does this
// myApp.arrayObj.updateCityOptions(myApp.countries);
处理国家变更事件:
$('#ListingCountry').on('change', function() {
myApp.arrayObj.updateCityOptions(myApp.countries);
}).trigger('change');
编辑:请注意,如果您只想将"isCapital": true 保留为一个,则可以删除所有"isCapital": false,并且它的工作方式相同。
在这里玩的示例:https://jsfiddle.net/MarkSchultheiss/okk22ovq/2/
编辑:奖金;备用排序选项
// sort the select
$.fn.sortOptionsByText = function() {
$(this).each(function() {
var op = $(this).children("option");
op.sort(function(a, b) {
return a.text > b.text ? 1 : -1;
})
return $(this).empty().append(op);
});
}
// sort the select
$.fn.sortOptionsByValue = function() {
$(this).each(function() {
var op = $(this).children("option");
op.sort(function(a, b) {
return a.value > b.value ? 1 : -1;
})
return $(this).empty().append(op);
});
}
// sort the select **IF** we had a "mostused" data-mostused value
// sort the select
$.fn.sortOptions = function() {
$(this).each(function() {
var op = $(this).children("option");
op.sort(function(a, b) {
return $(a).data('mostused') > $(b).data('mostused') ? 1 : -1;
})
return $(this).empty().append(op);
});
}