Angular - Make a button active when it is clicked and inactive when an other button in the group is clicked - typescript2.0

I got this code from this post Adding the active class to each clicked button, Angular 4
What I am trying to do is the same thing that is mentioned on that post, make a button active when it is clicked and make the other buttons in the group inactive.
Same code copied, for some reason it doesn't work
<button mat-button *ngFor="let button of filterButtons" [ngClass]="{'active': button.isClicked}" (click)="button.isClicked = !button.isClicked">
{{ button.text }}
</button>
Component.ts
filterButtons = [
{ text: 'Posted', isClicked: false },
{ text: 'FFM', isClicked: false },
{ text: '9999', isClicked: false },
{ text: '9000', isClicked: false },
{ text: '8555', isClicked: false },
{ text: 'Canceled', isClicked: false },
]

The problem is that you are not resetting isClicked value when you click on other buttons.
The simplest solution is something like this.
app.component.html
<button mat-button *ngFor="let button of filterButtons" [ngClass]="{'active': button.isClicked}" (click)="setActive(button)">
{{ button.text }}
</button>
app.component.ts
import { Component } from '#angular/core';
#Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
filterButtons = [
{ text: 'Posted', isClicked: false },
{ text: 'FFM', isClicked: false },
{ text: '9999', isClicked: false },
]
setActive(button: any): void {
for(let but of this.filterButtons) {
but.isClicked = false;
}
button.isClicked = true;
}
}
You can find other solutions, like explained in this post

Related

how to create vue-shepperd component

I am trying to develop guided tour with shepherd: https://www.npmjs.com/package/vue-shepherd but I cannot get the element. So here is my component for guide tour:
<template>
<div></div>
</template>
<script>
import { useShepherd } from 'vue-shepherd';
export default {
props: {
element: {
required: true,
},
id: {
type: Number,
required: true,
},
title: {
type: String,
},
text: {
type: String,
required: true,
},
position: {
type: String,
required: true,
},
},
mounted() {
this.tour.start();
},
data() {
return {
tour: null,
};
},
methods: {
createTour() {
this.tour = useShepherd({
useModalOverlay: true,
});
this.tour.addStep({
title: this.title,
text: this.text,
attachTo: { element: this.element, on: this.position },
buttons: [
{
action() {
return this.back();
},
classes: 'shepherd-button-secondary',
text: 'Back',
},
{
action() {
return this.next();
},
text: 'Next',
},
],
id: this.id,
});
this.tour.start();
},
},
created() {
this.createTour();
},
};
</script>
and here is my parent component:
<button ref="button">
Click
</button>
<guide :element="element" :title="'Tour'" :text="'Example'" :position="'bottom'" :id="1" />
and the mounted of the parent element:
mounted() {
this.element = this.$refs.button;
},
but the tour doesnt attach the the button element. it just appears in the middle of the page. Why do you think it is?
Looks like a usage problem in vue hooks. The child component's hooks fire before the parent component's hooks. Therefore, at the time of the creation of the tour, the element does not exist. Vue-shepherd does not use vue reactivity.
Use
mounted() {
this.$nextTick(() => {
this.createTour();
});
},
Codesanbox
But it's better to change the component structure. If you are using vue3 you can use my package
In this case it will look like this
<template>
<button v-tour-step:1="step1">
Click
</button>
</template>
<script>
import { defineComponent, inject, onMounted } from "vue";
export default defineComponent({
setup() {
const tour = inject("myTour");
onMounted(() => {
tour.start();
});
const step1 = {
/* your step options */
}
return {
step1,
};
}
});
</script>

vue-chartJs, Unable to update the chart

