Polymer 1.0动态地向菜单添加选项

时间:2015-09-09 14:15:36

标签: dynamic data-binding polymer

嗨我在获取动态添加选项的菜单时遇到了一些麻烦。他们的想法是选择第一个菜单决定第二个菜单包含的内容。我成功地在没有聚合物的情况下制造了它它与聚合物一起半成品。下拉列表根据选择从json获取其内容,下拉列表2也从json获取其内容。这部分有效,问题是当您从下拉列表中进行选择然后更改它时,下拉列表2不会删除旧选择。我上次使用了一个函数,在重新填充内容之前首先删除所有下拉列表的子项。 Polymer的问题是删除childNodes后,下拉列表中断,并且不能通过数据绑定添加其他子项。尝试使用普通JS添加本机,填充菜单,但子项不可选(从我读到的这可能是一个错误)。此外,我认为动态项目的数据绑定也不再起作用。无论如何,这就是我所拥有的:

<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../../bower_components/paper-material/paper-material.html">
<link rel="import" href="../../../bower_components/paper-dropdown-menu/paper-dropdown-menu.html">
<link rel="import" href="../../../bower_components/paper-menu/paper-menu.html">
<link rel="import" href="../../../bower_components/paper-item/paper-item.html">
<link rel="import" href="../../../bower_components/iron-ajax/iron-ajax.html">
<link rel="import" href="../../../bower_components/paper-button/paper-button.html">
<link rel="import" href="../../../bower_components/iron-dropdown/demo/x-select.html">

<dom-module id="add-skill">
  <template>
    <paper-material elevation="1">
      <paper-dropdown-menu id="ddMenu" attr-for-selected="value" >
        <paper-menu class="dropdown-content" id="vendorSelect" on-iron-select="_itemSelected">
          <template is="dom-repeat" items="{{vendorList}}">
            <paper-item id="vendorName" value="item">[[item]]</paper-item>
          </template>
        </paper-menu>
      </paper-dropdown-menu>

      <paper-dropdown-menu>
        <paper-menu class="dropdown-content" id="certificationSelect" on-iron-select="_itemSelected">
        </paper-menu>
      </paper-dropdown-menu>

      <!-- testing ideas -->
      <paper-dropdown-menu>
        <paper-menu class="dropdown-content" id="test" on-iron-select="_itemSelected">
          <option extends="paper-item"> Option </option>
          <option extends="paper-item"> Option1 </option>
          <option extends="paper-item"> Option2 </option>
        </paper-menu>
      </paper-dropdown-menu>

      <paper-button on-click="_deleteElement">
        Delete
      </paper-button>
    </paper-material>
    <iron-ajax
      id="vendorSubmit"
      method="POST"
      url="../../../addskill.php"
      handle-as="json"
      on-response="handleVendorResponse"
      debounce-duration="300">
    </iron-ajax>

    <iron-ajax
      id="certificationSubmit"
      method="POST"
      url="../../../addskill.php"
      handle-as="json"
      on-response="handleCertificationResponse"
      debounce-duration="300">
    </iron-ajax>
  </template>
  <script>
    Polymer({
      is: 'add-skill',
      ready: function() {
        this.sendVendorRequest();
        this.vendorList = [];
        this.certificationList = [];
      },

      sendVendorRequest: function() {
        var datalist = 'vendor=' + encodeURIComponent('1');
        //console.log('datalist: '+datalist);
        this.$.vendorSubmit.body = datalist;
        this.$.vendorSubmit.generateRequest();
      },

      handleVendorResponse: function(request) {
        var response = request.detail.response;

        for (var i = 0; i < response.length; i++) {
          this.push('vendorList', response[i].name);
        }
      },

      vendorClick: function() {
        var item = this.$;
        //var itemx = this.$.vendorSelect.selectedItem.innerHTML;
        //console.log(item);
        //console.log(itemx);
      },

      sendCertificationRequest: function(vendor) {
        var datalist = 'vendorName=' + encodeURIComponent(vendor);
        console.log('datalist: ' + datalist);
        this.$.certificationSubmit.body = datalist;
        this.$.certificationSubmit.generateRequest();
      },

      handleCertificationResponse: function(request) {
        var response = request.detail.response;

        //var vendorSelect = document.getElementById('vendorSelect');
        for (var i = 0; i < response.length; i++) {

          this.push('certificationList', response[i].name);
        }

        console.log(this.certificationList);
      },

      _itemSelected: function(e) {
        var selectedItem = e.target.selectedItem;
        if (selectedItem) {
          this.sendCertificationRequest(selectedItem.innerText);
          console.log("selected: " + selectedItem.innerText);
        }
      },

      _removeArray: function(arr) {
        this.$.certificationList.remove();
        for (var i = 0; i < arr.length; i++) {
          console.log(arr[i]);
          arr.splice(0, i);
          arr.pop();
        }

        console.log(arr.length);
      },

      _deleteElement: function() {
        var element = document.getElementById('certificationSelect');

        while (element.firstChild) {
          element.removeChild(element.firstChild);
        }
      },

      _createElement: function() {
        var doc = document.querySelector('#test');
        var option = document.createElement('option');
        option.extends = "paper-item";
        option.innerHTML = "Option";
        doc.appendChild(option);
      }
    });
  </script>
</dom-module>

始终感谢任何指导

0 个答案:

没有答案