Unable to make Multiselect display data properly in VueJS 2 - javascript

I have a problem by which I'm unable to make Vue-Multiselect display what I want properly.
Current Behaviour:
Here is my code:
<multiselect
v-model="displayCategories"
:selected="displayCategories"
:options="optionsForSubCat"
:searchable="false"
:allow-empty="true"
deselect-label="Can't remove this value"
label="subCategories"
track-by="subCategories"
>
</multiselect>
This is my json object to display the Type of Sub-Category:
"displayCategories": {
"categoryId": "3080854a-13d9-4e38-ab96-358aa6405a2c",
"categoryName": "Furniture",
"subCategories": [
{
"subCategoryName": "Sofa",
"subCategoryId": "21eb061d-9c67-4c55-ac48-e33e684a307c",
"createdAt": "2017-07-24T13:07:18.000Z",
"updatedAt": "2017-07-24T13:08:28.000Z",
"fk_categoryId": "3080854a-13d9-4e38-ab96-358aa6405a2c"
},
{
"subCategoryName": "Bed",
"subCategoryId": "2af11eee-5fce-4d97-b483-00a02de123ca",
"createdAt": "2017-07-24T13:06:04.000Z",
"updatedAt": "2017-07-24T13:08:28.000Z",
"fk_categoryId": "3080854a-13d9-4e38-ab96-358aa6405a2c"
}
]
}
Even if i changed the label and track-by to "subCategoryName", it does not even show the dropdown at all.
The expected behaviour would be Multiselect would show "Sofa", "Bed" in the dropdown.
How do i go about achieving the expected behaviour?
Thank you for your help!

Related

MaterialUI DropDownMenu Multiple select using react JS

Am facing issue with the multiselect dropdown, Can someone help me out.While configuring the API through Dropdown, Filter API need to be filter for the all 5 input multiselects based on the initial Dropdown After the filter should get the next avaliable data should be reflect in the Next Dropdown selector.
{
"input_data": [],
"filter_data": {
"customers": [
"Asda"
],
"sub_categories": [
"CAT CARE & TREATS"
],
"brands": [
"DREAMIES"
],
"promo_groups": [
"Dreamies 200g"
],
"years": [
"2021"
],
"quarters": [
"Q2"
]
},
"level": "feature_type"
}

How to use v-for 2 times to get specific values in check-box action form?

