动态改变约束

发布于 2024-08-27 13:30:30 字数 730 浏览 8 评论 0原文

我有一个 dijit.form.NumberTextBox 输入字段,以这些参数开头:

 new dijit.form.NumberTextBox({
    id: din1,
    style: "width:60px",
    constraints: {
        places: 0,
        pattern: '######'
      }
    },
    din1);

一切都很好。我的问题是我想动态更改“位置”和“模式”参数。所以我写这个来改变'places'和'patterns'参数:

var myFldObj = dijit.byId(din1);
if (myFldObj) {
  var myConstObj = myFldObj.attr('constraints');
  if (myConstObj) {
     myConstObj.places = 2;
     myConstObj.pattern = '#####.0';
  }
}

所以,在我再次显示表单后,我希望输入字段允许2个小数位,但表单仍然像places=0和pattern='#一样#####'。当我检查“places”和“pattern”的值时,我得到了我所期望的结果(2 和 #####.0)。我的问题:

你能即时更改这些值吗?

或者

您是否必须销毁原始 dijit 对象并使用新参数重新创建?

谢谢!!

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';
  }
}

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??

OR

Do you have to destroy the original dijit object and recreate with new parms??

Thx!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱人配狗天长地久 2024-09-03 13:30:30

所以,这对我有用:

首先,我认为这是一个错误,因为像这样开始的输入字段

new dijit.form.NumberTextBox({
    id: "fieldID",
    style: "width:60px",
    constraints: {
        places: 0
      }
    },
    "fieldID");

然后使用如下代码动态更改:

注意:ntbArry - 绑定到的 dijit.form.NumberTextBox 对象的数组一个html
输入标签 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#';      
  } 
}

与以这种方式创建的字段不表现出相同的行为(没有动态约束模式):

new dijit.form.NumberTextBox({
  id: "fieldID",
  style: "width: 60px",
  constraints: {
    places: 2,
    pattern: '#####.0#'
  }
},
"fieldID");

它的行为很接近,但每次您键入小数点时,都会弹出错误消息,指出输入无效。在最初使用约束 place=2 和模式“#####.0#”创建的字段上键入小数点时,不会弹出此消息。

因此,为了获得我想要的原始行为:

fieldIDEvents 是与 NumberTextBox 字段关联的 dojo 事件数组。
在继续断开 dojo 事件之前

for (var x=0;x < fieldIDEvents.length;x++) {
  var handle = fieldIDEvents[x];
  if (handle) {    
    dojo.disconnect(handle);
  }
}

,然后销毁 NumberTextBox dojo 对象

for (var x=0;x < ntbArry.length;x++) {
  var handle = ntbArry[x];
  if (handle) {
    handle.destroy();
    ntbArry[x] = null;
  }
}

接下来,将输入标记放回到 html 中,因为它会被破坏:

注意:tdtag 和 html td 标记上的 id 应包含输入标记。

var fld1 = this.document.getElementById("tdtag");

if (fld1) {
  //alert("\""+fld1.innerHTML+"\"");
  fld1.innerHTML = "<input id=\"fieldID\">";
} 

现在,再次创建 NumberTextBox 对象:

ntbArry[0] = new dijit.form.NumberTextBox({
  id: "fieldID",
  style: "width: 60px",
  constraints: {
    places: 2,
    pattern: '#####.0#'
  }
},
"fieldID");

这是一些额外的步骤,但是,至少我知道这对我有用......如果我缺少一些基本的东西,请告诉我,很容易错过这些东西的小细节。

So, here is what worked for me:

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:

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#';      
  } 
}

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");

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 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);
  }
}

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;
  }
}

Next, place the input tag back into the html because it gets destroyed:

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\">";
} 

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.

玩套路吗 2024-09-03 13:30:30

我使用 Dojo 1.3,我可以看到 dijit.form.NumberTextBox 没有 patternplaces 属性,但有 editOptions 属性。所以我会尝试改变这样的约束:

myConstObj.editOption.places = 2;

I use Dojo 1.3 and I can see that dijit.form.NumberTextBox has no pattern and places properties, but has editOptions property. So I would try to change the constraints like this:

myConstObj.editOption.places = 2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文