dojo / form / select addoption not working

时间:2016-01-07 21:31:40

标签: dojo

我在模板html文件和JS填充组合框中使用数据附加点 distanceUnitSelect 定义元素dijit / form / select。此外,组合框的初始化是在postCreate方法中完成的。

当我运行代码时出现错误 TypeError:this.distanceUnitSelect.addOption不是函数。请告知如何修复此问题,问题,我是dojo的新手。< / p>

define([
    "dojo/_base/declare",
    'dojo/_base/lang',
    "dojo/on",
    'dojo/_base/array',
    "dijit/_WidgetBase",
    "dijit/_TemplatedMixin",
    "dojo/text!./Widget.html",
    'dijit/form/Select'
], function(declare, lang,on, array,_WidgetBase, _TemplatedMixin, template) {

    /**
     * dijit._WidgetBase is the base class for all widgets in dijit, and in general
     * is the base class for all dojo based widgets.
     *
     * It provide life cycle method which get called at diffrent stages
     *
     */
    return declare([_WidgetBase, _TemplatedMixin], {

        /**
         * templateString is property used by _TemplatedMixin, to get the HTML template and put attach point
         * and event on it.
         * @type {[type]}
         */
        templateString: template,


        counter: 0,

        _initUnitSelect:function(){


        this._initDefaultUnits();
        var b = this.defaultDistanceUnits;
        console.log(b.length);
        this.distanceUnits = b;
         var test = this.distanceUnitSelect;
        array.forEach(this.distanceUnits,lang.hitch(this,function(unitInfo){
          var option = {
            value:unitInfo.unit,
            label:unitInfo.label
          };

           // dijit.byId("test").addOption(option);
            this.distanceUnitSelect.addOption(option);

        }));

      },

      _initDefaultUnits:function(){
        this.defaultDistanceUnits = [{
          unit: 'KILOMETERS',
          label: 'KILOMETERS'
        }, {
          unit: 'MILES',
          label: 'MILES'
        }, {
          unit: 'METERS',
          label:'METERS'
        }, {
          unit: 'FEET',
          label: 'FEET'
        }, {
          unit: 'YARDS',
          label: 'Yards'
        }];


      },



        /**
         * constructor method will be called before the parameters are mixed into the widget,
         * and can be used to initialize arrays
         */
        constructor: function() {
            console.log("in constructor");
        },

        /**
         * Most used life cycle method of _WidgetBase.
         * At this stage, widget has been rendered but not attached to node.
         */
        postCreate: function() {
            console.log("in postCreate...");
            this._initUnitSelect();
       },

       postMixInProperties: function(){
            this.inherited(arguments);
      },

        /**
         * Increases counter value to one.
         */
        incCounter: function() {
            console.log("in incCounter");
            //this.output.innerHTML = (++this.counter);

        },

        /**
         * Decrease counter value to one.
         */
        decrCounter: function() {
            console.log("in decrCounter");
            //this.output.innerHTML = (--this.counter);

        }

    });
});
<div data-dojo-type="dijit/layout/TabContainer" style="padding-top:100px;width: 100%; min-width: 100px;height: 50%;" tabStrip="true">
  <div data-dojo-type="dijit/layout/ContentPane" title="Draw Tools" selected="true">

    <div class="formContainer">
      <div data-dojo-type="dijit/form/Form" data-dojo-attach-point="printSettingsFormDijit">
        <table cellspacing="5" style="width:100%;">
          <tr>
            <td style="width:65px;">
              Boundary Types:
            </td>
          </tr>
          <tr>
            <td>
              <select id="test" data-dojo-attach-point="distanceUnitSelect" data-dojo-type="dijit/form/Select" style="width:100%;height:30px; float: left;">
              </select>

            </td>



          </tr>
          <tr>
            <td>
              <button>Load Boundaries</button>&nbsp;
            </td>
            <td>



            </td>
          </tr>
          <tr>
            <div id="grid"></div>
          </tr>
      </div>
    </div>

    <span data-dojo-attach-point="output"></span>


    <div class="operations" onselectstart="return false;">
      <button data-dojo-attach-point="inc" data-dojo-attach-event="onclick:incCounter">Validate</button>&nbsp;
      <button data-dojo-attach-point="dec" data-dojo-attach-event="onclick:decrCounter">Create</button>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:2)

如果您要在模板中安装需要解析的小部件,那么您的小部件需要扩展'dijit/_WidgetsInTemplateMixin',现在this.distanceUnitSelect是一个简单的DomNode,所以将此添加到您的代码中让它工作,阅读评论

define([
    "dojo/_base/declare",
    'dojo/_base/lang',
    "dojo/on",
    'dojo/_base/array',
    "dijit/_WidgetBase",
    "dijit/_TemplatedMixin",
    'dijit/_WidgetsInTemplateMixin', //ADD THIS IF YOU NEED TO PARSE WIDGET IN YOUR TEMPLATES
    "dojo/text!./Widget.html",
    'dijit/form/Select'
], function(declare, lang,on, array,_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, template) {

    /**
    * Your comments .......
    */
    return declare([_WidgetBase, _TemplatedMixin, /*YOU ARE MISSING THIS*/_WidgetsInTemplateMixin], {
    //the rest of your code
    ............

阅读示例代码中的注释a。

一些参考:
The _WidgetsInTemplateMixin Mixin
dijit._WidgetsInTemplateMixin