I am new to Vue js. I am trying to do an action form for the rest of API. I don't know how to get the data of name and location only. I was trying to use slice in Array, but it does not work.
My action form:
<div class="form-group">
<label class="required"> Social Media </label>
<b-form-checkbox v-for="event in events" :key="event._id" :value="event" v-model="selected">
{{event.name}}, {{event.location}}
</b-form-checkbox>
<span class="mt-3">Selected: <strong>{{ selected }}</strong></span>
</div>
My Vue instance
export default {
data() {
return {
events: [{
"_id": "d4d81da6-b453-4a31-999f-a2ea04848ee9",
"name": "A",
"location": "US",
"__v": 0
},
{
"_id": "91205d34-4480-4e4e-bdf7-fe66e46922b0",
"name": "B",
"location": "Korea",
"__v": 0
},
{
"_id": "0b168c44-4f38-4f86-8ee6-e077333aca95",
"name": "C",
"location": "Japan",
"__v": 0
}],
selected: ''
};
}
}
The Output when checking the first option of the checkbox:
Selected: ["_id": "d4d81da6-b453-4a31-999f-a2ea04848ee9", "name": "A", "location": "US", "__v": 0]
Expected output when checking the first option of the checkbox:
Selected: [ "name": "A", "location": "US" ]
You can create the necessary structure within the :value="" assignment.
<b-form-checkbox v-for="event in events" :key="event._id" :value="{ name: event.name, location: event.location }" v-model="selected">
{{event.name}}, {{event.location}}
</b-form-checkbox>
Firstly make Selected:false boolean ... then make a button and on click it'll get to a function which will accepts a parameter, iterate your array and select an object which is matching with the parameter
private selectFun(item){this.events.filter(val=>{val._id===item._id})//and then whatever}

Which is the best way to filter data from an array with Vue JS?

I'm looking for best practices.
I get this product list from my API:
[
{
"id": 6,
"name": "Pechuga de pollo",
"category": "Chicken",
"existencia": 100
},
{
"id": 7,
"name": "Pierna de pavo",
"category": "Chicken",
"existencia": 100
},
{
"id": 8,
"name": "Lonja de pescado",
"category": "Fish",
"existencia": 200
},
{
"id": 9,
"name": "Coca Cola",
"category": "Soda",
"existencia": 200
},
{
"id": 10,
"name": "Jugo de naranja",
"category": "Juice",
"existencia": 200
}
]
So I need to filter this array of products by the value "Chicken".
I've tried with the filter() method and It works well.
this.chickenProducts =productList.filter(product=>product.category=="Chicken")
Then I use the v-for directive with the chickenProducts array to fill the select input that only contains chicken products.
Do you know another way to filter data from an array to fill a select input considering a criteria in Vue JS?
I'm practicing with Vue yet.
computed filter array?
By the way, I remember Vue1 can used filterBy in v-for, but Vue2 removed it.
Your way of implementation is a bit strict. What if you would want to change your filtering condition? or have multiple condition in your code?
If I were you, I would create a method which returns an array based on condition passed into it. the I iterate over that result. This is more straightforward and modular in regards of maintenance and extensibility.

Always keep selected data in new Array (React Native)

I need help because I'm losing my mind haha ...
I have the main array products with this (it's just a sample) :
[
{
"from": "country",
"maker": "name of maker",
"id": "1969",
"image": "image.jpg",
"label": "355",
"name": "name of product",
"price": "12.90",
"subscriber_price": "8.90",
"url_path": "url",
"occasion": null,
"colour": "31",
"origin": "397",
},
{
"from": "country",
"maker": "name of maker",
"id": "2043",
"image": "image.jpg",
"label": "362",
"name": "name of product",
"price": "24.90",
"subscriber_price": "24.90",
"url_path": "url",
"occasion": "51,376,155,39",
"colour": "31",
"origin": "395"
}
]
I'm working this the Picker Component. So, what I'm doing is :
I have a Picker to select products with their "colour".. then I have another one to filter the selected products (only with colour:31 for example) with their "origin" and finally I want to filter them through their "label" ...
The fact is I have 3 Pickers, 3 functions to select them and it's working but the problem is I'm erasing with a setState my render of "displayProducts". So, when I have selected the 3 options, I can't go back..
For example, I choose "colour:31" with "origin:397" and "label:355" .. I can't go back and tell : finally I want "origin:395" because it doesn't exist anymore, etc... and one "colour" can have different "label, origin, ..."
I'm doing something like this but it's only available for ONE option and not multiple options and without keeping a solution to find again my filtered products :
onChangeGetOrigin(originValue) {
this.setState(() => ({
activeOrigin: originValue,
displayProducts: this.state.displayProducts.filter(product => product.origin == originValue)
}));
}
Do anyone can understand what I'm saying ? :-D
You can maintain two arrays. One contains the complete list of products and the other one is a derived array after applying the filters. You can use the derived list for display and original array for selection.

Set list items in ng-model in a dropdownmenu angularjs

currently I stuck with an angularjs problem. The following scenario:
I have a Game entity and a Team entity. One Game can hold multiple Team objects. (ManyToMany Relationship)
In my frontend application I want to add a new Game with optional Teams. For the Teams I decided to use two dropdown-menus (each menu for one team).
Now I am not able to give the ng-model the correct values.
I tried something like this but it might be wrong:
<select class="form-control" ng-options="team.teamname for team in teams
track by team.id" ng-model="game.teams[0]">
<option value="" selected>-- No Team --</option>
</select>
<select class="form-control" ng-options="team.teamname for team in teams
track by team.id" ng-model="game.teams[1]">
<option value="" selected>-- No Team --</option>
</select>
As soon as I click the save button i get an error message "400: Unable to process JSON":
Possibly unhandled rejection: {"data":{"code":400,"message":"Unable to process JSON"},"status":400,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","data":{"teams":{"0":{"id":1,"games":[{"id":1,"date":20180810}],"teamname":"BVB"},"1":{"id":2,"games":[{"id":1,"date":20180810}],"teamname":"FCB"}},"date":"20180810"},"url":"/api/games","headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}},"statusText":"Bad Request"}
When creating the two Teams for a Game with Postman it works:
{
"date": 20180810,
"teams": [{"id": 1}, {"id": 2}]
}
Output:
{
"id": 1,
"date": 20180810,
"teams": [
{
"id": 1,
"games": [
{
"id": 1,
"date": 20180810
}
],
"teamname": "BVB"
},
{
"id": 2,
"games": [
{
"id": 1,
"date": 20180810
}
],
"teamname": "FCB"
}
]
}
Any suggestions how to set the ng-model (Game) with the values of the first and second dropdown-menus? thanks
I have added a screenshot of the form that i want to have:
If i understand well, when you do the request with postman, you send this:
{
"date": 20180810,
"teams": [{"id": 1}, {"id": 2}]
}
But your angular form is actually sending this:
{
"date": "20180810"
"teams": {
"0": {
"id": 1,
"games": [
{
"id": 1,
"date": 20180810
}
],
"teamname": "BVB"
},
"1": {
"id": 2,
"games": [
{
"id": 1,
"date": 20180810
}
],
"teamname": "FCB"
}
}
}
While the date looks fine, your teams is an object but your backend is expecting an array. You also have additional informations in your teams (like games and teamname, but i can't know if this is a problem for your backend.
Most of the time this kind of problems with Angularjs are not in the html template but in the controller. In your controller you should have something like this:
scope.game = {
date: 'something',
teams: []
};
I think your problem is that you don't initialize scope.game.teams as an array properly. So ng-model just initliaze it by itself with an object.
From what i gather you want to add multiple teams for each game, follow the checklist model, its probably what you want, https://vitalets.github.io/checklist-model/ from there you can pass the server a list of games for each team

Categories

Resources