How to get v-select items inside items? - select

I have a dropdown with v-select
<v-select
label="Select"
v-bind:items="companies"
v-model="e11"
item-text="employee.name"
item-value="name"
max-height="auto"
>
</v-select>
Data from API like
new Vue({
el: '#app',
data () {
return {
e11: [],
companies: [
{
companyName: 'Google',
employee: [{
name: 'Sandra Adams',
name: 'Ali Connors',
name: 'Trevor Hansen',
name: 'Tucker Smith',
}]
},
{
companyName: 'Facebook',
employee: [{
name: 'Sandra Adams',
name: 'Ali Connors',
name: 'Trevor Hansen',
name: 'Tucker Smith',
}]
},
{
companyName: 'Twitter',
employee: [{
name: 'Sandra Adams',
name: 'Ali Connors',
name: 'Trevor Hansen',
name: 'Tucker Smith',
}]
},
]
}
}
})
In dropdown item getting [object object] to fixed fixed it? I want to get dropdown list with grouped by company name.
Here is what I want to do
Here is the codepen link https://codepen.io/yurenlimbu/pen/bGVKGEa?editors=1011

I had a crack at it, as ive also had some issues in the past with vuetify dropdown, i managed to get it to work, you may need to update your current data objects to make the name key unique in the list.
Added computed property to map companies and employees and inject the employees company so we have one object with the data we need
mappedNames: function() {
return this.companies.map(function(person) {
person.employee.forEach(e => e.company = person.companyName)
return person.employee
})
}
Helper function to concat the arrays from the map
item-value prop update to access entire object, and linked to e11 v-model
Here is the codepen:
https://codepen.io/mcwalshh/pen/Vwvdvve
EDIT:
I've included the slot for item and selected item, so you can change how the data is displayed inside the select:
<template v-slot:selection="data">
{{ data.item.name }} - {{ data.item.company }}
</template>
ref: https://vuetifyjs.com/en/components/selects/
Here is the updated codepen:
https://codepen.io/mcwalshh/pen/rNOKqvd
EDIT #2:
Not really sure how to go about adding titles, i suggest reading more vuetify documentation, or perhaps in my example, use the helper in the text-name instead of the items to access companyName. But following from my previous posts, i think the potential way of doing it, is to inject/unshift an object into the employees with:
{ name: person.employee.companyName, type: "title" }
For now in my example i add it straight to the object, then use template v-if's to determine what is a title:
codepen: https://codepen.io/mcwalshh/pen/NWGzeoK
Now just a matter of targeting parent to match your gif, but im in no way saying this is the best approach, like i said before i had issues with vuetify in the past, so i hope this can help get you by until you work out the best method

Related

Proper custom component with complex data in it

I have following interface:
export interface Product {
name: string;
provider: {
name: string;
logo: string;
};
pricePerUnit: {
quantity: number;
currency: string;
};
}
And my rowData looks like this:
rowData = [
{
name: 'Fish',
provider: {
name: 'Amazon',
logo: 'url to amazon logo',
},
pricePerUnit: {
quantity: 5,
currency: 'USD',
},
},
]
So, as you can see i have at least 2 complex object here, and by design I should display provider as img + name and price as quantity + currency symbol.
I`m using custom angular components for that with styling.
Actual problem
In order to provide these object to my custom components, I set field property in colDefs as follow (example for price):
{
headerName: 'Price',
field: 'pricePerUnit',
cellRenderer: PriceCellRendererComponent,
},
And here is the catch, because I specified in field property complex object, I no longer able to visualize data using integrated charts, because for them to work I should specify in my field propery path to number itself, like so:
{
field: 'pricePerUnit.quantity',
}
But now I`ve broke my custom component because params.value now holds just a number and not my complex object. Same goes to provider.
And it`s also broke grouping, sorting, filtering.
html template for one of my custom component (provider) looks like so:
<div class="wrapper provider">
<tui-avatar [avatarUrl]="params.value.logo" class="provider__logo"></tui-avatar>
<div class="provider__name">{{params.value.name}}</div>
</div>
So the question is:
How to properly setup custom components, so they would work in grouping, sorting, filtering and also integrated charts would use just simple primitive like number to correctly display data?

b-form-select get selected option as value

I'm having some trouble understanding b-form-select from bootstrap-vue.
I have a list of object lets say
factories = [{ id: 1, name: "A" }, { id: 2, name: "B" }]`
And my select as
<b-form-select
v-model="factory"
:options="factories"
value-field="id"
text-field="name"
/>
But how would I do to get the full selected object rather than just the id without having to declare options manually ?
This works but it feels 'hacky'.
<b-form-select v-model="factory">
<option v-for="f in factories" :value="f" :key="f.id">{{f.name}}</option>
</b-form-select>
If not possible, any reasons why ?
This doesn't work because <b-form-select> requires the options array to take the form of:
[{value: factory, text: factory.name}]
Put another way, <b-form-select> doesn't know what to do with [{id: 1, name: 'A'}]
To make your first form work, you'll need to transform the array 'factories' into an array the can use:
const factories = [{ id: 1, name: "A" }, { id: 2, name: "B" }]
var factoriesSelectList = []
factories.forEach((factory, index, mechanicsArray) => {
var selectListOption = {
value: factory,
text: factory.name
}
factoriesSelectList.push(selectListOption)
})
Then, in your template:
<b-form-select
v-model="factory"
:options="factoriesSelectList"
/>
Note: This probably isn't much different than doing it in the template as in your second form. I've not looked, but I'm betting the resulting javascript is similar.

