在飞行中改变约束 [英] Changing constraints on the fly
问题描述
我有一个dijit.form.NumberTextBox输入字段,以这些parms开头:
I have a dijit.form.NumberTextBox input field that starts out with these parms:
new dijit.form.NumberTextBox({
id: din1,
style: "width:60px",
constraints: {
places: 0,
pattern: '######'
}
},
din1);
一切都很好,我想问的是我想改变'地方'和'模式'在飞行中所以我写这个来改变'地方'和'模式'的方法:
Everything works great..My question is I would like to change 'places' and 'pattern' parms on the fly. So I wrote this to change 'places' and 'patterns' parms:
var myFldObj = dijit.byId(din1);
if (myFldObj) {
var myConstObj = myFldObj.attr('constraints');
if (myConstObj) {
myConstObj.places = 2;
myConstObj.pattern = '#####.0';
}
}
所以,再次显示表单后, d期望输入字段允许2位小数,但表单仍然像place = 0和pattern ='######'。当我检查地方和模式的值时,我会得到我期望的值(2和#####。0)。我的问题:
So, after I show the form again, I'd expect the entry field to allow 2 decimal places but the form still acts like places=0 and pattern='######'. When I check the values of 'places' and 'pattern' I get what I'd expect (2 and #####.0). My question:
您可以随时更改这些值吗?
Can you change these values on the fly??
或
您是否必须销毁原始的dijit对象并使用新的parms重新创建?
Do you have to destroy the original dijit object and recreate with new parms??
Thx !!
推荐答案
所以,这是对我有用的:
So, here is what worked for me:
首先,我认为这是一个bug因为一个输入字段开始像
First, I think this is a bug because an input field that starts out like
new dijit.form.NumberTextBox({
id: "fieldID",
style: "width:60px",
constraints: {
places: 0
}
},
"fieldID");
然后随便更改代码如下:
that is then changed on the fly with code like:
注意:ntbArry - dijit.form.NumberTextBox的数组objs绑定到html
输入标记ID。
NOTE: ntbArry - Array of dijit.form.NumberTextBox objs tied to a html input tag id.
for (var x=0;x < ntbArry.length;x++) {
var handle = ntbArry[x];
if (handle) {
handle.attr('constraints').places = 2;
handle.attr('constraints').pattern = '#####.0#';
}
}
没有显示与创建此类别的字段相同的行为方式(没有限制mods在飞行中):
Does not exhibit the same behavior as a field created this way (no constraints mods on the fly):
new dijit.form.NumberTextBox({
id: "fieldID",
style: "width: 60px",
constraints: {
places: 2,
pattern: '#####.0#'
}
},
"fieldID");
它的行为接近,但每次输入小数点时,会弹出错误消息,指出无效条目。在最初使用约束places = 2和pattern'#####。0#'创建的字段上输入小数点时,不会弹出此消息。
It's close in behavior but every time you type a decimal point, the error message pops up stating invalid entry. This message doesn't pop up when typing the decimal point on a field that was originally created with the constraints places=2 and pattern '#####.0#'.
所以,要获得我想要的原始行为:
So, to get original behavior I wanted:
fieldIDEvents是绑定到NumberTextBox字段的dojo事件数组。
在继续断开连接之前,dojo事件
fieldIDEvents is an array of dojo events tied to NumberTextBox fields. Before continuing disconnect dojo events
for (var x=0;x < fieldIDEvents.length;x++) {
var handle = fieldIDEvents[x];
if (handle) {
dojo.disconnect(handle);
}
}
然后销毁NumberTextBox dojo对象
then destroy the NumberTextBox dojo objects
for (var x=0;x < ntbArry.length;x++) {
var handle = ntbArry[x];
if (handle) {
handle.destroy();
ntbArry[x] = null;
}
}
接下来,将输入标签放回html,因为它被破坏:
Next, place the input tag back into the html because it gets destroyed:
注意:tdtag和html td标签上的一个id,应该包含输入标签。
NOTE: tdtag and an id on a html td tag which should contain the input tag.
var fld1 = this.document.getElementById("tdtag");
if (fld1) {
//alert("\""+fld1.innerHTML+"\"");
fld1.innerHTML = "<input id=\"fieldID\">";
}
现在,再次创建NumberTextBox对象:
Now, create the NumberTextBox object again:
ntbArry[0] = new dijit.form.NumberTextBox({
id: "fieldID",
style: "width: 60px",
constraints: {
places: 2,
pattern: '#####.0#'
}
},
"fieldID");
这是一些额外的步骤,但至少我知道这对我有用。如果我我错过了一些基本的东西,让我知道,很容易错过这些东西的细节。
It's a few extra steps but, at least I know this is what works for me..If I'm missing something basic, let me know, it's easy to miss the small details with this stuff.
这篇关于在飞行中改变约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!