Creating a Grouped Select in Ember.js - javascript

It's relatively easy to create a select in Ember.js using Ember.Select.
The question is, how do I make this into a grouped select, using an optgroup. I don't think this is built in, but I'm guessing it's possible with some modifications to the template.

This is natively supported by Ember now, but there are a few gotchas. In 1.4.0, the following solution works...
Here's the example data:
foos: Ember.A([{value: 'foo', label: 'Foo', group: 'Foos'}, {value: 'bar', label: 'Bar', group: 'Bars'}, {value: 'bar2', label: 'Bar2', group: 'Bars'}, {value: 'foo2', label: 'Foo2', group: 'Foos'}])
Here's the view helper:
{{view Ember.Select content=controller.foos optionLabelPath='content.label' optionValuePath='content.value' optionGroupPath='group'}}
If, you just do the above you will get a select list that looks like this:
The only way I could get around this was to sort the array first by the grouped field:
foos: Ember.A([{value: 'foo', label: 'Foo', group: 'Foos'}, {value: 'bar', label: 'Bar', group: 'Bars'}, {value: 'bar2', label: 'Bar2', group: 'Bars'}, {value: 'foo2', label: 'Foo2', group: 'Foos'}]).sortBy("group")

Here's the solution that I came up with. https://gist.github.com/3297425
I had to make two collections. One grouped and the other just content so that the proper option can be selected.
groupedServiceCodes = [Ember.Object.create({label: 'Label for optgroup', content: Ember.A()}), …]
And then flatten the content from groupedServiceCodes down to maintain order for the select:
flattenedServiceCodes = [].concat(object.get('content'), …)
This is a bit of a hack, and I think Ember is wanting for a better solution, but this works for me. I would love to hear thoughts on improving this.

The answers here are a bit outdated. Ember now supports grouping of stuff. Imagine you have:
App.SomeController = Ember.Controller.extend({
content: [{value: 'foo', label: 'Foo', group: 'Foos'}, {value: 'bar', label: 'Bar', group: 'Bars'}]
})
You can then do
Ember.Select contentBinding='content' optionLabelPath='content.label' optionValuePath='content.value' optionGroupPath='group'
Notice that optionGroupPath does not do content.path, just path

Ember.Select does not support optgroups, but you can extend Ember.Select to do so by providing a new template for it and a new template for options. I've done this to support chosen.js selects within Ember.

Ember now supports this out of the box. Refer to this git pull request https://github.com/emberjs/ember.js/pull/2465

Related

vue: can v-html be inside a js array?

I have a js file containing array and objects. I need to style some properties from these arrays. For example
myArray= [
{
id: 1,
name: 'honda',
description: 'to buy the latest car, click here'
},
{
id: 2,
name: 'tesla',
description: 'to buy the latest car, click here'
}
]
Let's say I want to style the description property so that I can bind a link there.
I figure that the way to do it is to use raw html. But in my case, I'm going to include it to my array. Is this possible? I have tried to search this question everywhere but there's no explanation for this case. Thank you so much.
You can use any html styled code in your Array such as below.
myArray= [
{
id: 1,
name: 'honda',
description: 'to buy the latest car, click here'
},
{
id: 2,
name: 'tesla',
description: 'to buy the latest car, click here'
}
]
And in your templete you would use: like
...
<div v-for="car in myArray" :key="car.id">
<p v-html="car.description"></p>
</div>
Check out this code.
Check out this codepen.
You can check out the vuejs documentation for more info.

Ember.js 2.0 Struggling to make hardcoded list available in template

I am currently building an application where a User will have to correct data within a prefilled form.
This form we will have some <select> that can show a selected value that corresponds to the data sent by the server. And sometimes we will not have data corresponding to that select, so we will not preselect any data. The server with never send me the list of all the possible values so I have to hard code then somewhere.
For that, I thought about creating an enumerable in the Route, for each list, and expose those to the Template via the setupController method.
1st question: is this a good way of doing?
2nd question: the code below is what I wrote and it does not work. When I call {{buildingDescriptions.length}} in the template, it does not show anything whereas the console outputs the right value... Also the {{each-in}} loop does not work either.
import Ember from 'ember';
const { computed, RSVP } = Ember;
export default Ember.Route.extend({
buildingDescriptions: computed(function() {
const descriptions = Ember.A([
{value: 'oneFamilyHouse', desc: ''},
{value: 'oneFamilyHouseWithSmallFlat', desc: ''},
{value: 'twoFamilyHouse', desc: ''},
{value: 'twoFamilyHouseWithSmallFlat', desc: ''},
{value: 'threeFamilyHouse', desc: ''},
{value: 'garageOrShelter', desc: ''}
]);
return RSVP.hash({descriptions: descriptions}).then(
function(results) {
Ember.Logger.log(results.descriptions.length);
Ember.Logger.log(results.descriptions);
return results.descriptions;
}
);
}),
model() {
},
setupController(controller) {
Ember.Logger.log(this.get('buildingDescriptions'));
controller.set('buildingDescriptions', this.get('buildingDescriptions'));
}
});
Many thanks in advance. Cheers
EDIT: This is what a simplified version of the code that works now:
import Ember from 'ember';
export default Ember.Route.extend({
buildingDescriptions: Ember.A([
{value: 'oneFamilyHouse', desc: ''},
{value: 'oneFamilyHouseWithSmallFlat', desc: ''},
{value: 'twoFamilyHouse', desc: ''},
{value: 'twoFamilyHouseWithSmallFlat', desc: ''},
{value: 'threeFamilyHouse', desc: ''},
{value: 'garageOrShelter'}
]),
setupController(controller) {
controller.set('buildingDescriptions', this.get('buildingDescriptions'));
}
});

