KnockoutJS 添加和循环 Observables
KnockoutJS Add and Loop Observables
嗨,我一直在努力解决这个问题,但我是 KnockoutJS 的菜鸟。这个想法是你添加一个输入,然后它会在接下来显示它自己的设置,比如名称属性占位符,必需的等等但是我 运行 遇到了几个问题。
我已经设法完成了一些工作,但出于某种原因,required 总是正确的。我遇到的另一个问题是添加更多 - 我的说法是否正确,然后我需要在 js 中添加更多可观察对象?我可以不做某种循环吗?这是我的代码,请帮忙。
<div class="input-row">
<div class="input-item">
<input type="text" data-bind="attr: { name: itemName, placeholder: itemPlaceholder, value : itemValue, required : itemRequired }" />
</div>
<div class="input-settings">
name:
<input type="text" data-bind="value: itemNameSetting">
<br/>
placehoder:
<input type="text" data-bind="value: itemPlaceholderSetting">
<br/>
required:
<select data-bind="value: itemRequiredSetting">
<option value="true">true</option>
<option value="false">false</option>
</select>
<br/>
maxlength:
<br/>
defaultvalue:
<input type="text" data-bind="value: itemValueSetting">
<br/>
</div>
</div>
<button>+ ADD MORE INPUTS</button>
JS
var ViewModel = function() {
this.itemNameSetting = ko.observable();
this.itemPlaceholderSetting = ko.observable();
this.itemRequiredSetting = ko.observable();
this.itemValueSetting = ko.observable();
this.itemName = ko.pureComputed(function() {
return this.itemNameSetting();
}, this);
this.itemPlaceholder = ko.pureComputed(function() {
return this.itemPlaceholderSetting();
}, this);
this.itemRequired = ko.pureComputed(function() {
return this.itemRequiredSetting();
}, this);
this.itemValue = ko.pureComputed(function() {
return this.itemValueSetting();
}, this);
};
ko.applyBindings(new ViewModel());
您的这个东西具有多个属性 - 名称、占位符、值等等:
{
name: 'foo',
placeholder: 'foo goes here',
value: 'bar'
}
但是你需要很多。在 Javascript 中,一个好的方法是构建一个构造函数:
var InputItem = function InputItem(name, placeholder, value) {
this.name = name;
this.placeholder = placeholder;
this.value = value;
}
现在我们想做多少就做多少:
var item1 = new InputItem('foo', 'foo goes here', 'bar');
var item2 = new InputItem('bar', 'bar goes here', 'baz');
item1.placeholder
// returns 'foo goes here'
item2.name
// returns 'bar'
item2.value = 'something else'
// item 2's value is now changed to 'something else'
但由于这是 Knockout,我们希望我们的属性是可观察的:
var InputItem = function InputItem(name, placeholder, value) {
this.name = ko.observable(name);
this.placeholder = ko.observable(placeholder);
this.value = ko.observable(value);
}
var item1 = new InputItem('foo', 'foo goes here', 'bar');
item1.name()
// returns 'foo'
item1.placeholder('kittens')
// item 1's placeholder is now changed to 'kittens'
你这里有一个数据模型——它保存了单个 'thing' 所需的所有数据,在你的情况下是一个输入。现在我们需要一个包含我们所有数据模型的视图模型,以及一种让用户添加更多数据模型的方法:
var ViewModel = function ViewModel() {
var that = this;
this.inputItems = ko.observableArray([]);
this.addInput = function addInput() {
that.inputItems.push( new InputItem() );
};
}
ko.applyBindings( new ViewModel() );
在我们的标记中,我们使用 foreach
:
遍历可观察 inputItems
数组中的所有 InputItems
<div data-bind="foreach: inputItems">
<!-- everything inside here is rendered once for every InputItem -->
<input type="text" data-bind="value: name">
<input type="text" data-bind="value: placeholder">
</div>
<button data-bind="click: addInput">Add another input</button>
试试这个交互式演示:
var InputItem = function InputItem(name, placeholder, value) {
this.name = ko.observable(name);
this.placeholder = ko.observable(placeholder);
this.value = ko.observable(value);
}
var ViewModel = function ViewModel() {
var that = this;
this.inputItems = ko.observableArray([]);
this.addInput = function addInput() {
that.inputItems.push(new InputItem());
};
}
ko.applyBindings(new ViewModel());
.console {
background-color: lightgrey;
padding: 1rem;
margin-top: 1rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<ul data-bind="foreach: inputItems">
<li>
<!-- everything inside here is rendered once for every InputItem -->
<input type="text" data-bind="value: name" placeholder="input name">
<input type="text" data-bind="value: placeholder" placeholder="input placeholder">
</li>
</ul>
<button data-bind="click: addInput">Add another input</button>
<div class="console">
<h1>You have added <span data-bind="text: inputItems().length"></span> input items</h1>
<ul data-bind="foreach: inputItems">
<li>
Name: <span data-bind="text: name"></span> with placeholder <span data-bind="text: placeholder"></span>
</li>
</ul>
</div>
嗨,我一直在努力解决这个问题,但我是 KnockoutJS 的菜鸟。这个想法是你添加一个输入,然后它会在接下来显示它自己的设置,比如名称属性占位符,必需的等等但是我 运行 遇到了几个问题。
我已经设法完成了一些工作,但出于某种原因,required 总是正确的。我遇到的另一个问题是添加更多 - 我的说法是否正确,然后我需要在 js 中添加更多可观察对象?我可以不做某种循环吗?这是我的代码,请帮忙。
<div class="input-row">
<div class="input-item">
<input type="text" data-bind="attr: { name: itemName, placeholder: itemPlaceholder, value : itemValue, required : itemRequired }" />
</div>
<div class="input-settings">
name:
<input type="text" data-bind="value: itemNameSetting">
<br/>
placehoder:
<input type="text" data-bind="value: itemPlaceholderSetting">
<br/>
required:
<select data-bind="value: itemRequiredSetting">
<option value="true">true</option>
<option value="false">false</option>
</select>
<br/>
maxlength:
<br/>
defaultvalue:
<input type="text" data-bind="value: itemValueSetting">
<br/>
</div>
</div>
<button>+ ADD MORE INPUTS</button>
JS
var ViewModel = function() {
this.itemNameSetting = ko.observable();
this.itemPlaceholderSetting = ko.observable();
this.itemRequiredSetting = ko.observable();
this.itemValueSetting = ko.observable();
this.itemName = ko.pureComputed(function() {
return this.itemNameSetting();
}, this);
this.itemPlaceholder = ko.pureComputed(function() {
return this.itemPlaceholderSetting();
}, this);
this.itemRequired = ko.pureComputed(function() {
return this.itemRequiredSetting();
}, this);
this.itemValue = ko.pureComputed(function() {
return this.itemValueSetting();
}, this);
};
ko.applyBindings(new ViewModel());
您的这个东西具有多个属性 - 名称、占位符、值等等:
{
name: 'foo',
placeholder: 'foo goes here',
value: 'bar'
}
但是你需要很多。在 Javascript 中,一个好的方法是构建一个构造函数:
var InputItem = function InputItem(name, placeholder, value) {
this.name = name;
this.placeholder = placeholder;
this.value = value;
}
现在我们想做多少就做多少:
var item1 = new InputItem('foo', 'foo goes here', 'bar');
var item2 = new InputItem('bar', 'bar goes here', 'baz');
item1.placeholder
// returns 'foo goes here'
item2.name
// returns 'bar'
item2.value = 'something else'
// item 2's value is now changed to 'something else'
但由于这是 Knockout,我们希望我们的属性是可观察的:
var InputItem = function InputItem(name, placeholder, value) {
this.name = ko.observable(name);
this.placeholder = ko.observable(placeholder);
this.value = ko.observable(value);
}
var item1 = new InputItem('foo', 'foo goes here', 'bar');
item1.name()
// returns 'foo'
item1.placeholder('kittens')
// item 1's placeholder is now changed to 'kittens'
你这里有一个数据模型——它保存了单个 'thing' 所需的所有数据,在你的情况下是一个输入。现在我们需要一个包含我们所有数据模型的视图模型,以及一种让用户添加更多数据模型的方法:
var ViewModel = function ViewModel() {
var that = this;
this.inputItems = ko.observableArray([]);
this.addInput = function addInput() {
that.inputItems.push( new InputItem() );
};
}
ko.applyBindings( new ViewModel() );
在我们的标记中,我们使用 foreach
:
inputItems
数组中的所有 InputItems
<div data-bind="foreach: inputItems">
<!-- everything inside here is rendered once for every InputItem -->
<input type="text" data-bind="value: name">
<input type="text" data-bind="value: placeholder">
</div>
<button data-bind="click: addInput">Add another input</button>
试试这个交互式演示:
var InputItem = function InputItem(name, placeholder, value) {
this.name = ko.observable(name);
this.placeholder = ko.observable(placeholder);
this.value = ko.observable(value);
}
var ViewModel = function ViewModel() {
var that = this;
this.inputItems = ko.observableArray([]);
this.addInput = function addInput() {
that.inputItems.push(new InputItem());
};
}
ko.applyBindings(new ViewModel());
.console {
background-color: lightgrey;
padding: 1rem;
margin-top: 1rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<ul data-bind="foreach: inputItems">
<li>
<!-- everything inside here is rendered once for every InputItem -->
<input type="text" data-bind="value: name" placeholder="input name">
<input type="text" data-bind="value: placeholder" placeholder="input placeholder">
</li>
</ul>
<button data-bind="click: addInput">Add another input</button>
<div class="console">
<h1>You have added <span data-bind="text: inputItems().length"></span> input items</h1>
<ul data-bind="foreach: inputItems">
<li>
Name: <span data-bind="text: name"></span> with placeholder <span data-bind="text: placeholder"></span>
</li>
</ul>
</div>