Merging two Mongoose query results, without turning them into JSON

I have two Mongoose model schemas setup so that the child documents reference the parent documents, as opposed to the Parent documents having an array of Children documents. (Its like this due to the 16MB size limit restriction on documents, I didnt want to limit the amount of relationships between Parent/Child docs):
// Parent Model Schema
const parentSchema = new Schema({
name: Schema.Types.String
})
// Child Model Schema
const childSchema = new Schema({
name: Schema.Types.String,
_partition: {
type: Schema.Types.ObjectId,
ref: 'Parent'
}
})
I want to create a static method that I can query for a Parent document, then query for any Child documents that match the parent document, then create a new item in the parent document that will reference the Children array.
Basically if the Parent document is:
{
_id: ObjectId('56ba258a98f0767514d0ee0b'),
name: 'Foo'
}
And the child documents are:
[
{
_id: ObjectId('56b9b6a86ea3a0d012bdd062'),
name: 'Name A',
_partition: ObjectId('56ba258a98f0767514d0ee0b')
},{
_id: ObjectId('56ba7e9820accb40239baedf'),
name: 'Name B',
_partition: ObjectId('56ba258a98f0767514d0ee0b')
}
]
Then id be looking to have something like:
{
_id: ObjectId('56ba258a98f0767514d0ee0b'),
name: 'Foo',
children: [
{
_id: ObjectId('56b9b6a86ea3a0d012bdd062'),
name: 'Name A',
_partition: ObjectId('56ba258a98f0767514d0ee0b')
},{
_id: ObjectId('56ba7e9820accb40239baedf'),
name: 'Name B',
_partition: ObjectId('56ba258a98f0767514d0ee0b')
}
]
}
Also, I want them to remain Mongoose documents, so I can update the Parents and Assets if I need to.
I was able to accomplish this by using toJSON on the Parent, then creating a new item that would hold the Child docs, but then obviously the Parent document isn't a real document..
The error I kept getting when I tried this was that I couldnt create a new element in the Document that wasnt in the schema.
I know I could do something like create a Virtual item that would return the promise that would query the Children, but im looking to have one static method, that returns one response (meaning they dont have to handle the virtual item as a promise or callback)
Let me know if this is possible. Thanks!
FYI, this apparently isn't possible, I've tried a few things and it doesn't seem like it will work.
Use the .populate() method of the schema.
Parent.find(query)
.populate("children")
.exec((err, items) => {
if (err) {
...
}
...
});

EmberJS Handling Complex Object returned from REST Api

