This is for select a city and area, It will filter the same data to show it. But I meet with a problem is that when I selected, the value doesn't appear.
I have been try and searched similar code but still doesn't find the solution.
This is the image when of HTML rendering :
Here is template :
<a href="#" class="list-group-item" v-for="(live , idx) of filterLivingNames" :key="idx">
<h3 class="text-center">{{live.Name}}</h3>
<p>Address:</p>
<p>Phone:</p>
</a>
Here is script
import LivingData from '#/assets/LivingData.json';
export default {
computed: {
livedata(){
return LivingData;
},
filterLivingNames(){
let livelength = this.livedata.length
for(let i = 0 ; i < livelength ; i++){
if(this.livedata[i].Region === this.city && this.livedata[i].Town === this.area){
console.log(this.livedata[i])
return this.livedata[i]
}
else{
continue
}
}
}
}
}
Update json file
[
{
"Id": "1",
"Name": "Hotel-1",
"Region": "Region-1",
"Town": "Town-1",
},
{
"Id": "2",
"Name": "Hotel-2",
"Region": "Region-2",
"Town": "Town-2",
},
{
"Id": "3",
"Name": "Hotel-3",
"Region": "Region-2",
"Town": "Town-1",
},
{
"Id": "4",
"Name": "Hotel-4",
"Region": "Region-1",
"Town": "Town-2",
},
]
As you are filtering out the livedata based on Region and Town. You can simply use the Array.filter() method and this will resolve the issue you are facing as it will return the array.
Live Demo :
new Vue({
el: '#app',
data: {
livedata: [
{
"Id": "1",
"Name": "Hotel-1",
"Region": "Region-1",
"Town": "Town-1",
},
{
"Id": "2",
"Name": "Hotel-2",
"Region": "Region-2",
"Town": "Town-2",
},
{
"Id": "3",
"Name": "Hotel-3",
"Region": "Region-2",
"Town": "Town-1",
},
{
"Id": "4",
"Name": "Hotel-4",
"Region": "Region-1",
"Town": "Town-2",
}
],
city: 'Region-1',
area: 'Town-1'
},
computed: {
filterLivingNames() {
return this.livedata.filter(obj => obj.Region === this.city && obj.Town === this.area)
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<a href="#" class="list-group-item" v-for="(livingData, index) of filterLivingNames" :key="index">
<h3 class="text-center">{{ livingData.Name }}</h3>
<p>Address:</p>
<p>Phone:</p>
</a>
</div>
Related
I have products two types: simple and configurable:
"products" : [
{
"type": "simple",
"id": 1,
"sku": "s1",
"title": "Product 1",
"regular_price": {
"currency": "USD",
"value": 27.12
},
"image": "/images/1.png",
"brand": 9
},
{
"type": "configurable",
"id": 2,
"sku": "c1",
"title": "Product 2",
"regular_price": {
"currency": "USD",
"value": 54.21
},
"image": "/images/conf/default.png",
"configurable_options": [
{
"attribute_id": 93,
"attribute_code": "color",
"label": "Color",
"values": [
{
"label": "Red",
"value_index": 931,
"value": "#ff0000"
},
{
"label": "Blue",
"value_index": 932,
"value": "#0000ff"
},
{
"label": "Black",
"value_index": 933,
"value": "#000"
}
]
},
{
"attribute_code": "size",
"attribute_id": 144,
"position": 0,
"id": 2,
"label": "Size",
"values": [
{
"label": "M",
"value_index": 1441,
"value": 1
},
{
"label": "L",
"value_index": 1442,
"value": 2
}
]
}
],
"variants": [
{
"attributes": [
{
"code": "color",
"value_index": 931
},
{
"code": "size",
"value_index": 1441
}
],
"product": {
"id": 2001,
"sku": "c1-red-m",
"image": "/image/conf/red.png"
}
},
{
"attributes": [
{
"code": "color",
"value_index": 931
},
{
"code": "size",
"value_index": 1442
}
],
"product": {
"id": 2002,
"sku": "c1-red-l",
"image": "/image/conf/red.png"
}
},
{
"attributes": [
{
"code": "color",
"value_index": 932
},
{
"code": "size",
"value_index": 1441
}
],
"product": {
"id": 2003,
"sku": "c1-blue-m",
"image": "/image/conf/blue.png"
}
},
{
"attributes": [
{
"code": "color",
"value_index": 933
},
{
"code": "size",
"value_index": 1442
}
],
"product": {
"id": 2004,
"sku": "c1-black-l",
"image": "/image/conf/black.png"
}
}
],
"brand": 1
}
]
The above data I get with actions (Vuex)
GET_PRODUCTS_FROM_API({ commit }) {
return axios('http://localhost:8080/products', {
method: 'GET',
})
.then((products) => {
commit('SET_PRODUCTS_TO_STATE', products.data);
return products;
})
.catch((e) => {
console.log(e);
return e;
});
}
then I mutate the data:
SET_PRODUCTS_TO_STATE: (state, products) => {
state.products = products
}
and get from in getters
PRODUCTS(state) {
return state.products = state.products.map((product) => {
const brand = state.brands.find((b) => b.id === product.brand)
return {...product, brandName: brand?.title || 'no brand'}
})
}
after which i get the data in the component
At the moment I'm stuck on how to render the color and size attributes of a configurable product. Tell me how to do it right? Do I need to write logic in vuex or parent component?
I tried to push data from parent component to child. But it stopped there again.
I also tried to separate the color and size attributes separately using getters.
For Vuex, the syntax is the following
<template>
<div>
<div v-for="product in products" :key="product.id">
<span>type: {{ product.type }}</span>
<span>type: {{ product.title }}</span>
</div>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
computed: {
...mapGetters(['products']),
...mapGetters('fancyNamespace', ['products']), // if namespaced
},
}
</script>
As of where to call it, directly into the component I guess. Otherwise, as explained here it may not be relevant to use Vuex at all.
PS: you can even rename on the fly if you want.
Solved this issue by Computed Properties and transfer props to child components
computed: {
getAttributeColors() {
let attributes_colors = []
this.product_data.configurable_options.map((item) => {
if(item.label === 'Color') {
attributes_colors.push(item.values)
}
})
return attributes_colors
},
getAttributeSize() {
let attributes_size = []
this.product_data.configurable_options.map((item) => {
if(item.label === 'Size') {
attributes_size.push(item.values)
}
})
return attributes_size
}
}
I have a dropdown as you see in the picture:
The value of this dropdown is coming from the fetched data:
"data": {
"type": "products",
"id": "2021-01-04.1.1",
"attributes": {
"name": "product 1",
"descriptions": "some description",
"image": null,
"date": "2021-01-04",
"valid_from": null,
"valig_untill": null,
"xparc_paid_product_id": 1,
"xparc_unpaid_product_id": 1,
"xparc_access_id": 1,
"stock": null,
"variations": [
{
"id": 1,
"product_template_id": "1",
"name": "child ticket",
"description": "Only for children!",
"price_excl_vat": 1,
"created_at": "2021-09-15T13:16:00.000000Z",
"updated_at": "2021-09-15T13:16:00.000000Z"
},
{
"id": 2,
"product_template_id": "1",
"name": "Adults",
"description": "Not for children!",
"price_excl_vat": 2,
"created_at": "2021-09-15T13:16:10.000000Z",
"updated_at": "2021-09-15T13:16:10.000000Z"
},
{
"id": 3,
"product_template_id": "1",
"name": "Test",
"description": "Test",
"price_excl_vat": 10,
"created_at": "2021-09-30T11:29:44.000000Z",
"updated_at": "2021-09-30T11:29:44.000000Z"
}
]
},
As you see I am displaying variations of this object into my dropdown. What I am trying to achieve is according to the selected value I want to put some info into object (for example this
info: {
"id": 1,
"product_template_id": "1",
"name": "child ticket",
"description": "Only for children!",
"price_excl_vat": 1,
"created_at": "2021-09-15T13:16:00.000000Z",
"updated_at": "2021-09-15T13:16:00.000000Z"
},
)
For this I have a `dropdown component:
<template>
<div class="custom-select" :tabindex="tabindex" #blur="open = false">
<div class="selected" :class="{ open: open }" #click="open = !open">
{{ selected.name }}
</div>
<div class="items" :class="{ selectHide: !open }">
<div
v-for="(option, i) of options"
:key="i"
#click="
selected = option;
open = false;
$emit('input', option);
"
>
{{ option.name }}
</div>
</div>
</div>
</template>
<script>
export default {
props: {
options: {
type: Array,
required: true,
},
default: {
type: String,
required: false,
default: null,
},
tabindex: {
type: Number,
required: false,
default: 0,
},
},
data() {
return {
selected: this.default
? this.default
: this.options.length > 0
? this.options[0]
: null,
open: false,
sel: '',
};
},
mounted() {
this.$emit("input", this.selected);
},
computed: {
variations() {
return this.data.attributes.variations
}
},
methods: {
getSelected(opt) {
this.sel = opt.description
}
}
};
</script>
And a parent of this component, I am trying to fill the object:
<Dropdown
:options="getVariations"
:default="'Choose an option'"
class="select"
#input="getSelected"
/>
<script>
data() {
return {
selectedObject: "",
};
},
computed: {
getVariations() {
return this.product.attributes.variations;
},
getSelected(opt) {
this.selectedObject= opt;
}
},
</script>
But unfortunately, I am not getting anything from selectedObject. Could you please have a look?
Thanks...
You emit selected item data from dropdown component, so set your object in method:
getSelected(opt) {
this.selectedObject = opt
Vue.component('Dropdown', {
template: `
<div class="custom-select" :tabindex="tabindex" #blur="open = false">
<div class="selected" :class="{ open: open }" #click="open = !open">
{{ selected.name }}
</div>
<p>Description child :{{ selected.description }}</p>
<div class="items" :class="{ selectHide: !open }">
<div
v-for="(option, i) of options"
:key="i"
#click="
selected = option;
open = false;
$emit('input', option);
"
>
{{ option.name }}
</div>
</div>
</div>
`,
props: {
options: {
type: Array,
required: true,
},
default: {
type: String,
required: false,
default: null,
},
tabindex: {
type: Number,
required: false,
default: 0,
},
},
data() {
return {
selected: this.default
? this.default
: this.options.length > 0
? this.options[0]
: null,
open: false,
};
},
mounted() {
this.$emit("input", this.selected);
},
})
new Vue({
el: '#demo',
data(){
return {
"data":
{
"type": "products",
"id": "2021-01-04.1.1",
"attributes": {
"name": "product 1",
"descriptions": "some description",
"image": null,
"date": "2021-01-04",
"valid_from": null,
"valig_untill": null,
"xparc_paid_product_id": 1,
"xparc_unpaid_product_id": 1,
"xparc_access_id": 1,
"stock": null,
"variations": [
{
"id": 1,
"product_template_id": "1",
"name": "child ticket",
"description": "Only for children!",
"price_excl_vat": 1,
"created_at": "2021-09-15T13:16:00.000000Z",
"updated_at": "2021-09-15T13:16:00.000000Z"
},
{
"id": 2,
"product_template_id": "1",
"name": "Adults",
"description": "Not for children!",
"price_excl_vat": 2,
"created_at": "2021-09-15T13:16:10.000000Z",
"updated_at": "2021-09-15T13:16:10.000000Z"
},
{
"id": 3,
"product_template_id": "1",
"name": "Test",
"description": "Test",
"price_excl_vat": 10,
"created_at": "2021-09-30T11:29:44.000000Z",
"updated_at": "2021-09-30T11:29:44.000000Z"
}
]
},
},
sel: '',
selectedObject: null
}
},
computed: {
variations() {
return this.data.attributes.variations
}
},
methods: {
getSelected(opt) {
this.selectedObject = opt
this.sel = opt.description
}
}
})
Vue.config.productionTip = false
Vue.config.devtools = false
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<h3>selected object</h3>
<p>{{ selectedObject }}</p>
<Dropdown
:options="variations"
:default="'Choose an option'"
class="select"
#input="getSelected"
/>
</div>
I want to list the states of the country in 2nd selection (JSON) format when country is selected in 1 selection I want to make with vuejs. I can do this with normal select but I want to do it in "select2" action. I have not seen or encountered an example of it anywhere.
For example, when France is chosen as the country. in the second option i want the states of france to be listed
Please answer considering that you are a beginner when writing an answer.
select2 or vue-select
[
{
"country": "America",
"plate": 1
},
{
"country": "France",
"plate": 2
},
{
"country": "Holland",
"plate": 3
},
{
"country": "China",
"plate": 4
},
{
"country": "state",
"plate": 5
}
]
[
{
"id": 1,
"country": "America",
"state": "Texas"
},
{
"id": 2,
"country": "America",
"state": "NewMexico"
},
{
"id": 3,
"country": "America",
"state": "Montana"
},
{
"id": 4,
"country": "France",
"state": "Normandy"
},
{
"id": 5,
"country": "France",
"state": "Paris"
},
{
"id": 6,
"country": "France",
"state": "Occitanie"
},
{
"id": 7,
"country": "Holland",
"state": "Köln"
},
{
"id": 8,
"country": "Holland",
"state": "Brüksel"
},
{
"id": 9,
"country": "China",
"state": "Gansu"
},
{
"id": 10,
"country": "China",
"state": "Hubei"
},
{
"id": 11,
"country": "China",
"state": "Hainan"
},
{
"id": 12,
"country": "Iraq",
"state": "Erbil"
},
{
"id": 13,
"country": "Iraq",
"state": "Bagdat"
}
]
<template>
<div class="container">
<div class="row">
<div class="col-md-4">
<label class="my-1 mr-2">countries</label>
<select class="custom-select my-1 mr-sm-2 form-control" v-model="selected" #change="getState()">
<option v-if="selected === 'Choose'">Choose</option>
<option v-for="item in countries" v-bind:key="item.id">{{ item.country}}
</option>
</select>
</div>
<div class="col-md-4 ">
<label class="my-1 mr-2">state</label>
<select class="custom-select my-1 mr-sm-2 form-control">
<option v-if="selectedCountry === false">Choose</option>
<option v-for="list in states" v-bind:key="id">{{ list.state}}</option>
</select>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
selected: 'Choose',
countries: {},
states: [],
selectedCountry: false,
}
},
created() {
this.getCounty()
},
methods: {
getCounty() {
axios.get('json/country.json')
.then(res => this.countries = res.data)
},
getState() {
this.selectedCountry = true
this.states = []
axios.get('json/states.json')
.then(res => {
res.data.forEach(i => {
if (this.selected === i.country) {
this.states.push(i.state)
}
})
})
}
}
}
</script>
You can prepare data and use vue-multiselect :
Vue.config.productionTip = false
new Vue({
el: '#demo',
components: { Multiselect: window.VueMultiselect.default },
data() {
return {
options: [
{"id": 1,"country": "America", "plate": 1}, {"id": 2,"country": "France","plate": 2},
{"id": 3,"country": "Holland","plate": 3}, {"id": 4,"country": "China","plate": 4},
{"id": 5,"country": "state","plate": 5}
],
states: [
{"id": 1,"country": "America","state": "Texas"},
{"id": 2,"country": "America","state": "NewMexico"},
{"id": 3,"country": "America","state": "Montana"},
{"id": 4,"country": "France","state": "Normandy"},
{"id": 5,"country": "France","state": "Paris"},
{"id": 6,"country": "France", "state": "Occitanie" },
{"id": 7,"country": "Holland","state": "Köln"},
{"id": 8,"country": "Holland","state": "Brüksel"},
{"id": 9,"country": "China","state": "Gansu"},
{"id": 10,"country": "China","state": "Hubei"},
{"id": 11,"country": "China","state": "Hainan"},
{"id": 12,"country": "Iraq","state": "Erbil"},
{"id": 13,"country": "Iraq","state": "Bagdat"}
],
value: '',
statesSel: [],
selectedCountry: '',
}
},
methods: {
getCounty() {
//axios.get('json/country.json')
//.then(res => this.countries = res.data)
},
getStates() {
//axios.get('json/states.json')
//.then(res => {this.states.push(i.state)
},
updateSelected: function(newValue) {
this.selectedCountry = '';
this.statesSel = this.states.filter(s => s.country === newValue.country)
}
},
created() {
this.getCounty()
this.getStates()
},
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<link rel="stylesheet" href="https://unpkg.com/vue-multiselect#2.1.0/dist/vue-multiselect.min.css">
<script src="https://unpkg.com/vue-multiselect#2.1.0"></script>
<div id="demo">
<div class="row">
<div class="col-md-4 col-md-ofset-2 form-group">
<label class="typo__label">Select with search</label>
<multiselect v-model="value" placeholder="İl" :multiple="false" :options="options" label="country" track-by="country" :allow-empty="false" #select="updateSelected">
</multiselect>
</div>
<div class="col-md-4 col-md-ofset-2 form-group">
<label class="typo__label">Select with search</label>
<multiselect v-model="selectedCountry" placeholder="İlçe" :multiple="false" :options="statesSel" label="state" track-by="state" :allow-empty="false">
</multiselect>
</div>
</div>
</div>
I used this rule to calculate the minimum purchase quantity and as long as this is not reached the form cannot be sent. The calculation itself works, but the first element is always "undefined".
Because the first element is "undefined", the number in the function getSizesQuantity is not correct if the value of the input field is 0.
What could be the reason for this?
product.json
{
"sku": "12345",
"name": "Product Name",
"variants": [
{
"color": "White",
"sku": "246420009",
"colorimage": "path/to/image",
"sizes": [
{
"size": "XS",
"sku": "246420002"
},
{
"size": "S",
"sku": "246420003"
},
{
"size": "M",
"sku": "246420004"
},
{
"size": "L",
"sku": "246420005"
},
{
"size": "XL",
"sku": "246420006"
},
{
"size": "2XL",
"sku": "246420007"
},
{
"size": "3XL",
"sku": "246420008"
},
{
"size": "4XL",
"sku": "246420009"
}
],
"images": [
{
"look": "front",
"packshot": "path/to/image"
}
]
}
]
}
<v-form
ref="form"
v-model="isValid"
lazy-validation
#submit.prevent="onSubmit">
<v-form ref="sizeform">
<div class="grid-row sizes-col">
<template v-for="(size, index) in variant.selected.sizes">
<v-text-field
ref="sizeInputs"
:key="size.sku"
v-model.number="variantSize[index]"
:rules="sizeRules"
:label="size.size"
type="number"
min="0"
#change="(value) => setVariantSize(value, size, index)"
required />
</template>
</div>
</v-form>
<div class="small muted">
* Mindestabnahme Menge 25 Stück / {{ getSizesQuantity }}
</div>
</v-form>
data() {
return {
variant: this.activeVariant,
variantSize: [],
minimumSizesQuantity: 25,
sizeQuantity: 0,
sizeRules: [ (v) => {
let totalQuantity = 0;
console.log(this.$refs.sizeInputs && this.$refs.sizeInputs.length);
if (this.$refs.sizeInputs && this.$refs.sizeInputs.length > 0) {
this.$refs.sizeInputs.forEach((input) => {
if (input.value) {
totalQuantity += parseInt(input.value, 10);
this.sizeQuantity = totalQuantity;
}
});
}
return totalQuantity >= this.minimumSizesQuantity;
}
],
};
},
computed: {
getSizesQuantity() {
return this.minimumSizesQuantity - this.sizeQuantity;
},
},
this is the output of console.log(this.$refs.sizeInputs && this.$refs.sizeInputs.length);
undefined
1
2
3
4
5
6
7
This is driving me crazy, I have nested data in an array and have not been successful in getting it to render using components, I am able to render it without a component though.
var data2 = {
"team": [{
"id":0,
"author": "Johnnie Walker",
"title": "Aging Your Own Whisky",
"content": "A bunch of steps and a whole lot of content",
"members": [
{
"id": "0",
"name": "name 1",
"text": "bio"
},
{
"id": "1",
"name": "name 2",
"text": "bio"
}
]
},
{
"id":1,
"author": "Captain Morgan",
"title": "Rum stories",
"content": "A bunch of steps and a whole lot of contentttt",
"members": [
{
"id": "3",
"name": "name 3",
"text": "bio"
}
]
}
]}
What I am trying to do is loop through members, here is my current code
index.html
<div id="app2">
<entry v-for="t in team" v-bind:cat="t" v-bind:key="t.id">
<detail v-for="mem in t.members" v-bind:ember="mem" v-bind:key="mem.id"></detail>
</entry>
</div>
and here is my js file
Vue.component('entry', {
props:['cat'],
template: '<div>{{cat.author}}</div>'
})
Vue.component('detail', {
props:['ember'],
template: '<div>{{ember.id}}</div>',
})
var vm2 = new Vue({
el: '#app2',
data: function() {
console.log(data2.team)
return data2;
}
});
The data in entry renders but nothing happens with detail, no warnings either, how do I proceed?
Note: When I approach it this way it DOES work, but this is not using a component:
var vm = new Vue({
el: '#app',
data: function() {
console.log(data2.team)
return data2;
}
});
and the html
<div id="app">
<div v-for="t in team" v-bind:key="t.id" v-bind:author="t.author">
{{t.author}}
<div v-for="m in t.members" v-bind:key="m.id">
{{m.name}}
</div>
</div>
</div>
You either need to add the detail component to the entry template, or you need to add a slot to the entry component.
Here is your code modified to use a slot.
console.clear()
var data2 = {
"team": [{
"id": 0,
"author": "Johnnie Walker",
"title": "Aging Your Own Whisky",
"content": "A bunch of steps and a whole lot of content",
"members": [{
"id": "0",
"name": "name 1",
"text": "bio"
},
{
"id": "1",
"name": "name 2",
"text": "bio"
}
]
},
{
"id": 1,
"author": "Captain Morgan",
"title": "Rum stories",
"content": "A bunch of steps and a whole lot of contentttt",
"members": [{
"id": "3",
"name": "name 3",
"text": "bio"
}]
}
]
}
Vue.component('entry', {
props: ['cat'],
template: '<div>{{cat.author}}<slot/></div>'
})
Vue.component('detail', {
props: ['ember'],
template: '<div>{{ember.id}}</div>',
})
var vm2 = new Vue({
el: '#app2',
data: function() {
console.log(data2.team)
return data2;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>
<div id="app2">
<entry v-for="t in team" v-bind:cat="t" v-bind:key="t.id">
<detail v-for="mem in t.members" v-bind:ember="mem" v-bind:key="mem.id">
</detail>
</entry>
</div>
And here it is modifying the entry component to include the detail.
console.clear()
var data2 = {
"team": [{
"id": 0,
"author": "Johnnie Walker",
"title": "Aging Your Own Whisky",
"content": "A bunch of steps and a whole lot of content",
"members": [{
"id": "0",
"name": "name 1",
"text": "bio"
},
{
"id": "1",
"name": "name 2",
"text": "bio"
}
]
},
{
"id": 1,
"author": "Captain Morgan",
"title": "Rum stories",
"content": "A bunch of steps and a whole lot of contentttt",
"members": [{
"id": "3",
"name": "name 3",
"text": "bio"
}]
}
]
}
Vue.component('detail', {
props: ['ember'],
template: '<div>{{ember.id}}</div>',
})
Vue.component('entry', {
props: ['cat'],
template: `<div>
{{cat.author}}
<detail v-for="mem in cat.members" v-bind:ember="mem" v-bind:key="mem.id">
</detail>
</div>`
})
var vm2 = new Vue({
el: '#app2',
data: function() {
return data2;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>
<div id="app2">
<entry v-for="t in team" v-bind:cat="t" v-bind:key="t.id"></entry>
</div>