在我的Razor视图中,我有一个包含3个未显示属性的表。 Id_Loaction
,Id_Level
& Id_Section
在此表格上方,我有3个复选框列表,每个位置,级别和&带有适当ID的部分作为值。
@foreach (var section in Model.Sections)
{
<li>
<input type="checkbox" data-bind="checked: data" value="@Html.Encode(section.Value)"/>@Html.Encode(section.Text)
</li>
}
注意:section.Value
是元素的ID,也用于表行
表格本身相当简单:
@foreach (var item in Model.Offers)
{
<tr data-bind="visible: data.IsChecked == true ">
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location)
</td>
<td>
@Html.DisplayFor(modelItem => item.Section.Text)
</td>
<td>
@Html.DisplayFor(modelItem => item.Section.Value)
</td>
<td>
@Html.DisplayFor(modelItem => item.Section.IsChecked)
</td>
<td>
@Html.DisplayFor(modelItem => item.Level)
</td>
<td>
@Html.ActionLink("Details", "Details", new { id=item.ID })
</td>
</tr>
}
<script type="text/javascript">
var data = @Html.Raw(Json.Encode(Model.Sections));
function FormViewModel(data) {
this.data = ko.observableArray(data);
}
window.viewModel = new FormViewModel(data);
console.log(viewModel.data());
ko.applyBindings(viewModel);
</script>
/编辑: 我已将代码更新为实际的混乱。 Model.Sections是一个ViewModel,它与用于Model.Offers.Section的ViewModel相同。两者都包含Value,Text和IsChecked属性。
如何比较这两者并仅显示检查该部分的表格行?
请慢慢对我说。 亲切的问候
答案 0 :(得分:4)
这是尝试将淘汰赛与我假设Serv试图做的精简版本一起使用。这是对象列表上的简单过滤器的示例,可以轻松扩展以包含更多过滤器和更复杂的模型。
小提琴 http://jsfiddle.net/zTRq2/3/
简单过滤器ViewModel:
function filterViewModel(data) {
var self = this;
self.text = ko.observable(data.text);
self.checked = ko.observable(data.checked);
}
修剪优惠视图模型:
function offerViewModel(offer) {
var self = this;
self.description = offer.description;
self.location = offer.location;
}
主视图模型:
function FormViewModel(data) {
var self = this;
// map data from server to locations filter
self.locations = ko.observableArray(ko.utils.arrayMap(data.locations, function (filter) {
return new filterViewModel(filter);
}));
self.offersView = ko.computed(function () {
// get list of offers that were passed in, we'll manipulate filteredOffers from now on
var filteredOffers = data.offers;
// get all selected locations view models
var selectedFilterLocations = ko.utils.arrayFilter(self.locations(), function (location) {
return location.checked();
});
// run through util function to filter only offers that match any selected location
filteredOffers = ko.utils.arrayFilter(filteredOffers, function (offer) {
var matchesLocation = false;
ko.utils.arrayForEach(selectedFilterLocations, function (location) {
if (offer.location === location.text()) matchesLocation = true;
});
return matchesLocation;
});
return filteredOffers;
});
};
HTML:
<!-- list of location checkboxes -->
<ul class="unstyled inline" data-bind="foreach: locations">
<li>
<input type="checkbox" data-bind="checked: checked" /> <span data-bind="text: text"></span>
</li>
</ul>
<!-- table which is bound to the offersView computed observable -->
<!-- offersView is recalculated everytime a selection changes from the filter list -->
<table class="table-bordered">
<tbody data-bind="foreach: offersView">
<tr>
<td data-bind="text: description"></td>
<td data-bind="text: location"></td>
</tr>
</tbody>
</table>
答案 1 :(得分:2)
除非必须,否则我不会使用jQuery。
knockout提供了你对foreach方法的看法。使用它和if语句或可见绑定。
<ul data-bind="foreach: entity">
<!-- ko if:checked() -->
<!-- display your Li here -->
<li></li>
<!-- /ko -->
</ul>
或者你可以删除ko if less less语句并使用以下代码
<li data-bind="visible: checked()"></li>
但请记住,如果检查等于真,那两个测试都会说。如果您的值是字符串,则需要针对字符串进行测试。
修改强>
如果您有一些要测试的ID或值,可以这样做 -
<li data-bind="visible: section.checked() === true"></li>
或其他一些逻辑测试
答案 2 :(得分:2)
如果检查了tr,则需要将tr的样式设置为none(如果只显示选中的值,则设置为非'none'值),这就是全部 例如:
var viewModel= function(){
var self=this;
self.tableData=ko.observableArray(),
init=function(){
for(var i=0;i<5;i++){
//Put all your data here
var data={
selected:ko.observable(false),
id:ko.observable('loc'+i),
desc:'desc'+i
};
self.tableData.push(data);
}
};
init();
}
ko.applyBindings(new viewModel());
然后HTML就像
<div data-bind="foreach:tableData">
<input type="checkbox" data-bind="checked:selected"/><span data-bind="text:id"></span>
</div>
<table>
<thead>
<th>Location</th>
<th>Desc</th>
</thead>
<tbody data-bind="foreach:tableData">
<tr data-bind="{style:{display:selected()?'none':''}}">
<td data-bind="text:id"></td>
<td data-bind="text:desc"></td>
</tr>
</tbody>
</table>
这里是jsfiddle: http://jsfiddle.net/Dhana/keT7B/
答案 3 :(得分:1)
按“行”我假设你的意思是李。作为jQuery库一部分的each()
方法用于遍历数组,在本例中是由jQuery选择器$('input[type="checkbox"]')
返回的匹配元素数组。
$('input[type="checkbox"]').each(function(){
if(this.checked)
$(this).closest('li').hide()
})
答案 4 :(得分:1)
如果你:
然后你可以 - jsfiddle:
$('#sectionslist input').change(function() {
$('#offersTable .offerId' + $(this).val()).toggle();
});
答案 5 :(得分:1)
我猜你只是想做一些简单的客户端过滤?看看这个小提琴:http://jsfiddle.net/dzul1983/e6ADe/2/
由于会有很多部分,因此在JS中对它们进行硬编码并不理想。相反,您可以使用.NET在TR元素中输出它们。
e.g。 (如果你使用我的小提琴)
<tr data-bind="visible: isChecked('@Html.Encode(item.Section.Value)') ">