【发布时间】:2015-10-03 01:28:59
【问题描述】:
好的,到此为止。
问题 1: 我想根据选中的单选按钮显示或不显示 tr 元素。共有 3 个按钮,分别显示“解锁成就”、“锁定”和“全部”(锁定 + 解锁)。
下面的代码显示了当我尝试调用三个不同的函数时的外观,每个函数都将 tr:s 可见性设置为 true/false,具体取决于选中的单选按钮。 (D.R.Y,我知道,但现在我只是在寻找功能)。
问题 2: 使 for 循环运行。 itemsListForFilter 是全局声明的,在过滤器函数之外。 itemsListForFilter 是一个对象 arrayMap 的副本,它在代码的其他地方被初始化和填充。该数组包含具有 - 除其他外 - 布尔值“radioCheck”的项目,默认值为“true”,我想检查它。
当我在复制发生的函数中访问 itemsListForFilter 时,它充满了项目,但是...... 当我尝试在过滤器函数中访问 itemsListForFilter 时,它显示为具有 null 值。所以副本在某处“丢失”了:)
查看/HTML:
<div class="widget-header-container">
<h3 class="widget-header">Achievements</h3>
<div class="wrapper">
<input type="radio" name="appliedFilter" value="all" data-bind="checked: filterAll"/><label for="all">Show all</label>
<input type="radio" name="appliedFilter" value="unlocked" data-bind="checked: filterUnlocked"/><label for="unlocked">Unlocked</label>
<input type="radio" name="appliedFilter" value="locked" data-bind="checked: filterLocked"/><label for="locked">Locked</label>
</div>
<div><div class="widget-header-line-game1"></div><div class="widget-header-line-game2"></div><div class="widget-header-line-shadow"></div></div>
</div>
<div>
<div class="rounded-box" style="padding:15px;padding-top: 0;background-color:#fff;overflow:hidden;">
<table id="game-achievements" class="table table-condensed" style="margin-top:10px;">
<tbody data-bind="foreach: viewGame.achievements()">
<tr data-bind="visible: radioCheck" style="display: none">
视图模型/JS:
filterUnlocked: function(){
return filter('unlocked');
},
filterLocked: function(){
return filter('locked');
},
filterAll: function(){
return filter('all');
},
filter: function(x){
for (var item in itemsListForFilter){
if (x === 'locked'){
item.radioCheck = '!achieved';
}
if (x === 'unlocked') {
item.radioCheck = 'achieved';
}
else {item.radioCheck = true;}
}
观察视图模型是一个对象而不是一个函数:
var gamesViewModel = {
self: this,
settings: null,
gameId: null,
authorized: false,
(...)
现在名为 filterUnlocked 的函数等(过滤器除外)在 JS 文件中显示为“未使用的属性”。我应该怎么做才能从 HTML 中调用它们?或者有没有更好的方法来完成我正在寻找的东西?
谢谢。
【问题讨论】:
-
这是一种非标准的过滤方法。您是否正在优化以减少表格中的 DOM 插入和删除,或者您是否可以在过滤器更改时重新绘制所有行?
标签: javascript html knockout.js