Vue auto select checkbox according to another array - json

So i have 2 different api request in my vue application. One of them is bringing the all questions.
[ { "id": 20, "question": "Cigarette" }, { "id": 2, "question":
"Alcohol" }, { "id": 3, "question": "Diabetes" }]
In second request is returning what client has checked from the form about these questions.
{ "cigarette": "yes", "alcohol": "yes", "mobile": "+44111111111"}
and so on...
In my form.js file i want to see the checkbox has checked if client has selected that checkbox. In for loop i have this
<li v-for="(ask, askey) in patientQuestions" :key="askey">
<b-form-checkbox v-model="ask.value">{{ ask.question }}</b-form-checkbox>
</li>
How can i auto select this checkboxes. Thanks in advance

It's more difficult than it needs to be because you need something to relate the answer to the question. You'd want something that's the same in both cases and you don't really have that right now. The answers key and question text almost match, but one is lowercase and the other one is not. It would be easier/more reliable if you could for example get the question ID in the answer object.
Given your current data structure, you could do this:
<div id="app">
<ul>
<li v-for="(pq, index) in patientQuestions" :key="pq.id">
<b-form-checkbox value="yes" v-model="answers[pq.question.toLowerCase()]">{{ pq.question }}</b-form-checkbox>
</li>
</ul>
</div>
var app = new Vue({
el: '#app',
data: {
patientQuestions: [{
"id": 20,
"question": "Cigarette"
},
{
"id": 2,
"question": "Alcohol"
},
{
"id": 3,
"question": "Diabetes"
}
],
answers: {
"cigarette": "yes",
"alcohol": "yes",
"mobile": "+44111111111"
}
}
})
Fiddle: https://jsfiddle.net/thebluenile/1bheo648/

Related

JSON Database doesn't work correctly as per REST technique

I created a JSON server and this is the data that I'm using. However, when I'm trying to query the examlist and relate it to the students (i'd like to receive the students based on their ID (the picture below shows the REST query - I'm using ?_expand=student )) it won't display them. The code shows correct as per JSON validators, but my goal is to have them working.
The way my data is organized (the examlist table) won't display the students, because apparently, it cannot read their IDs. This database will be used for HTTP requests, hence I need it fully working.
I'll upload another image so that you can visualize my code.
Momentarily instead of my studentIDs, it's showing some random 0,1 numbers and the student IDs are being pushed down along the arbitrary tree.
(Just the examlist "table")
It's M:M relationship (relational database) and how I want it structured is:
Table "students" that contains information about the students;
I have "table" exams that contains information about the exams;
And then I have another "table" examlist which contains information about the EXAM (ExamID) and the students enrolled in it (basically relates the two abovementioned tables)
When I try querying the students through the "examlist" table, it won't work. However, the other "table" -- exam, does work.
My assumption is the way I have organized the students in the examlist "table" is not good, however, given my very little experience I cannot seem to see where the issue is.
I hope I cleared it out for the most of you! Sorry for any inconvenience caused.
{
"students": [
{
"id": 3021,
"nume": "Ionut",
"prenume": "Grigorescu",
"an": 3,
"departament": "IE"
},
{
"id": 3061,
"nume": "Nadina",
"prenume": "Pop",
"an":3,
"departament": "MG"
},
{
"id": 3051,
"nume": "Ionut",
"prenume": "Casca",
"an": 3,
"departament": "IE"
}
],
"exams": [
{
"id": 1,
"subiect": "Web Semantic",
"profesor": {
"Nume": "Robert Buchman"
}
},
{
"id": 2,
"subiect": "Programare Web",
"profesor": {
"Nume": "Mario Cretu"
}
},
{
"id": 3,
"subiect": "Medii de Programare",
"profesor": {
"Nume": "Valentin Stinga"
}
}
],
"listaexamene": [
{
"examId":1,
"Data Examen":"02/06/2022 12:00",
"studentId":
[
{
"id":3021
},
{
"id":3051
}
]
},
{
"examId":2,
"Data Examen":"27/05/2022 10:00",
"studentId":
[
{
"id":3021
},
{
"id":3051
}
]
},
{
"examId":1,
"Data Examen":"04/06/2022 10:00",
"studentId":
[
{
"id":3021
},
{
"id":3051
},
{
"id":3061
}
]
}
]
}
I had to repost with more information after my first one got closed down
I think I finally got the answer. The problem lays in the JSON server. Apparently, it cannot obtain information from further down the arbitrary tree, only the first layer.
Thank you all for your input on the previous post!

Preparing NodeJS DB content for use in Mustache

