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.

support selected values be simple values mapped to options beeing objects

See original GitHub issue

Hi,

I just extended the PowerSelect so I can set and get the selected values by only passing an identifying property of the options objects.

Maybe you wanna include that as a standard feature to the power select. Here is the code:

import PowerSelect from 'ember-power-select/components/power-select';
import Ember from 'ember';
import { isEmberArray } from 'ember-array/utils';

export default PowerSelect.extend({
  valueProperty:null,

  actions: {
    select(selected /*, e */) {
      if (this.publicAPI.selected !== selected) {

        let valueProperty = this.get("valueProperty");
        if(valueProperty){
          if(isEmberArray(selected)){
            for(let i = 0; i < selected.length; i++){
              if(Ember.typeOf(selected[i]) === 'object'){
                selected[i] = selected[i][valueProperty];
              }
            }
          } else if(Ember.typeOf(selected) === 'object'){
            selected = selected[valueProperty];
          }
        }

        this._super(selected);
      }
    },
  },
  updateSelection(selection) {

    let valueProperty = this.get("valueProperty");
    let options = this.get("options");

    if(valueProperty && options && Ember.typeOf(options[0]) === 'object') {
      if (isEmberArray) {
        for (let i = 0; i < selection.length; i++) {
          if (Ember.typeOf(selection[i]) !== 'object') {
            selection[i] = options.findBy(valueProperty, selection[i]);
          }
        }
      } else if (Ember.typeOf(selection) !== 'object') {
        selection = options.findBy(valueProperty, selection);
      }
    }
    this._super(selection);
  },
});

If you like the idea I can build a pull request for this.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
cibernoxcommented, Sep 21, 2016

What I do and encourage other people to do is to use value="whatever" to extract that, in combination with the find-by helper:

{{#power-select options=options selected=(find-by 'id' id options) onchange=(action (mut id) value="id") as |option|}}
  {{option.label}}
{{/power-select}}

Were you aware of this? I just a little bit more verbose but also has very little magic.

0reactions
cibernoxcommented, Sep 27, 2016

I’m closing since I prefer users to do this themselves and keep the component simple. Thanks for the feedback.

Read more comments on GitHub >

github_iconTop Results From Across the Web

<select>: The HTML Select element - HTML - MDN Web Docs
Each <option> element should have a value attribute containing the data value to submit to the server when that option is selected.
Read more >
Angular Material: mat-select not selecting default
I am trying to set the value to default to one of the options, however it is being left selected when the page...
Read more >
Filter Data from Your Views - Tableau Help
Special: Select the Special option to filter on Null values. ... Filters shelf (if it is not already being filtered), and a filter...
Read more >
Underscore.js
Produces a new array of values by mapping each value in list through a transformation function (iteratee). The iteratee is passed three arguments:...
Read more >
Context Object - AWS Step Functions
The value for a Task field is null if the Parameters field is outside of a task ... a simple Map state, we...
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