Javascript 如何在每个表行上放置click事件,并将结果作为参数传递,以将新数据绑定到另一个源?
我有以下代码,用于填充Javascript 如何在每个表行上放置click事件,并将结果作为参数传递,以将新数据绑定到另一个源?,javascript,jquery,knockout.js,knockout-2.0,Javascript,Jquery,Knockout.js,Knockout 2.0,我有以下代码,用于填充select options,并将所选值传递给视图模型中的另一个函数,该函数使用与所选值匹配的数据填充表格: 简言之,如果用户从select(选择)选项中选择ASIA(亚洲),则亚洲的所有国家/地区都绑定到该表: 如何向表中的每一行添加单击事件,以便将单击行的CountryId作为参数传递给视图模型中的另一个函数?我需要使用参数和函数来执行其他绑定。例如:国家/地区详细信息 这就是我到目前为止所做的: <div id="country-select">
select options
,并将所选值传递给视图模型中的另一个函数,该函数使用与所选值匹配的数据填充表格:
简言之,如果用户从select(选择)选项中选择ASIA(亚洲),则亚洲的所有国家/地区都绑定到该表:
如何向表中的每一行添加单击事件,以便将单击行的CountryId
作为参数传递给视图模型中的另一个函数?我需要使用参数和函数来执行其他绑定。例如:国家/地区详细信息
这就是我到目前为止所做的:
<div id="country-select">
<select data-bind="options: UniqueContinent,
value: SelectedContinent"></select>
</div>
<table id="country-list">
<tr>
<th>CountryID</th>
<th>Country Name</th>
<th>City</th>
<th>Continent</th>
<th>CountryAbbr</th>
</tr>
<tbody data-bind= "foreach: FilteredEntries">
<tr>
<td data-bind="text: CountryId"></td>
<td data-bind="text: Country"></td>
<td data-bind="text: City"></td>
<td data-bind="text: Continent"></td>
<td data-bind="text: CountryAbbr"></td>
</tr>
</tbody>
</table>
只需将单击处理程序添加到
:
请参阅更新的和
function ViewModel() {
var self = this;
self.CountryData = ko.observableArray([
{
"City": "KABUL",
"Continent": "ASIA",
"Country": "AFGHANISTAN",
"CountryAbbr": "AF",
"CountryId": "102120"
},
{
"City": "DHAKA",
"Continent": "ASIA",
"Country": "BANGLADESH",
"CountryAbbr": "BD",
"CountryId": "102136"
},
{
"City": "BRUSSELS",
"Continent": "EUROPE",
"Country": "BELGIUM",
"CountryAbbr": "BE",
"CountryId": "102139"
},
{
"City": "MINSK",
"Continent": "EUROPE",
"Country": "BELARUS",
"CountryAbbr": "BY",
"CountryId": "102138"
}]);
self.SelectedContinent = ko.observable('');
self.UniqueContinent = ko.dependentObservable(function() {
var continent = ko.utils.arrayMap(self.CountryData(),
function(item){
return item.Continent
})
return ko.utils.arrayGetDistinctValues(continent).sort();
});
// Call this function when changes are made
self.FilteredEntries = ko.computed(function() {
return ko.utils.arrayFilter(self.CountryData(), function(item) {
// I need to use the selected value
return item.Continent === self.SelectedContinent();
});
});
}
ko.applyBindings(new ViewModel)
<tr data-bind="click: $root.CountryDetails">
self.CountryDetails = function(country)
{
doSomethingWithCountryId(country.CountryId);
}
function doSomethingWithCountryId(countryId)
{
// ...
}