I am using Vue-Chartjs to create a simple Line chart, I'm filling the chart with data via a get request to an API
however I want to generate new values randomly when I click on a button, & pass the values as a prop to chart-line component.
I've tried using reactiveProp & I also tried using a watcher for chartData prop, but I'm always getting this error
client.js?06a0:83 TypeError: Cannot read property 'map' of undefined
Dashboard Component
<template>
<div class="container">
<h1>Dashboard Page</h1>
<v-alert v-if="errorDetected"
class="mt-4"
dense
outlined
type="error"
>
There was an error while getting the chart data
</v-alert>
<v-btn #click="generateNewData()">Generate new data</v-btn>
<div class="loader-container">
<img v-if="!loaded" class="chart-loader mt-3" src="../static/loader-dotted.gif" alt="">
</div>
<ChartLine v-if="loaded" :chartData="values" :bind="true" />
</div>
</template>
<script>
import ChartLine from '../components/chart-line'
export default {
middleware: 'session',
components: {
ChartLine
},
data() {
return {
values: [],
customValues: [],
loaded: false,
errorDetected: false
}
},
head() {
return {
title: 'Dashboard page',
meta: [
{
hid: 'description',
name: 'description',
content: 'simple dashboard SPA'
}
]
}
},
mounted() {
this.requestData()
},
methods: {
requestData() {
this.loaded = false
this.$axios.get('http://www.mocky.io/v2/5eda474f330000fefc79eab4?mocky-delay=2000ms').then(response => {
console.log("requestData -> response", response)
this.values = response.data.data.value
this.loaded = true
}).catch(error => {
this.loaded = true
this.errorDetected = true
})
},
generateNewData() {
this.values = [];
for(let i=0; i<7; i++)
this.values.push(Math.floor((Math.random() * 10) + 1))
}
}
}
</script>
<style>
.loader-container {
display: flex;
justify-content: center;
}
.chart-loader {
width: 150px;
}
</style>
ChartLine Component
<script>
//Importing Line class from the vue-chartjs wrapper
import { Line, mixins } from 'vue-chartjs'
const { reactiveProp } = mixins
//Exporting this so it can be used in other components
export default {
extends: Line,
mixins: [reactiveProp],
props: ['chartData'],
data () {
return {
datacollection: {
//Data to be represented on x-axis
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [
{
label: "Data 1",
backgroundColor: "transparent",
borderColor: "rgba(1, 116, 188, 0.50)",
pointBackgroundColor: "rgba(171, 71, 188, 1)",
//Data to be represented on y-axis
data: this.chartData
}
],
},
options: {
responsive: true,
maintainAspectRatio: false
}
//Chart.js options that controls the appearance of the chart
}
},
watch: {
chartData() {
this.renderChart(this.datacollection, this.options)
}
},
mounted () {
//renderChart function renders the chart with the datacollection and options object.
this.renderChart(this.datacollection, this.options)
}
}
</script>
UPDATE
I've managed to solve the issue of updating the chart
I removed the dataCollection from the chart-line component & added it in the dashboard component, I've also used the requestData() method in the created() hook to make a get request to the API, then on a button click I generate a new values and pass it as a prop
Update Code
Dashboard Component
<template>
<div class="container">
<h1>Dashboard Page</h1>
<v-alert v-if="errorDetected"
class="mt-4"
dense
outlined
type="error"
>
There was an error while getting the chart data
</v-alert>
<v-btn class="primary" #click="generateNewData()">Generate New Data</v-btn>
<div class="loader-container">
<img v-if="!loaded" class="chart-loader mt-3" src="../static/loader-dotted.gif" alt="">
</div>
<ChartLine v-if="loaded" :chart-data="dataCollection" />
</div>
</template>
<script>
import ChartLine from '../components/chart-line'
export default {
middleware: 'session',
components: {
ChartLine
},
data() {
return {
dataCollection: null,
values: [],
customValues: [],
loaded: false,
errorDetected: false
}
},
head() {
return {
title: 'Dashboard page',
meta: [
{
hid: 'description',
name: 'description',
content: 'simple dashboard SPA'
}
]
}
},
created() {
// this.loaded = false
// this.fillData()
// this.loaded = true
this.requestData()
},
methods: {
requestData() {
this.loaded = false
this.$axios.get('http://www.mocky.io/v2/5eda474f330000fefc79eab4?mocky-delay=2000ms').then(response => {
this.values = response.data.data.value
this.loaded = true
this.fillData()
}).catch(error => {
this.loaded = true
this.errorDetected = true
})
},
fillData () {
this.dataCollection = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [
{
label: "Data 1",
backgroundColor: "transparent",
borderColor: "rgba(1, 116, 188, 0.50)",
pointBackgroundColor: "rgba(171, 71, 188, 1)",
data: this.values
}
]
}
},
generateNewData() {
this.values = []
this.loaded = false
setTimeout(function(){},2000)
for(let i=0; i<7; i++) {
this.values.push(Math.floor(Math.random() * (50 - 5 + 1)) + 5)
}
this.fillData()
this.loaded = true
}
}
}
</script>
<style>
.loader-container {
display: flex;
justify-content: center;
}
.chart-loader {
width: 150px;
}
</style>
Chart-Line Component
<script>
//Importing Line class from the vue-chartjs wrapper
import { Line, mixins } from 'vue-chartjs'
//Exporting this so it can be used in other components
export default {
extends: Line,
mixins: [mixins.reactiveProp],
// props:['chartData'],
data () {
return {
options: {
responsive: true,
maintainAspectRatio: false
}
//Chart.js options that controls the appearance of the chart
}
},
mounted () {
//renderChart function renders the chart with the datacollection and options object.
this.renderChart(this.chartData, this.options)
}
}
</script>
however there's still one thing I can't figure out, which is the loading state, when clicking on the button to generate new data
when I first open the dashboard page, the loading state works, but when I click on the button, loading state doesn't work
any Idea why??????

How to show modal using Vue Js and FullCalendar (EventClick)?

