Vue - input with multiple checkboxes - html

I'm rendering some checkboxes based on an array and using a data attribute as the v-model. I'm using Vue2.
However, I end up having all checkboxes checked for some reason, when the value of the v-model equals 1 (I guess it treats it as a bool instead of a number).
I tried v-model.number - without any luck. What am I doing wrong?
My template:
<div v-for="category in categories">
<input
type="checkbox"
v-model.number="item.category"
:id="'category_' + category.id"
:value="category.id"
#change="save"
/>
<label>{{ item.category }} : {{ category.id }}</label>
</div>
Model Data (item.category):
1
Categories:
[
{
"id": 2,
"name": "news Category 0"
},
{
"id": 7,
"name": "news Category 1"
},
{
"id": 12,
"name": "news Category 2"
},
{
"id": 17,
"name": "news Category 3"
},
{
"id": 22,
"name": "news Category 4"
},
{
"id": 27,
"name": "news Category 5"
},
// other values
]
Screenshot (Ive added item.category and category.id as label text to make it more clear):

As you are using Multiple checkboxes, you have to give an array in v-model, so your item.category has to be an array: [1].
See the working fiddle: https://jsfiddle.net/mimani/y36f3cbm/
var demo = new Vue({
el: '#demo',
data() {
return {
categories: [{
"id": 2,
"name": "news Category 0"
}, {
"id": 92,
"name": "news Category 8"
}, {
"id": 97,
"name": "news Category 9"
}],
item: {
category: [1]
}
};
}
})

Related

How to Translate data in array of objects with .map() using react-i18next

Just started using i18next and having a bit of issues translating the following data.
export const educationData = [
{ education: "Some education 1", timeframe: "2017 - 2018", id: 1 },
{ education: "Some education 2", timeframe: "2016 - 2017", id: 2 },
{ education: "Some education 3", timeframe: "2015 - 2016", id: 3 },
{ education: "Some education 4", timeframe: "2014 - 2015", id: 4 }
];
JSON in locales for different languages looks like:
"education": {
"heading": "Education",
"educationData": [
{
"id": 1,
"education": "Some education 1",
"timeframe": "2017 - 2018"
},
{
"id": 2,
"education": "Some education 2",
"timeframe": "2016 - 2017"
},
{
"id": 3,
"education": "Some education 3",
"timeframe": "2015 - 2016"
},
{
"id": 4,
"education": "Some education 4",
"timeframe": "2014 - 2015"
}
]
}
Component looks like:
import React from "react";
import { useTranslation } from "react-i18next";
import { educationData } from "../data/educationData";
import Education from "./Education.js";
function ListEducation() {
const { t } = useTranslation();
return (
<div className="education py-1">
<h2>{t("education.heading")}</h2>
<hr />
{educationData.map((edu) => (
<Education
key={edu.id}
education={edu.education}
timeframe={edu.timeframe}
/>
))}
</div>
);
}
export default ListEducation;
How do i get the translations to work in the .map() function? Outside of .map() something like t("education.educationData.0.education") works fine.
Inside .map function it just outputs "education.educationData.0.education" as a string.
You can get access to an array from file with translations using:
t('education.educationData', { returnObjects: true })
and then easily map through this array.
Source: i18next documentation: Arrays
You can try this
{t(`education.educationData.${id}.${education}`)}
You're basically concatenating the string.

How to dynamically add object inside json array?

I'm trying to dynamically add an object inside every object which is present in my json array. But I'm unable to do so. My object is getting appended at the end of json which is not what I want.
jsonArray:any=[
{
"id": 1000,
"body": "some comment",
"postId": 1
},
{
"id": 2,
"body": "some comment",
"postId": 1
},
{
"id": 3,
"body": "some comment",
"postId": 1
}
]
selectFLag:any={"selected":"true"}
temArray:any;
learnJSONPArse()
{
for (var i = 0; this.jsonArray.length > i; i++)
{
Alert(this.jsonArray.length)
}
}
this.jsonArray.push(this.selectFLag)
-----expected output is
[
{
"id": 1000,
"body": "some comment",
"postId": 1,
"selected":"true"
},
{
"id": 2,
"body": "some comment",
"postId": 1,
"selected":"true"
},
{
"id": 3,
"body": "some comment",
"postId": 1,
"selected":"true"
}
]
You're question is a little unclear, but it sounds like you want to map each item in your array to a new item. The new item is the same as the old one, but assigned an additional property.
If so, something like this could work for you:
const objToAppend = { selected: true };
jsonArray.map(item => Object.assign(item, objToAppend));
Refs:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Bootstrap Typeahead search issue with data contains HTML

