question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] How to set options of 'SELECT' Traits type dynamically when its previous traits value is set?

See original GitHub issue

Hello @artf again…!! I’m working with your million dollar tool since last few weeks as I’m working on a project where this 'Drag ‘n Drop’ tool is very useful.

I’m frequently having many questions about ‘How to’ type!

Once again I’m having a question for now.

I have use component to create a dynamic block which get values of API URL, with authentications and receives product data from it. This data have many different product categories. So, after receiving data(check code below), I have to populate a new trait with type ‘select’ which will list all those categories.

So the question is how to set options list in ‘select’ trait dynamically after API call?

screenshot from 2017-07-13 11-27-11

My code structure is like this:

comps.addType('productList', {
	model: defaultModel.extend({
      init() {
        this.listenTo(this, 'change:apiurl change:items change:callApi', this.doStuff);
      },
      doStuff() {
      	// All stuff of API calls
      	// Will receive data of categories in array[{ value:'val1',name:'name1' }, ....];
      	// Will set HTML in the editor using editor.setComponents(<html-data></html-data>);

      	How to set categories TRAITS now? I tried:
      	MyDynamicDataArrayGoesHere = array[{ value:'val1',name:'name1' }, ....];
      },
      defaults: Object.assign({}, defaultModel.prototype.defaults, {
        editable: true,
        droppable: true,
        traits: [
          {
            label: 'API URL',
            name: 'apiurl',
            options: [
              {value: 'text', name: 'Text'}
            ]
          },
          {
            label: 'Username',
            name: 'username',
            type: 'text',
            changeProp: 1
          },
          {
            label: 'Password',
            name: 'password',
            type: 'password',
            changeProp: 1
          },
          {
            label: 'Items',
            name: 'items',
            type: 'text',
            changeProp: 1
          },
          {
          	label: 'Categories',
          	name: 'cats',
          	type: 'select',
          	options: MyDynamicDataArrayGoesHere			// Tried this but didn't worked!
          	changeProp: 1
          }
        ],
    }),

},
{
  isComponent: function(el) {
    if(el.tagName == 'PRODUCTLIST'){
      return {type: 'productList'};
    }
  },
}),

view: defaultType.view,
});

I already tried custom traits, but in that I’ll first have to call API and get the category list, which is called in doStuff(). So how to set select traits option in doStuff()…??? Thanks a lot 😇

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
blairpanekcommented, Jun 6, 2018

editor.TraitManager.getTraitsViewer().render(); this is what you are looking for… @HarshOB @cmcintosh

2reactions
artfcommented, Jul 13, 2017

You should get the trait from the model and then update its options:

doStuff(){
  var categsTrait = this.get('traits').where({name: 'cats'})[0];
  categsTrait.set('options', [{ value:'val1',name:'name1' }, ....])
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamically change selectbox options based on previous ...
Check the setChild(value) function. Parent's selected value is passing to that function when page is ready (just after loading) and also when ...
Read more >
Computed Traits | Segment Documentation
Computed Traits allow you to quickly create user or account-level ... The last trait returns the last event property value Segment has seen....
Read more >
Changing select option dynamically . | SAP Community
hi ! I need to change selects option dynamically from mandatory / not mandatory. depends on the user selection of radio buttons.
Read more >
Run unit tests with Test Explorer - Visual Studio (Windows)
Searches both trait category and value for matches. The syntax to specify trait categories and values are defined by the unit test framework....
Read more >
Behavioral cohorts: Identify users with similar behaviors
This article will help you: ... In Amplitude, a cohort is a group of users who share a trait or set of traits....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found