我的淘汰视图模型中有一个可观察的名称字段。 现在我想限制此字段中的字符数,如果超过一定数量。
好像名字=“约翰史密斯”,我有6个字符的限制然后
显示“john s ...”
答案 0 :(得分:20)
另一种可重用的解决方案是创建一个自定义绑定,显示文本的剪裁版本。
这允许基础值保持不受影响,但修剪文本以用于显示目的。这对于消息预览或将数据拟合到网格列中非常有用。
示例绑定:
ko.bindingHandlers.trimLengthText = {};
ko.bindingHandlers.trimText = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var trimmedText = ko.computed(function () {
var untrimmedText = ko.utils.unwrapObservable(valueAccessor());
var defaultMaxLength = 20;
var minLength = 5;
var maxLength = ko.utils.unwrapObservable(allBindingsAccessor().trimTextLength) || defaultMaxLength;
if (maxLength < minLength) maxLength = minLength;
var text = untrimmedText.length > maxLength ? untrimmedText.substring(0, maxLength - 1) + '...' : untrimmedText;
return text;
});
ko.applyBindingsToNode(element, {
text: trimmedText
}, viewModel);
return {
controlsDescendantBindings: true
};
}
};
像这样使用:
<div data-bind="trimText: myText1"></div>
...或
<div data-bind="trimText: myText1, trimTextLength: 10"></div>
答案 1 :(得分:18)
<span data-bind="text: (name.length > 6 ? name().substring(0, 5) + '...' : name)"></span>
或者您可以在ViewModel中创建计算的observable,例如:
var self = this;
this.shortName = ko.computed(function() {
return (self.name.length > 6 ? self.name().substring(0, 5) + '...' : self.name);
});
然后:
<span data-bind="text: shortName"></span>
答案 2 :(得分:7)
ko.extenders.maxLength = function(target, maxLength) {
//create a writeable computed observable to intercept writes to our observable
var result = ko.computed({
read: target, //always return the original observables value
write: function(newValue) {
var current = target(),
valueToWrite = newValue ? newValue.substring(0, Math.min(newValue.length, maxLength)) : null;
//only write if it changed
if (valueToWrite !== current) {
target(valueToWrite);
} else {
//if the rounded value is the same, but a different value was written, force a notification for the current field
if (newValue !== current) {
target.notifySubscribers(valueToWrite);
}
}
}
});
//initialize with current value to make sure it is rounded appropriately
result(target());
//return the new computed observable
return result;
};
然后,您可以在任何observable上使用它,并且可以为它们中的任何一个指定不同的最大长度。这消除了HTML(解决方案1)中的混乱,以及编写计算的可观察量(解决方案2)的必要性。您只需按以下方式定义您的observable:
this.shortName = ko.observable().extend({ maxLength: 25 });
答案 3 :(得分:2)
如果要截断数字输入的值,可以使用和扩展器截断值,如下所示:
ko.extenders.truncateValue = function(target, option) {
target.subscribe(function (newValue) {
if(newValue.length > option){
target(newValue.substring(0,option));
}
});
return target;
};
然后创建一个自定义绑定,将扩展程序附加到observable:
ko.bindingHandlers.maxLength = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
'use strict';
var maxlength = element.getAttribute("maxlength");
valueAccessor().extend({truncateValue: maxlength })
ko.bindingHandlers.value.init(element, valueAccessor, allBindingsAccessor, viewModel);
}
};
在您的html上,您应用maxLength绑定,如下所示:
<input type="number" data-bind="maxLength: yourObservable" maxlength="9"></input>