I wanna show Modal from fullcalendar using bootstrap-vue. So when I click event on calendar, the modal will be show. But my code does't work.
This is my html code:
<div class="flex-fill bd-highlight col-lg-12">
<div class="card card-default p-3 my-2">
<full-calendar :event-sources="eventSources" :config="calendarConfig"></full-calendar>
</div>
</div>
<div>
<b-modal v-model="modalShow">Hello From Modal!</b-modal>
</div>
This is my vue code:
<script>
export default {
data() {
return {
modalShow: false,
eventId: 0,
eventSources: [
{
events(start, end, timezone, callback) {
axios.get("http://localhost:8000/api/events").then(response => {
callback(response.data.data);
});
},
color: "yellow",
textColor: "black"
}
],
calendarConfig: {
defaultView: "month",
allDaySlot: false,
locale: "id",
buttonText: {
today: "Hari ini",
month: "Bulanan",
week: "Mingguan",
day: "Harian",
list: "Daftar Kegiatan"
},
header: {
left: "prev,next today",
center: "title",
right: "month,agendaWeek,agendaDay list"
},
eventClick: function(view) {
this.modalShow = true;
}
}
};
}
};
</script>
When I console.log(this.modalShow) the value has been change form "false" to "true". But the modal is not showing.
The scope of this is not your vueContext anymore:
eventClick: function(view) {
this.modalShow = true;
}
you can solve this by using bind:
eventClick: function(view) {
this.modalShow = true;
}.bind(this)
Or
eventClick(view) => {
this.modalShow = true;
}
Full explanation is overhere: https://www.w3schools.com/js/js_arrow_function.asp

ag-grid context menu shortcut key not working

hopefully this will be helpful to someone else. I'm using ag-grid enterprise's context menu feature. I wanted to have a shortcut key corresponding to the menu items, but nothing happened when I pressed the key combination.
To make it more confusing (to me) their own example of how to do this doesn't work:
https://www.ag-grid.com/javascript-grid-context-menu/
The problem is that the shortcut attribute only lists the shortcut but doesn't fire anything. You must also write an event handler for the cellKeyDown event to watch for the shortcut:
import { Component } from '#angular/core';
import { GetContextMenuItemsParams } from 'ag-grid-community';
#Component({
selector: 'app-root',
template:
`
<ag-grid-angular style="width: 500px; height: 500px;" class="ag-theme-balham"
[rowData]="rowData" [columnDefs]="columnDefs"
[getContextMenuItems]="getContextMenuItems"
(cellKeyDown)="onCellKeyDown($event)">
</ag-grid-angular>
`,
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'ag-grid-import-excel';
columnDefs = [
{headerName: 'Make', field: 'make' },
{headerName: 'Model', field: 'model' },
{headerName: 'Price', field: 'price'}
];
rowData = [
{ make: 'Toyota', model: 'Celica', price: 35000 },
{ make: 'Ford', model: 'Mondeo', price: 32000 },
{ make: 'Porsche', model: 'Boxter', price: 72000 }
];
getContextMenuItems(params:GetContextMenuItemsParams ) {
return [
'copy',
'copyWithHeaders',
'paste',
'separator',
{
name: "insert",
shortcut: "Alt + i",
action: function() {
console.log("insert");
},
icon: '<i class="fas fa-plus"></i>'
},
'export',
];
}
onCellKeyDown($event) {
let event:KeyboardEvent = $event.event;
if (event.altKey && event.key === "i") {
console.log('alt i pressed')
}
}
}

How to add and remove CSS class names in Angular 2?

I get the data in the JSON format and must and I must to handle which item the user clicked on. I'm write this code and it correctly work.
My Example with DOM-usage:
#Component({
selector: 'my-app',
template: `
<div *ngFor="let person of personsList; let i = index">
<span class="toggle-icon" (click)="toggleStatus(person.id)" id="{{person.id}}">{{person.name}}</span>
</div>
`,
styles: ['.active { color: red; }']
})
export class App {
toggleIsActive: boolean = false;
personsList: any;
constructor() {
this.personsList = [
{
"id": "1",
"name": "Alex"
},
{
"id": "2",
"name": "John"
}
]
}
toggleStatus(id){
const span = document.getElementById(`${id}`);
if (span.className.indexOf('active') >= 0) {
span.classList.remove('active');
} else {
span.classList.add('active');
}
}
}
How I can add and remove CSS class names without DOM for a similar case?
I'm just add "personIsActive" field, and use Class binding.
#Component({
selector: 'my-app',
template: `
<div *ngFor="let person of personsList; let i = index">
<span class="toggle-icon" [class.active]="person.personIsActive"
(click)="toggleStatus(person.id)">{{person.name}}</span>
</div>
`,
styles: ['.active { color: red; }']
})
export class App {
toggleIsActive: boolean = false;
personsList: any;
constructor() {
this.personsList = [
{
"id": "1",
"name": "Alex",
"personIsActive": false
},
{
"id": "2",
"name": "John",
"personIsActive": false
}
]
}
toggleStatus(id){
for (let i = 0; i < this.personsList.length; i++) {
if (this.personsList[i].id === id) {
this.personsList[i].personIsActive= !this.personsList[i].personIsActive;
}
}
}
}