Mackey
Mackey

Reputation: 23

Setting dijit NumberSpinner value programmatically

I would like to change the dijit/form/NumberSpinner value programmatically. How to achieve this using a 'data-dojo-attach-point' or id?

The below code update the Number Spinner text, but it gives me

"Uncaught TypeError: c.advice.apply is not a function" error.

How to properly change the value programmatically?

this.typeResidential.set('value', 30);

Upvotes: 2

Views: 458

Answers (2)

GibboK
GibboK

Reputation: 73988

In dojo you can set properties for a widget programmatically (as properly showed by bRIMOs answer) or declaratively.

When using programmatically make sure that the this has the right scope to your widget.

this.typeResidential.set('value', 30);

In alternative, below an example of a declarative syntax, which consist in adding in your HTML markup a property of name value.

You can achieve the same result in both ways, it really depends of you application design.

require(["dojo/parser", "dijit/form/NumberSpinner"]);
<script>
var dojoConfig = {
        parseOnLoad: true,
        isDebug: true,
    };
</script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.11.3/dojo/dojo.js"></script>
<link href="//ajax.googleapis.com/ajax/libs/dojo/1.8.3/dijit/themes/claro/claro.css" rel="stylesheet"/>
<div class="claro">
    <input data-dojo-type="dijit/form/NumberSpinner"
        id="integerspinner2"
        value="1000"
        data-dojo-props="smallDelta:10, constraints:{min:9,max:1550,places:0}"
        name="someNumber"
        />
</div>

Upvotes: 1

Bourbia Brahim
Bourbia Brahim

Reputation: 14712

It depends on how you are writing your app , if you are writing a widget both using data-dojo-attach-point and access this.your-attach-name Or using id and accessing widget throw registry .

bellow example by using an id and set the value of the spiiner using dijit/registry of this widget by using registry.by("widgetId").set("value",val);

Live example: https://jsfiddle.net/gibbok/9rLtzm1u/

require(["dijit/registry","dojo/ready","dojo/parser"],
		function(registry,ready,parser){
        parser.parse();
    		ready(function(){
        	registry.byId("typeResidential").set("value",213);
        });    
    }
);
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.11.3/dojo/dojo.js"></script>
<link href="//ajax.googleapis.com/ajax/libs/dojo/1.8.3/dijit/themes/claro/claro.css" rel="stylesheet"/>
<div class="claro">
  <div data-dojo-type="dijit/form/NumberSpinner" id="typeResidential"></div>
</div>

Upvotes: 0

Related Questions