以编程方式设置dijit NumberSpinner值

时间:2017-03-21 21:38:39

标签: javascript dojo set dijit.form

我想以编程方式更改data-dojo-attach-point值。如何使用' id'还是this.typeResidential.set('value', 30);

下面的代码更新了Number Spinner文本,但它给了我

  

"未捕获的TypeError:c.advice.apply不是函数"错误。

如何以编程方式正确更改值?

#ifdef _MSC_VER
#include <boost/config/compiler/visualc.hpp>
#endif
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <cassert>
#include <exception>
#include <iostream>
#include <sstream>
#include <string>

void print(boost::property_tree::ptree const& pt)
{
    using boost::property_tree::ptree;
    ptree::const_iterator end = pt.end();
    for (ptree::const_iterator it = pt.begin(); it != end; ++it) {
        std::cout << it->first << ": " << it->second.get_value<std::string>() << std::endl;
        print(it->second);
    }
}

int main()
{
    try
    {
        std::stringstream ss;
        // send your JSON above to the parser below, but populate ss first

        ss << "{ \"particles\": [ { \"electron\": { \"pos\": [ 0, 0, 0 ], \"vel\": [ 0, 0, 0 ] }, \"proton\": { \"pos\": [ -1, 0, 0 ], \"vel\": [ 0, -0.1, -0.1 ] } } ]}";


        boost::property_tree::ptree pt;
        boost::property_tree::read_json(ss, pt);

        BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("particles"))
        {
            assert(v.first.empty()); // array elements have no names
            print(v.second);
        }
        return EXIT_SUCCESS;
    }
    catch (std::exception const& e)
    {
        std::cerr << e.what() << std::endl;
    }
    return EXIT_FAILURE;
}

2 个答案:

答案 0 :(得分:1)

在dojo中,您可以通过编程方式(通过bRIMO答案正确显示)或声明性地设置窗口小部件的属性。

以编程方式使用时,请确保this具有适合您窗口小部件的范围。

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

在替代方案中,下面是声明性语法的示例,其中包括在HTML标记中添加名为value的属性。

您可以通过两种方式获得相同的结果,这实际上取决于您的应用程序设计。

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>

答案 1 :(得分:0)

这取决于您编写应用的方式, 如果您正在使用data-dojo-attach-point编写窗口小部件并访问this.your-attach-name或使用id并访问窗口小部件registry

下面的示例使用id并使用dijit/registry

使用此小部件的registry.by("widgetId").set("value",val);设置spiiner的值

实例:https://jsfiddle.net/gibbok/9rLtzm1u/

&#13;
&#13;
require(["dijit/registry","dojo/ready","dojo/parser"],
		function(registry,ready,parser){
        parser.parse();
    		ready(function(){
        	registry.byId("typeResidential").set("value",213);
        });    
    }
);
&#13;
<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>
&#13;
&#13;
&#13;