敲除JS-将CSS类绑定到元素
我想实现动态类别的划船,该划线应根据变量分配和重新分配正确的类,该类别放在该字段中。我得到的问题,当我诱导函数setred()
,然后setwhite()
时,两个颜色类都是串出的,当然也考虑了第一css类。
我有一个绑定看起来像这样的元素:
<div data-bind='dxNumberBox: dxCustomCalc'></div>
到目前为止,我制作了elementAttr
class ko.observable();
self.dxCustomCalc = {
displayExpr: 'Name',
keyExpr: 'Id',
value: self.customCalc,
//onValueChanged: function (e) {
// self.childFormName(e.component.option('selectedItem'));
//},
disabled: ko.computed(function () {
return self.readOnly;
}),
elementAttr: {
/* class: "is-valid-nok"*/ /* - red*/
/*class: "is-valid-onlyok" */ /* -white */
/*class: "is-required-empty",*/ /* - yellow */
class: ko.observable(),
}
};
and tough element:tough element:
function setRed() {
self.dxCustomCalc.elementAttr.class("is-valid-nok");
console.log("color changed to red")
}
function setWhite(){
self.dxCustomCalc.elementAttr.class("is-valid-onlyok");
console.log("color changed to white")
}
函数是基于值,场地。例如,如果值匹配,则触发了函数setred()
。然后,如果值更改并满足条件,则功能setWhite()
将发射。 我得到的结果是,在订阅元素上执行两个函数之后,
<div data-bind="dxNumberBox: dxCustomCalc" class="dx-numberbox is-valid-nok is-valid-onlyok">
我要实现的结果是:执行两个函数之后是:
<div data-bind="dxNumberBox: dxCustomCalc" class="dx-numberbox is-valid-onlyok">
I want to achieve dynamic class biding, which should assign and reassign correct class based on variable, putted in this field. The issue I got, when I induce function setRed()
, and then setWhite()
, both color classes are binded, and of course 1st CSS class is considered.
I've got an element which binding looks like that:
<div data-bind='dxNumberBox: dxCustomCalc'></div>
So far I made elementAttr
class ko.observable();
self.dxCustomCalc = {
displayExpr: 'Name',
keyExpr: 'Id',
value: self.customCalc,
//onValueChanged: function (e) {
// self.childFormName(e.component.option('selectedItem'));
//},
disabled: ko.computed(function () {
return self.readOnly;
}),
elementAttr: {
/* class: "is-valid-nok"*/ /* - red*/
/*class: "is-valid-onlyok" */ /* -white */
/*class: "is-required-empty",*/ /* - yellow */
class: ko.observable(),
}
};
And went through element:
function setRed() {
self.dxCustomCalc.elementAttr.class("is-valid-nok");
console.log("color changed to red")
}
function setWhite(){
self.dxCustomCalc.elementAttr.class("is-valid-onlyok");
console.log("color changed to white")
}
Functions are executed based on value in field. For example, If value matches, function setRed()
is fired. Then, if the value changes and the condition is met, function setWhite()
is fired.
The result I got, after executing both functions on subscription to element is:
<div data-bind="dxNumberBox: dxCustomCalc" class="dx-numberbox is-valid-nok is-valid-onlyok">
The result I want to achieve, after executing both functions is:
<div data-bind="dxNumberBox: dxCustomCalc" class="dx-numberbox is-valid-onlyok">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将使用
类
绑定来设置基于可观察的CSS类。您可以直接使用它
,也可以使用
dxcustomcalc
自定义绑定,使用ko.applybindingstonode()
::拥有
.setred()
之类的额外方法是笨拙且不必要的。将CSSCLASS
变成可观察到的可观察到的可观察到的,该观察值基于ViewModel的状态计算类名称,例如I'd use the
class
binding to set a CSS class based on an observable.You could do use it directly
or you could apply the
class
binding as part of yourdxCustomCalc
custom binding, usingko.applyBindingsToNode()
:Having extra methods like
.setRed()
is clunky and unnecessary. TurncssClass
into a computed observable that calculates a class name based on the state of the viewmodel, e.g.