How to add dynamic classes to looped elements in angular - angular7

I have a looped list. I have to add class active when any block is clicked. I am not sure how to do it using [ngClass]. Please help me.
It is HTML code:
<div *ngFor="let cell of myData">
<div class="list-header">
<label>{{ cell.name }}</label>
</div>
<div class="list-group">
<a class="list-group-item list-group-item-action d-flex" *ngFor="let unit of cell.array" (click)= "onClick()" [ngClass]="{'active': this.active}">
<label>{{ unit }}</label>
</a>
</div>
</div>
my TS code:
myData = [
{
'name': 'abc',
'array': ["asass","From Mac","New", "test 1", "test 10", "test 2", "test 3", "test 4", "test 5", "test 6", "test 7", "test 8", "test 9" ]
},
{
'name': 'all types and options',
'array': ['Camera','del TYPE','Fan','hardware','icons','mobile','new asset type']
},
{
'name': 'am cat',
'array': ['am type','camera','new 423423']
},
{
'name': 'cat with no asset types, dec added',
'array': ['camera']
},
{
'name': 'cat with one asset type',
'array': ['camera']
},
{
'name': 'colors',
'array': ['pink', 'yellow']
}
];

<div class="my_class" (click)="clickEvent()"
[ngClass]="active ? 'success' : 'danger'">
Some content
</div>
active: boolean = false;
clickEvent(){
this.active = !this.active;
}

template:
<div *ngFor="let cell of myData">
<div class="list-header">
<label>{{ cell.name }}</label>
</div>
<div class="list-group">
<a class="list-group-item list-group-item-action d-flex" *ngFor="let unit of cell.array; let i = index" (click)= "cell.selectedIndex=i" [ngClass]= "{ 'active': cell.selectedIndex === i }" >
<label>{{ unit }}</label>
</a>
</div>
</div>

Related

I want to send data to back-end by selecting multiple values as object as mentioned in form 2

I want to achieve output as format 2 and i am getting only format 1. so please help me. How can i do that? Please help me .I am new to angular and stack-overflow. Please don't be harsh on me if i made any silly mistake. Thank you.
Thank you.
I want to achieve output as format 2 and i am getting only format 1. so please help me. How can i do that? Please help me .I am new to angular and stack-overflow. Please don't be harsh on me if i made any silly mistake. Thank you.
Thank you.
format:1(present output)
"subjectList": [
{
"subject": [
{
"subjectId": 1,
"subject": "English"
}
]
}
]
format:2(desired output)
{
"subjectList": [
{
"subjectId": 1,
"subject": "English"
},
{
"subjectId": 1,
"subject": "English"
}
]
}
here is my ts file.
buildteacherForm() {
this.teacherForm = this.fb.group({
teacherId: [this.teacher.teacherId],
name: [this.teacher.name, [Validators.required,
Validators.minLength(2), Validators.maxLength(20)]],
qualification: [this.teacher.qualification, [Validators.required,
Validators.minLength(2), Validators.maxLength(20)]],
address: [this.teacher.address, [Validators.required,
Validators.minLength(2), Validators.maxLength(20)]],
phoneNo: [this.teacher.phoneNo, [Validators.required,
Validators.minLength(10)]],
dob: [this.teacher.dob, [Validators.required]],
subjectList: this.fb.array([]),
});
this.addSubjects();
}
addSubjects() {
const subjects = this.teacherForm.controls.subjectList as FormArray;
subjects.push(this.fb.group({
subject: [this.teacherSubject.subject],
}));
}
HTML: here is my html part
<div class="col-md-4">
<div
*ngFor="let teacherSubject of
teacherForm.get('subjectList').controls;index as i"
formArrayName="subjectList">
<div [formGroupName]="i">
<kurumba-form-group>
<mat-form-field class="example-full-width">
<mat-label>Subject list</mat-label>
<mat-select
multiple
matInput
id="subject"
formControlName="subject"
required>
<mat-option disabled> Select subject</mat-option>
<mat-option
[value]="subject"
*ngFor="let subject of teacherSubjectList"
>{{ subject.subject }}</mat-option>
</mat-select>
<mat-hint align="start">Select subject</mat-hint>
</mat-form-field>
</kurumba-form-group>
</div>
</div>
</div>

Data files with html content