I have the following user object returned from a REST Api:
{
user: {
id: 3451,
name: "First Last",
favorites: {
designs: {
name: "Design 1",
url: "Url 1"
},
typo: {
name: "Typo 1",
url: "Url 2"
},
games: {
name: "Game 1",
url: "Url 3"
}
}
}
}
I got this response from the url /users/3451.
And here's my User model:
App.User = DS.Model.extend({
name: DS.attr('string'),
favorites: DS.attr(), // ??? What to put here?
});
I have no problem displaying the {{name}}. But in the favorites, I don't know.
I tried using {{#each}}
{{#each favorites}}
{{#key}} - {{name}}
{{/each}}
but no luck.It throws an error: Error: Assertion Failed: The value that #each loops over must be an Array. You passed [object Object]
What is the correct way of handling these kinds of complex objects in EmberJS? Please help.
I think the error is pretty self explanatory: you need to be looping over an array, not an object. Here's how I would convert that object to an array, while saving the key (put this in your model):
favoritesArray: function() {
var favorites = this.get('favorites');
return Em.keys(favorites).map(function(key) {
return {
key: key,
data: favorites[key]
};
});
}.property('favorites.#each.{name,url}')
Then, in your template:
{{#each favoritesArray}}
{{key}} - {{data.name}}
{{/each}}
That would be the easiest way to do it. But if you're looking for a slightly better way (in my opinion), you can user a type transform to convert the data to the format you need at the time of (de)serialization.
EDIT: Just for a bit of background info, I believe the reason that Ember.js doesn't support iterating over objects is because there is no way to bind to the object keys. Ember.js knows to update a bound helper when the dependent key observers are fired, but as far as I know, there is no way to observe the keys of an object. Something like this might be possible using an Map or similar, but I don't think that it's built in functionality.

Understanding Relationships & Foreign Keys in Mongoose

In MongoDB/Mongoose, how do I define a relationship? I think there are a few ways I've seen, but I'm not sure I understand the differences or when do I use which. I am using Mongoose 3
I've defined Todo and TodoList model, where the relationship is obvious. So following the docs http://mongoosejs.com/docs/documents.html, I've defined classes like:
# Todo.coffee
mongoose = require "mongoose"
todoSchema = mongoose.Schema
name: String
desc: String
dueOn: Date
completedOn: Date
module.exports = mongoose.model "Todo", todoSchema
# TodoList.coffee
mongoose = require "mongoose"
Todo = require "./Todo"
todoListSchema = mongoose.Schema
name: String
todos: [Todo.schema]
module.exports = mongoose.model "TodoList", todoListSchema
Then I tried testing the classes:
list = new TodoList
name: "List 1"
todos: [
{ name: "Todo 1", desc: "Hello", dueOn: new Date(2012,10,1), completedOn: new Date(2012,10,2) }
{ name: "Todo 2" }
{ name: "Todo 3", desc: "Hello 2", dueOn: new Date(2012,10,6), completedOn: new Date(2012,10,2) }
{ name: "Todo 4" }
]
#list.todos.push { name: "Todo 5" }
console.log "List", list
list.save (err) ->
if !err
TodoList.find {}, (err, lists) ->
console.log "TODOS"
console.log lists.length, lists
done(err)
else
console.log "ERROR!!!"
done err
When I try to do Todo.find() I get nothing, so the Model (Todo.coffee) is kind of redundant? It looks like Todo are stored in TodoList, as a user I may not care, but I wonder what are the implications? Eg. will the document get too large? 1 TodoList with too many Todos? Does that matter? What if I allow nested Todos (not that I want to do itm just for understanding), is it better to store documents separately then? How do I do that, if I so desire, and when do I do it?
I saw another method, in Mongoose 2 actually. I don't know, if it is possible in 3. Something like using ObjectId instead of nested docs. Maybe thats to store it separately?
I'm still new to Node, Mongoose, and Mongo, but I think I can address at least part of your question. :)
Your current method is the same as I tried doing at first. Basically, it ends up storing it very similarly to this (written in JS, since I don't know CoffeeScript):
var todoListSchema = new mongoose.Schema({
name: String,
todos: [{
name: String,
desc: String,
dueOn: Date,
completedOn: Date
}]
});
I later found this method, which is what I was looking for, and I think what you were intending:
var todoListSchema = new mongoose.Schema({
name: String,
todos: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Todo' //Edit: I'd put the schema. Silly me.
}]
});
This stores an array of ObjectIds, which you can then load using Query#populate in Mongoose.
I don't know of the technical implications, but it makes more sense in my brain if I keep them separate, so that's what I'm doing. :)
Edit: Here is a some official docs that might be useful: http://mongoosejs.com/docs/populate.html