Getting a a.foreach is not a function error

I am trying to build a multiselect list using angular js. I am getting a weird TypeError: a.foreach is not a function and I can’t seem to figure out when.
js :
var myAppModule = angular.module('multiselect', []);
myAppModule.controller("view", function ($scope) {
$scope.listA = {
values: [{
id: 1,
label: 'aLabel',
subItem: {
name: 'aSubItem'
}
}, {
id: 2,
label: 'bLabel',
subItem: {
name: 'bSubItem'
}
}],
selected: {
name: 'aSubItem'
}
};
})
html:
<select multiple ng-options="item.subItem as item.label for item in listA.values track by item.id" ng-model="listA.selected"></select>
I don’t know what I could be doing wrong. Am I casting something wrong ?
The problem is that since you have added the multiple attribute, the value of the select should be an array. So try something similar to this:
$scope.listA = {
values: [{
id: 1,
label: 'aLabel',
subItem: {
name: 'aSubItem'
}
}, {
id: 2,
label: 'bLabel',
subItem: {
name: 'bSubItem'
}
}],
selected: [{
name: 'aSubItem'
}]
};
You need not to track your values by id. it will do it by default.
<div ng-controller="Main">
<select multiple ng-options="item.subItem as item.label for item in listA.values" ng-model="listA.selected"></select>
</div>
JS Fiddle for your code (Fix):
http://jsfiddle.net/juag4okg/
I was having the same problem. It worked well by creating the static json object as answered above, but did not worked out when i tried to fetched the object or list from the server.
Then i realized my server was not sending the json object and i hadn't convert it to json as well. And when i converted it to json it worked perfectly fine.
So if you are having similar problem to display the select options (muliple or single) from the server side data this process might help you.
here is the link to angular angular.toJson() function.

Is it possible to create ng-grid's dynamically?

as part of one of my web projects I need multiple instances of ng-grid in a same app, being the user able to create dynamically as many grids as needed (Think of usual "Add New Grid..") button.
I've gone through the docs and implemented a prototype but I've found that the setup for the grid includes the hardcoded name of the variable containing the data, for example:
in the angular .js controller,
$scope.gridOptions = { data: 'myData' };
and in the html:
<div class="gridStyle" ng-grid="gridOptions">
I've tried using a variable instead, for example:
var mychart = [{...}, {...}]; // data rows
$scope.gridOptions = { data: $scope.mychart };
and
<div class="gridStyle" ng-grid="{{gridOptions}}">
but without success. All the examples I've found on the web use only one table so my question remains unanswered.
Any idea of how this could be resolved or is a major limitation of this grid system?
Thanks!
As usual, when you find yourself needing an arbitrary number of objects, you should think about putting them in a collection rather than giving them individual names. I've updated wayne's jsfiddle to use arrays and ng-repeat: http://jsfiddle.net/Th47J/
Make a grid for each option:
<div ng-repeat="option in gridOptions" class="gridStyle" ng-grid="option"></div>
Obviously if these are being created on the fly, you'll need to update the gridOptions array programmatically rather than having everything hard-coded. But if the data source is the only thing that is changing that's very easy.
Well I finally managed to do this. The key is that the data reference (myData[] in this case) can be generated dynamically (myData + '[' + myData.length + ']' for example), each time a new tab is added.
Of course it's up to you to keep sync between the tabs and the myData length.
The HTML:
<div id="myTabContent" class="tab-content">
<div class="tab-pane" ng-repeat="tab in tabs" id="{{tab.id}}">
<div class="gridStyle" ng-grid="{{tab.ref}}"></div>
</div>
</div>
The angular part:
$scope.tabs = [
{id: 0, title: 'a', contentId: '#0', data: 'lorem 1', ref:{data: 'myData[0]'}},
{id: 1, title: 'b', contentId: '#1', data: 'lorem 2', ref:{data: 'myData[1]'}},
{id: 2, title: 'c', contentId: '#2', data: 'lorem 3', ref:{data: 'myData[2]'}}
];
$scope.myData = [[
{name: "ryan1", surn: "smith1", address:"main square 1", phone:"000000001"},
{name: "ryan1", surn: "smith1", address:"main square 1", phone:"000000001"},
],
[{name: "ryan2", surn: "smith2", address:"main square 2", phone:"000000002"},
{name: "ryan2", surn: "smith2", address:"main square 2", phone:"000000002"},
],
[{name: "ryan3", surn: "smith3", address:"main square 3", phone:"000000003"},
{name: "ryan3", surn: "smith3", address:"main square 3", phone:"000000003"},
],
];

Ext.Js model store objects

How can I adopt my ext js models to hold data containing objects.
Data example:
fruit {
apples {
quantity: '10',
color: 'red
}
pears {
color:'yellow',
taste: 'very bad'
}
How would then my model look like? I only know how to put data on one level:
Ext.define('app.mode.Fruits', {
extend: 'Ext.data.Model',
fields: [
{
name: 'apples'
},
{
name: 'pears'
},
],
Where can I put the other properties? I am working in Sencha Architect.
Take a read through the Ext.data.Field API docs. You'll see a variety of ways in which to configure data on your model.
I think what you're specifically asking for is the mapping config.

Categories

Resources