I'm making a nice simple html page using external data... here's the bit that's causing me grief:
<section class="faq_main">
{% for section in faq.sections %}
<h3>{{ section.heading }}</h3>
{% for question in section.questions %}
<article class="faq_question">
<a id="hide_{{ section.code }}_{{ question.code }}"
href="#hide_{{ section.code }}_{{ question.code }}"
class="hide"><span class="faq_toggler">+</span> {{ question.question }}</a>
<a id="show_{{ section.code }}_{{ question.code }}"
href="#show_{{ section.code }}_{{ question.code }}"
class="show"><span class="faq_toggler">-</span> {{ question.question }}</a>
<div class="details">
{{ question.answer }}
</div>
</article>
{% endfor %}
<p> </p>
{% endfor %}
</section>
... and the matching faq.json file:
{
"sections" : [
{ "heading" : "Section the first",
"code" : "gn",
"questions" : [
{
"question": "What do questions need?",
"code" : "1",
"answer": "An answer"
},
{
"question": "Is this also a question?",
"code" : "2",
"answer": "Yes, it is"
},
{
"question": "Is this junk data?",
"code" : "a",
"answer": "Yes"
},
]
},
{
"heading": "Another section",
"code" : "f",
"questions": [
{
"question": "Can I have html in my answer?",
"code" : "2",
"answer": "<ul>\n<li>First, json needs newlines escaped to be newlines</li>\\n<li>Eleventy seems to 'sanitize' the string</li>\\n</ul>"
},
{
"question": "question b",
"code" : "b",
"answer": "answer b"
},
{
"question": "question c",
"code" : "or",
"answer": "answer c"
},
]
}
]
}
.... and the rendered text for the answer in question is:
<ul> <li>First, json needs newlines escaped to be newlines</li>\n<li>Eleventy seems to 'sanitize' the string</li></ul>
Do I have any options here? Is there a way to allow [even a subset of] html elements into the page?
(and yes, the CSS does the clever show/hide descriptions using the '+'/'-' symbols - all that side of things works just lovely)
It should work by inserting the symbols directly into the quotes in the json. Use Non-breaking spaces for indentation, and unicode points for dots.
Otherwise, the framework is broken.
Change {{ question.answer }} to {{ question.answer | safe }}
(see https://www.11ty.dev/docs/layouts/#prevent-double-escaping-in-layouts - it's clear, once you understand what it's saying :) )

How do I remove an object from an array of objects. in knockoutJS. Error: Object doesn't support property or method 'remove'

How do I remove an object from an array of objects in Knockoutjs.
I am new to knockoutjs and any help will be appreciated.
A link to Jsfiddle
Line 24 in the HTML code on jsfiddle is the click binding to delete.
Line 67 in the javascript code on js fiddle is where the function to remove an object from the array is made.
I tried to use indexOf and splice Array functions on line 68 and 69 and it worked to remove but the DOM was not being updated.
Please take a look at the function removeProduct: function (product)
Html
<div>List of Product (<span data-bind="text: products().length"></span>)
</div>
<ul data-bind="foreach: products">
<li><span data-bind="text:name"></span>
Select
</li>
</ul>
<div>List of Group Ideas</div>
<ul data-bind="foreach: GroupIdeas">
<li data-bind="text:name">
<input type="button" value="Removethis" />
<input type="button" value="vote" />
</li>
</ul>
<div>List of Group members</div>
</body>
Javascript
$(function () {
var viewModel = {
productPrice: ko.observable(89),
productQty: ko.observable(2),
products: ko.observable([
{ name: "shoe", price: "788", id: 1 },
{ name: "blouse", price: "50", id: 2 },
{ name: "dress", price: "16", id: 3 },
{ name: "lipstick", price: "88", id: 4 },
{ name: "earring", price: "4", id: 5 },
{ name: "bra", price: "96", id: 6 },
{ name: "lingeringe", price: "48", id: 7 },
{ name: "neclace", price: "36", id: 8 },
]),
GroupIdeas: ko.observable([
{ name: "shoe", price: "788", prodId: 1, selectedby: "Akuba", memId:
1, votes: 3 },
{ name: "lingeringe", price: "48", prodId: 7, selectedby: "Isaac",
memId: 2, votes: 6 },
]),
GroupMember: ko.observable([
{ name: "Akuba", relation: "friend", id: 1 },
{ name: "Isaac", relation: "Husband", id: 2 },
{ name: "Ira", relation: "Sister", id: 3 },
{ name: "Davida", relation: "Mum", id: 4 }
]),
partyPerson: ko.observable("Ida"),
partyOrganiser: ko.observable("Royce"),
//addProduct = function () { /* ... leave unchanged ... */ }
removeProduct: function (product) {
/*var indexArr = viewModel.products().indexOf(product);
viewModel.products().splice(indexArr, 1)
*/
viewModel.products().remove(product)
console.log(product);
}
};
viewModel.totalAmt = ko.computed(function () {
return this.productPrice() * this.productQty();
}, viewModel);
ko.applyBindings(viewModel);
//ko.applyBindings(giftModel);
})**
Here's your updated fiddle.
You need to make products an observable array to take advantage of the remove function.
You need to make a reference to the viewmodel before you can refer to products, so your removeProduct function has to be written after the viewmodel initialisation. Similar to how you have written totalAmt.
viewModel.removeProduct = function (product) {
viewModel.products.remove(function(item){
return item.id === product.id;
});
}

How to Parse Custom (without Key) response in AMP-autosuggest (Rails 5)

Anyone tells me how can I parse my response in AMP-list. In AMP didn't find any solution how to parse own custom API response.
My Code.
<div
class="autosuggest-container hidden"
[class]="(showDropdown && query) ?
'autosuggest-container' :
'autosuggest-container hidden'"
>
<amp-list
class="autosuggest-box"
layout="fixed-height"
height="120"
src="http://lmyadpi.com/api.json?q=a"
id="autosuggest-list"
>
<template type="amp-mustache">
<amp-selector
id="autosuggest-selector"
keyboard-select-mode="focus"
layout="container"
on="
select:
AMP.setState({
query: event.targetOption,
showDropdown: false
}),
autosuggest-list.hide"
>
{{#results}}
<div
class="select-option no-outline"
role="option"
tabindex="0"
on="tap:autosuggest-list.hide"
option="{{.}}"
>{{.}}</div>
{{/results}}
{{^results}}
<div class="select-option {{#query}}empty{{/query}}">
{{#query}}Sorry! We don't ship to your city 😰{{/query}}
</div>
{{/results}}
</amp-selector>
</template>
</amp-list>
</div>
</div>
<amp-state
id="allLocations"
src="http://lmyadpi.com/api.json?q=a"
"emptyAndInitialTemplateJson": [{
"query": "",
[[]]
}]
></amp-state>
My API response :
[[
"1234",
"value2",
""
],
[
"6321123",
"value3",
""
],
[
"43934322",
"value4",
""
],
[
"43660213",
"value5",
""
],
[
"54373228",
"value6",
""
],
[
"410721327",
"value7",
""
]]

AngularJS: service $http.get JSON file

I am very new to AngularJS. I have read several posts on this topic, but am still unable to get data from external JSON file.
here is my code
data.json
[
{
"id": 1,
"date": "20th August 2016",
"day": "Sat",
"in1": "10:30",
"out1": "02:30",
"in2": "04:00",
"out2": "08:00",
"in3": "",
"out3": "",
"total_hours": "8:00",
"status": "1"
},
{
"id": 2,
"date": "20th August 2016",
"day": "Sat",
"in1": "10:30",
"out1": "02:30",
"in2": "04:00",
"out2": "08:00",
"in3": "",
"out3": "",
"total_hours": "8:00",
"status": "1"
}
];
html
<table ng-table="tctrl.tableEdit" class="table table-striped table-vmiddle">
<tr ng-repeat="w in $data" ng-class="{ 'active': w.$edit }">
<td data-title="'ID'">
<span ng-if="!w.$edit">{{ w.id }}</span>
<div ng-if="w.$edit"><input class="form-control" type="text" ng-model="w.id" /></div>
</td>
...
</tr>
</table>
controller.js
.controller('tableCtrl', function($filter, $sce, ngTableParams, tableService) {
var data = tableService.data;
//Editable
this.tableEdit = new ngTableParams({
page: 1, // show first page
count: 10 // count per page
}, {
total: data.length, // length of data
getData: function($defer, params) {
$defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
})
service.js
.service('tableService', [function($http){
this.data = function() {
return $http.get('data/timesheet.json');
}
}])
I'm just trying to pull data from json file but it's not working and getting blank table. I don't know what is the issue. What I will get in this.data?

Resources