Issue #1568Opened November 8, 2018by dkmurakaru0 reactions

List/Array Trait

Question

Hello,

I am trying to write up a trait that allows creating a list of forms and generates the respective in the model. So far I have this:

editor.TraitManager.addType('list', {
  events: {
    'keyup': 'onChange'
  },
  getInputEl: function() {
    var self = this;
    var idx = 1;
    function createForm(input) {
      self.model.attributes.options.map(function(m) {
        // TOOD: Add select options
        // console.log(m);
        if (m.type === 'select') {
          var s = $('<select name="'+ m.name +'"></select>').get(0);
          s.appendChild($('<option value="">'+ m.placeholder +'</option>').get(0));
          m.options.map(function(opt) {
            s.appendChild($('<option value="'+ opt.value +'">'+ opt.name +'</option>').get(0))
          })
          input.appendChild(s);
        } else {
          input.appendChild($('<input name="'+ m.name +'" placeholder="'+ m.placeholder +'"/>').get(0))
        }

      });
      input.appendChild($('<button id="'+ idx +'" class="btn btn-sm btn-danger pull-left"><i class="fa fa-minus"></i> Remove</button>').get(0))
    }
    if (!this.inputEl) {
      var input = document.createElement('div');
      var btn = $('<button class="btn btn-sm btn-default"><i class="fa fa-plus"></i> Add</button>');
      // Listen for clicks on add button and add to the model
      btn.on('click', function(ev) {
        // var id = $(ev.currentTarget).data('');
        idx += 1;
        createForm(input);
      });
      input.appendChild(btn.get(0));
      createForm(input);
      // input.value = this.target.get('content');
      this.inputEl = input;
    }
    return this.inputEl;
  },
  onValueChange: function () {
    console.log(this.model.get('value'));
    this.target.set('list', this.model.get('value'));
  }
});

The current issues I am unable to bind the resulting elements to the onChange event listeners. Will I have to manually wire up each element to a listener? If so how would I proceed to update the form's model?

Thank you.

Answers (2)

artfNovember 9, 20180 reactions

@dkmurakaru pay attention that you're creating not a real input

var input = document.createElement('div');

therefore you should update also methods like onChange and setInputValue

lock[bot]November 9, 20190 reactions

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.