support selected values be simple values mapped to options beeing objects
See original GitHub issueHi,
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:
- Created 7 years ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
What I do and encourage other people to do is to use
value="whatever"
to extract that, in combination with the find-by helper:Were you aware of this? I just a little bit more verbose but also has very little magic.
I’m closing since I prefer users to do this themselves and keep the component simple. Thanks for the feedback.