I am facing issue with bootstrap typeahead with data contains HTML angular brackets. My data is like
var jsonData = [
{
"id": 1,
"name": "<Andrew> Pougher"
},
{
"id": 2,
"name": "Michele Moore"
},
{
"id": 3,
"name": "Michele Boob"
},
{
"id": 4,
"name": "Michael Moore"
},
{
"id": 5,
"name": "George Michael"
}
]
If I type "u" in input field, it will create tag in HTML and values search dropdown displayes with value like "Pougher" instead of < Andrew > Pougher

Map two Collections (Inner Join) using AngularJS HTML

I'm having two Collection user and Type. I wish to dispay Email ID which is marked as IsPreffered = TRUE and I need to map the email.Type to Type.ID
{ "user" : [
{
"Name" : "B. Balamanigandan",
"Email": [
{
"Id": "bala#gmail.com",
"IsPreffered": true,
"Type": 1,
},
{
"Id": "mani#gmail.com",
"IsPreffered": false,
"Type": 2,
}
]}
]};
{
"Type": [
{
"ID": 1,
"Name": "Private"
},
{
"ID": 2,
"Name": "Home"
},
{
"ID": 3,
"Name": "Business"
},
]}
HTML Source Code:
<span ng-repeat="email in collection.user[0].Email | filter: {IsPreffered : true} " ng-if="$last"> {{email.Id}}</span>
Here I need to Specify the Type "Private" for the above HTML. How to Map the user[0].Email.Type to Type.ID and fetch the appropriate Type.Name
Kindly filter the condition using HTML Level not in JavaScript Level. Kindly assist me.
I would suggest doing it in code rather than the markup, but if you need this is one way you can map to Type:
<span
ng-repeat="email in collection.user[0].Email| filter: {IsPreffered : true} "
ng-if="$last">
Email : {{email.Id}} ,
<span ng-repeat="typename in typeCollection.Type|filter:{ID : email.Type}">
Type: {{typename.Name}}
</span>
</span>
where typeCollection is :
$scope.typeCollection = {
"Type": [{
"ID": 1,
"Name": "Private"
}, {
"ID": 2,
"Name": "Home"
}, {
"ID": 3,
"Name": "Business"
}, ]
};
Please note that you needed one more ng-repeat in markup because filter works on an array.
EDIT:
You can also do this using ng-init:
<span
ng-repeat="email in collection.user[0].Email| filter: {IsPreffered : true} "
ng-if="$last">
Email : {{email.Id}} ,
<span ng-init="types = (typeCollection.Type|filter:{ID : email.Type})">
Type: {{types[0].Name}}
</span>
</span>
for(var i = 0; i < Type.length ; i++)
{
if(user[0].Email.Type to Type[i].ID)
{
user[0].Email.TypeName = Type[i].ID;
}
};
Html
<span ng-repeat="email in collection.user[0].Email | filter: {IsPreffered : true} " ng-if="$last">{{email.TypeName}} : {{email.Id}}</span>

How to get value from two json files in angularjs

Can anyone please help to solve my problems.
in angular js. I have below json and HTML file
test.json
{
"list": [
{
"id": 1,
"Name": "Name 1"
},
{
"id": 2,
"Name": "Name 2"
}
]
}
array.json
{
"list": [
{
"id": 1,
"time": [
{
"time1": "10.00am",
"time2": "10.10am",
"time3": "10.20am",
"time4": "10.30am"
}
]
}
I need output like below
id: 1
Name: Name1
time: 10.00am, 10.10am, 10.20am, 10.30am
id: 1
Name: Name1
time: 10.00am, 10.10am
Thanks in Advance
HTML
<div class="abc" ng-repeat="data in datas[0].list">
ID:{{data.id}}<br>
Name:{{data.Name}}<br>
Time:
<ANY ng-repeat="times in data.time[0]">
{{times}}
</ANY>
<hr>
</div>
Controller
$scope.datas=[{
"list": [
{
"id": 1,
"Name": "Name 1",
"time": [
{
"time1": "10.00am",
"time2": "10.10am",
"time3": "10.20am",
"time4": "10.30am"
}
]
},
{
"id": 2,
"Name": "Name 2",
"time": [
{
"time1": "10.00am",
"time2": "10.10am"
}
]
}
]
}];
You can use ng-repeat in your case something link below.
<span ng-repeat="list in myList">
Id : {{list.id}}<br/>
Name : {{list.Name}}<br/>
<span ng-repeat="tm in times | filter:{id: list.id}">
Time : <span ng-repeat="tim in tm.time">
<span ng-repeat="t in tim">
{{t}}{{$last ? '' : ', '}}
<span>
</span>
</span><br/>
</span>
Here is the working pen for you.
Codepen