I have a library of FAQs that are split up into tabs on the front end, we have since built a NodeJS app that handles them so we can have a database of FAQs instead of hardcoded HTML.
I have a NodeJS model generating a JSON file that is filtering the questions by their tab association using the following:
res = _.groupBy(res, 'tab_title');
Which is outputting this.
However, below is the structure of the original JSON file that the Mustache tags in the template are expecting it to look like:
{
"tabs": [
{
"title": "Tab title",
"id": 1,
"questions": [
{
"question": "Question here",
"id": 1,
"answer": "Answer here"
},
{
"question": "Question two",
"id": 2,
"answer": "Answer here"
}
]
},
{
"title": "Another title",
"id": 2,
"questions": [
{
"question": "Question here",
"id": 1,
"answer": "Answer here"
},
{
"question": "Question two",
"id": 2,
"answer": "Answer here"
}
]
}
}
This is so the front end tags can just loop over the tabs, then loop over the questions within the tabs so the front end is relatively automated.
I have tried to use the _.map function to output all the appropriate information in this structure but I'm really struggling. Can anyone point my in a direction that could help?
I hope this is enough information, but if not, I can supply more.
Thanks!
So we start by generating the groups like you did.
_.groupBy(myArray, 'tab_title');
However, that doesn't quite solve our issue here, the object still wont match the intended structure. It'll look like :
{
"tab_title1" : [{...}, {...}],
"tab_title2" : [{...}, {...}]
}
So we could use the map function to process this further :
_.map(groupObject, (lstFaqs, tab_title) => {
let first = _.first(lstFaqs),
tab_object = {
title: first.tab_title,
id: first.tab_id,
questions: _.map(lstFaqs, (faqItem) => {
id: faqItem.faq_id,
question : faqItem.faq_question,
answer : faqItem.faq_answer
})
}
return tab_object;
})
We'll end up with an array of tab_object. So to finalize the formatting :
let result = { "tabs" : array_of_tab_object }
I hope this help!

Accessing the child of a child in a JSON with Handlebars.js

I'm having no trouble accessing the initial child of the parent object in a json, however, I cannot seem to figure out what the format for accessing the child of a child is. I'm currently using dot notation as described in the handlebars.js documentation.
My html with handlebars.js implemented (the 'Fees' aren't showing correctly, they show up as [object Object]):
{{#options}}
{{#company_base}}
<div>
<h1>{{name_full}}</h1><b>AM Best Rating</b>: {{ambest_rating}}
<p><b>Type</b>: {{business_type}}</p>
<p><b>Fees</b>:
<ol>
<li><b>Type</b>: {{../..options.fees.type}}</li>
<li><b>Name</b>: {{../..options.fees.name}}</li>
</ol>
</p>
</div>
{{/company_base}}
{{/options}}
My mock JSON:
{
"options": [{
"company_base": {
"business_type": "Life, Accident, and Health",
"established_year": 1998,
"med_supp_state_market_data": [{
"market_share": 0.63490064689900005,
"state": "AK",
"lives": 8041,
"premiums": 14714825,
"claims": 11649263
}, {
"market_share": 0.34445359987700003,
"state": "WY",
"lives": 14916,
"premiums": 30178554,
"claims": 24281001
}],
"underwriting_data": [],
"med_supp_national_market_data": {
"market_share": 0.315510079562,
"state": null,
"lives": 3723184,
"premiums": 8276072271,
"claims": 6436017316
},
"customer_complaint_ratio": 0.0013368044250809999,
"ambest_outlook": "Stable",
"name_full": "Major Health Partners of the Wind",
"ambest_rating": "A",
"parent_company": "aghzfmNzZ2sdfZWRfc3VwcA",
"last_modified": "2017-01-16T12:28:17.591830",
"customer_satisfaction_ratio": 0.83666666666699996,
"default_resources": {
"final-expense-life": {
"e_app_link": ""
},
"medicare-advantage": {
"e_app_link": ""
},
"medicare-supplement": {
"e_app_link": "sdf"
},
"hospital-indemnity": {
"e_app_link": ""
},
"dental": {
"e_app_link": ""
}
},
"key": "assdfsdfVwcA",
"parent_company_base": {
"established_year": 1998,
"code": "707",
"name": "Space Insurance",
"key": "asfdf",
"last_modified": "2016-11-11T16:42:52.240940"
},
"sp_rating": "AA-",
"naic": "79413",
"type": "STOCK",
"name": "Spacewomen Insurance"
},
"has_pdf_app": true,
"rate": {
"quarter": 23841,
"annual": 92964,
"semi_annual": 47682,
"month": 7747
},
"rating_class": "Standard",
"fees": [{
"name": "corgi discount",
"type": "buddy"
}]}
Here is a live example of my issue.
It is not the "child of a child" that you are having trouble accessing, but a sibling property that is of the array type.
There are two problems with your example. First, fees is on the same level as company_base. When you are within the {{#company_base}} {{/company_base}} tags you are within the context of the company_base object, so must step-up one level in order to access its siblings. The correct path would be: {{../fees}}.
Your second issue is that fees is an array. You might want to {{#each}} over this array, but if you want only the first object, then you can access it like: {{fees.0.type}}.
This means that your template should be updated with the following:
<li><b>Type</b>: {{../fees.0.type}}</li>
<li><b>Name</b>: {{../fees.0.name}}</li>
That should do the trick. However, I would like to recommend an alternative way of writing your template. I would eliminate the need to step-up a level to get the fees object by removing the {{#company_base}} {{/company_base}} tags. This will mean that you are at the level of the current object in the options array and you can use dot notation to access its descendant properties. The updated template would look like the following:
{{#each options}}
<div>
<h1>{{company_base.name_full}}</h1>
<b>AM Best Rating</b>: {{company_base.ambest_rating}}
<p><b>Type</b>: {{company_base.business_type}}</p>
<p>
<b>Fees</b>:
<ol>
<li><b>Type</b>: {{fees.0.type}}</li>
<li><b>Name</b>: {{fees.0.name}}</li>
</ol>
</p>
</div>
{{/each}}
Note: I am opting for the more explicit {{#each options}} over {{#options}}.
I have created an example fiddle here.

Using angular.js to set selected from a dynamic value in JSON files

Forgive me if this is a newbie question. I'm very new to Angular.
I've looked everywhere for this, and while there are a lot of questions answered about setting a default selected option, I haven't found one where the selected option is set dynamically from values in a JSON file.
My controller looks like this:
peopleControllers.controller('PeopleListCtrl', ['$scope','PeopleList',
function($scope, PeopleList) {
$scope.id = 'id';
$scope.people = PeopleList.query();
$scope.orderProp = 'name';
$scope.comments = 'comments';
$scope.department = 'department';
$scope.departmentList = [
{id : 1, name : "HR" },
{id : 2, name : "Accounting"},
{id : 3, name : "Marketing"}
];
}]);
PeopleList is a $resource that comes from a JSON file formatted like this:
[
{
"id": "johnasmith",
"name": "John A. Smith",
"department": "1",
"comments": "Good worker"
},
{
"id": "sarahqpublic",
"name": "Sarah Q. Public",
"department": "2",
"comments": "New hire"
},
{
"id": "janedoe",
"name": "Jane Doe",
"department": "3",
"comments": "Good resource for information"
}
]
...
And in the HTML, I have this:
ul class="people">
<li ng-repeat="person in people | filter:query | orderBy:orderProp">
<h3>{{people.name}}</h3>
<p>{{people.comments}}</p>
<select ng-model="department" ng-options="departmentList.name for department in departmentList track by department.id">
</select>
</li>
</ul>
The select statement populates with all the right info from departmentList, but its selected value ends up being blank. If I set a static value for $scope.department, like $scope.department = $scope.departmentList[1]; (for "Accounting") it works perfectly. But it doesn't seem to be able to pull the department value from the JSON file.
I know I'm missing something simple and obvious. There have got to be other people who have already asked and had answered this question, so I'm sorry if this turns out to be a duplicate. But I'm really stymied right now.
From Docs
select as and track by Do not use select as and track by in the same
expression. They are not designed to work together.
Additionally as #Chrillewoodz suggested you need to convert the string to number for department id
Markup
<select ng-model="person.department"
ng-options="department.id as department.name for department in departmentList">
</select>
Demo Plunkr
You JSON file contains only strings which is causing you to search for the value "3" in the array instead of selecting [3]. So change your JSON to department: 3 instead of department: "3" and so on.
Like this:
{
"id": "johnasmith",
"name": "John A. Smith",
"department": 1,
"comments": "Good worker"
},
{
"id": "sarahqpublic",
"name": "Sarah Q. Public",
"department": 2,
"comments": "New hire"
},
{
"id": "janedoe",
"name": "Jane Doe",
"department": 3,
"comments": "Good resource for information"
}
Also you should do select ng-model="person.department".

Access multiple items in multiple arrays from JSON using AngularJS

I'm building a phone directory app using AngularJS with JSON data. I'm new to AngularJS and JSON.
I went through the "Phonecat" tutorial on the AngularJS site and was able to build a test directory with JSON data that was similar to the tutorial.
I hit a roadblock using what I learned there and trying to build something with real data; the JSON objects have multiple arrays and I've not been able to figure out how to access the data in the arrays...
I tried to replicate my issue here: http://jsfiddle.net/4ykNX/
Thanks in advance!
//employee.json
{
"resultTruncated": false,
"containsSecureData": false,
"entries": [
{
"type": "employee",
"firstName": "Any",
"lastName": "One",
"address": [
{
"streetOne": "123 Long Street",
"streetTwo": "Big Building",
"streetThree": "Room 456",
"city": "City",
"state": "ST",
"zip": "12345"
}
],
"phone": [
{
"area": "111",
"number": "222-3333",
"extn": "444"
}
],
"email": "any#one.edu"
}
]
}
You are trying to set $scope.employees to data, when it should be data.entries
'use strict';
function EmpListCtrl($scope, $http) {
$http.get('employees.json').success(function(data) {
$scope.employees = data.entries;
});
}
Then you need to reference employee.phone instead of phone:
<div ng-app>
<div ng-controller="EmpListCtrl">
<h1>Employees</h1>
<ul>
<li ng-repeat="employee in employees">
{{employee.lastName}}, {{employee.firstName}}<br/>
{{employee.email}}
<ul>
<li ng-repeat="num in employee.phone">
{{num.area}}-{{num.number}}-{{num.extn}}
</li>
</ul>
</li>
</ul>
</div>
</div>
Here's a plnkr: http://plnkr.co/edit/L2nMu0?p=preview