Bootstrap 4 button inside card component that has click event - html

I have the following html code to generate a basic card using bootstrap 4.
I want to change the style of the card when the card is clicked. However, if the button is clicked, I dont want the style to change.
At the moment, when I click test1 button, I will also activate the function onCardClick().
Is there a way to do this such that when I click on the test button1 only Test1() executes
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="card" (click)="onCardClick()">
<div class="card-block">
<h4 class="card-title d-inline">Some Title</h4>
<button class="btn btn-primary" (click)="onTest1()">test1</button>
<button class="btn btn-primary" (click)="onTest2()">test2</button>
</div>
</div>
</div>

You could try this to stop the event from bubbling up the dom:
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="card"
(click)="onCardClick()">
<div class="card-block">
<h4 class="card-title d-inline">Some Title</h4>
<button class="btn btn-primary"
(click)="onTest1($event)">test1
</button>
<button class="btn btn-primary"
(click)="onTest2($event)">test2
</button>
</div>
</div>
</div>
</div>
</div>
And in your component:
import {Component} from "#angular/core"
#Component({
selector: 'test',
})
export class Test
{
onTest1(event)
{
event.stopPropagation();
//or event.preventDefault()
//stuff to run
}
onTest2(event)
{
event.stopPropagation();
//stuff to run
}
}

Related

Change column sizes by clicking in angular

i have this page that has two buttons divided By two clo-md-6 :
i want when i click in the button the first button become col-md-9 and the other col-md-3 and the same for the inverse
i don t know how to do it in angular
This is my HTML :
<div class="container-fluid">
<div class="row">
<!-- $("#btn1").parentElement.parentElement.className= "col-3"-->
<!--------------------THE FIRST BUTTON----------------------------->
<div class="col-xl-6" [class.col-xl-9]="isclicked">
<div class="card">
<button type="button" id="btn1" class="btn btn-primary" (click)="AfficherRecherche()">Agent professionnelle</button>
<div class="card-body" *ngIf="isclicked">
<h4 class="header-title mt-0 mb-4">Agent Professionnelle</h4>
<aw-wizard>
<aw-wizard-step class="sw-main sw-theme-default">
<form [formGroup]="searchForm">
<div class="row">
<div class="col-12">
<div class="form-group row mb-3">
<input type="text" class="form-control" (keyup)="searchTerm.next($event)" formControlName="search" placeholder="Rechercher ..." (input)="search()" [(ngModel)]="searchText" />
<div *ngIf="loading">
<p class="search-message">Searching</p>
<div class="lds-ellipsis">
<!-- -------------------------------THE SEOND BUTTON------------------------------------------------------------------ -->
<div class="col-xl-6" [class.col-xl-3]="isclicked">
<div class="card">
<div class="btn-group">
<button id="btn2" type="button" class="btn btn-primary" (click)="AfficherAgentpar()">Agent Partculier</button>
</div>
this is my component :
AfficherRecherche() {
this.isclicked = !this.isclicked;
}
AfficherAgentpar() {
this.agentparclick = !this.agentparclick;
}
You should trigger classes on click. So for example:
In your TS file declare a variable with a boolean:
public resize: boolean;
In your HTML you have 2 divs and a button:
<div class="clo-md-6" [class.col-md-9]="resize"></div>
<div class="clo-md-6" [class.col-md-3]="resize"></div>
<button (click)="resize != resize">Trigger me</button>
What happens is the following: When the button gets clicked it changes the value of our resize boolean. Since this is false by default, it'll trigger to true. When resize is true, the first div will be assigned the col-md-9 class, and the second div will be assigned the col-md-3 class. If you click the button again, the divs will go back to being 50/50.

Vue.js - How to pass data to Modal

I used vue to populate the item list in my html, and what I want to do, is when the item is clicked, a modal would appear with the data of the clicked item. I'm not using boostrap-vue.
Html:
<div class="row">
<div class="col-lg-4 col-md-6" v-for="items in data" data-toggle="modal" data-target="#exampleModal" user="'items'" click="sendInfo(items)">
<a href="#" class="latest-product__item">
<div class="latest-product__item__pic">
<img src="img/latest-product/lp-1.jpg" alt="">
</div>
<div class="latest-product__item__text">
<h6>{{items.item_name}}</h6>
<div v-for="variant in items.variants">
<div v-for="store in variant.stores">
<span>{{store.default_price}}</span>
</div>
</div>
</div>
</a>
</div>
</div>
Modal:
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<a href="#" class="latest-product__item">
<div class="latest-product__item__pic">
<img src="img/latest-product/lp-1.jpg" alt="">
</div>
<div class="latest-product__item__text">
<h6>{{items}}</h6>
<span>{{items}}</span>
</div>
</a>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success btn-block">Add to Cart</button>
</div>
</div>
</div>
</div>
Vue:
window.onload = function () {
const access_token = "access token here";
new Vue({
el: '#item-data',
data () {
return {
data:[]
}
},
mounted () {
..... API calll .....
},
methonds:{
sendInfo(items) {
this.selectedUser = items;
}
}
})
}
I want to pass the data {{items.item_name}} and {{store.default_price}} to the modal. How do I achieve this?
create a variable for the value you want to pass, a method to handle the event:
data () {
return {
data:[],
passedData:{item_name:null, default_price:null}
},
methods:{
sendInfo(item){
this.passedData = item
}
}
and on the HTML part where you loop through the items:
<div class="col-lg-4 col-md-6" v-for="items in data" data-toggle="modal" data-target="#exampleModal" user="'items'" click="sendInfo(items)">
<div class="latest-product__item__pic">
<img src="img/latest-product/lp-1.jpg" alt="">
</div>
<div class="latest-product__item__text">
<h6>{{items.item_name}}</h6>
<div v-for="variant in items.variants">
<div v-for="store in variant.stores">
<span>{{store.default_price}}</span>
</div>
<a href="#" class="latest-product__item" #click="sendInfo({item_name:items.item_name,default_price:store.default_price})">
Show Modal
</a>
</div>
</div>
</div>
Modal:
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<a href="#" class="latest-product__item">
<div class="latest-product__item__pic">
<img src="img/latest-product/lp-1.jpg" alt="">
</div>
<div class="latest-product__item__text">
<h6>{{passedData.item_name}}</h6>
<span>{{passedData.default_price}}</span>
</div>
</a>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success btn-block">Add to Cart</button>
</div>
</div>
</div>
</div>
Use common Vue instance for modal and HTML block to access the same data as. Suppose:
<div id='#item-data'>
<div>
//basic Html code
<div>
<div>
//modal code
<div>
<div>
then you can access same vue instance variable in your HTML and modal.
I'm going to assume that your modal markup is somewhere inside the element with the ID 'item-data', and so has access to the data in your Vue instance.
The first thing you need to do is declare selectedUser as a data property, as Vue needs to know about it upfront:
data () {
return {
data:[],
selectedUser: null,
}
},
Next, you need to change your modal markup slightly, so it's referring to the currently selected item (e.g the heading would be {{selectedUser.item_name}} instead of {{items}}).
Lastly, the syntax is wrong where you're attaching a click handler to your item row. Instead of click="sendInfo(items)" it should be #click="sendInfo(items)".

ngIf user is logged in show component

I'm making an app that needs to be accessible for users without an account but as soon as someone makes an account they need to be able to see additional functionality and it needs to be responsive as well.
For responsiveness I'm using material design for bootstrap and angular materialmdbootstrap. In the example bellow I'd want to change the mdb-col-size from 9 to 12 depending on if the user is logged in or not and thus not display save,... option(s).
<div>
<div class="row ">
<div class="col-lg-9 col-md-8 col-sm-6 ">
<div class="row ">
<div class="col-md-12 mb-12">
<div class="card testimonial-card ">
<div class="card-up lighten-1 ">
<div class="card-body">
<h4 class="card-title">{{ dare.title }}</h4>
<hr />
<p>{{ dare.description }}</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-3 col-md-4 col-sm-6"><!--this shouldn't be displayed when someone is not logged in-->
<div class="row">
<div class="col-12">
<button mat-raised-button class="btn btn-block">
Save
</button>
</div>
</div>
<br />
<div class="row">
<div class="col-12">
<button mat-raised-button class="btn btn-block">
Challenge someone else
</button>
</div>
</div>
<br />
<div class="row">
<div class="col-12">
<button mat-raised-button class="btn btn-block">
Create new ...
</button>
</div>
</div>
</div>
</div>
<br />
</div>
I expect to see the options only when someone is logged in and the responsiveness changing accordingly.
Thanks you in advance!
You should rather use Guards in your project, example in documentation: https://angular.io/api/router/CanActivate
To achieve it you should implement two components (one for authenticated user, another for non-authenticated user). Define routing, for example:
{
path: '',
component: AuthenticatedUserComponent,
canActivate: [AuthenticatedUserGuard]
},
{
path: '',
component: UnauthenticatedUserComponent
}
And then define guard:
#Injectable()
class AuthenticatedUserGuard implements CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree {
return true; // here you should call a method from service, that
// that holds whether user is authenticated or not
}
}

Collapsible Accordion in Angular WITHOUT using a JS code, But only HTML. Whenever I click the button, it does nothing. Please solve this

I am trying to implement an Accordion button on my webpage using the HTML code given below. I am currently implementing this on a very basic scale without using any javascript code with this accordion snippet.
The button just clicks and does nothing and the basic collapsing doesn't take place nor does the accordion functionality is implemented.
Kindly help me in implementing this!
<div class="panel panel-default">
<div class="panel-heading">
<button class="btn btn-primary" type="button"
data toggle="collapse" data-target="#collapseButtonExample" aria-
expanded="true" aria-controls="collapseExample">Question
</button>
</div>
<div class="collapse" id="collapseButtonExample" >
<div class="card card-body">
<p></p>
</div>
</div>
</div>
Something like this will work if there is single element
<div class="panel panel-default">
<div class="panel-heading">
<button class="btn btn-primary" type="button" #btn (click)="btn.toggle = !btn.toggle"
>Question
</button>
</div>
<div [ngClass]="{collapse:!btn.toggle}" >
<div class="card card-body">
<p>1# something</p>
</div>
</div>
</div>
if you have many element this trick will work
<div class="panel panel-default" *ngFor="let i of [1,2,3,4]">
<div class="panel-heading">
<button class="btn btn-primary" type="button" #btn (click)="btn.toggle = !btn.toggle"
>Question
</button>
</div>
<div [ngClass]="{collapse:!btn.toggle}" >
<div class="card card-body">
<p>{{i}}# something</p>
</div>
</div>
</div>
stackblitz demo 🚀🚀

Bootstrap modal opens after double click rather open at first click in Angular 4

I am working in an eCommerce application ,In this I have cart icon .What I want to do is when I click on the cart icon I want to show a modal screen with the user selected product data .
But in my case for the first time after page loads modal screen opens after double click,afterwards it opens with the single click .
icon :
<i class="fas fa-cart-plus fa-flip-horizontal text-primary" (click)="get_My_Cart($event)" data-toggle="modal" data-target="#exampleModal" style="font-size:25px;cursor: pointer;"></i>
Modal :
<div *ngIf="mycart && mycart.length" class="modal fade modalstyle" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header headerHeight text-white " style="background:rgb(0, 0, 0);font-weight:bold">
<h6 class="modal-title" id="exampleModalLabel">My Cart Items</h6>
<button #closeModalBtn type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div *ngFor="let products of mycart;let i =index;">
<div class="container col-sm-12">
<div class="row">
<div class="col-sm-4 paddingforimage">
<img [src]="products['ITEM_IMAGE_PATH']">
</div>
<div class="text-info col-sm-6">
<span>
<h6>{{products?.TOTAL_QTY}} × {{products?.ITEM_PRICE}} ₹</h6>
</span>
<span>
<h6>{{products?.ITEM_DESC}}</h6>
</span>
<span>
<h6>{{products?.TOTAL_AMT}} ₹</h6>
</span>
</div>
<div class="col-sm-1 text-right">
<button type="button" class="close closebtn" aria-label="Close" (click)="detele_My_Cart(products?.ITEM_CODE)">
<span aria-hidden="true" (click)="detele_My_Cart(products?.ITEM_CODE)">×</span>
</button>
</div>
</div>
</div>
<hr>
</div>
<div class=" container row col-sm-12">
<div class="col-sm-6">
<strong>SHIPPING</strong>
</div>
<div class="col-sm-6 text-right">0 ₹</div>
<hr>
<div class="col-sm-6">
<strong>TOTAL</strong>
</div>
<div class="col-sm-6 text-right">{{my_Cart_Total_Amount}} ₹</div>
</div>
<br>
<div class="container row col-sm-12" id="wrapper">
<button type="button" class="btn btn-success buttonSize" data-dismiss="modal" routerLink="/cartsummary">
<strong>CHECK OUT</strong>
</button>
</div>
</div>
</div>
</div>
</div>
Component:
get_My_Cart($event) {
this.publicIp.v4().then(ip => {
this.CartdataService.get_My_Cart_Products(ip).subscribe(
data => {
this.mycart = data['Table']
this.my_Cart_Total_Amount = data['Table1'][0]['TOTAL_AMOUNT']
});
});
}
Here in modal screen I have a validation ,Modal screen is open if data is bind in it otherwise it will not .(modal will not open if the cart is empty).
*ngIf="mycart && mycart.length"
If I remove this validation from the modal screen it works ,but it always opens (if there is no product in cart and user clicked on the cart icon it opens but I want to restrict it .)
can anyone tell me how to handle this issue ..
The flow of code is not correct.
Anyway, I think, you're using jQuery & Bootstrap.js. (Bad Idea)
You can programmatically trigger the modal using $('#exampleModal').modal('show') once data comes from server.
declare var $ :any;
get_My_Cart($event) {
this.publicIp.v4().then(ip => {
this.CartdataService.get_My_Cart_Products(ip).subscribe(
data => {
this.mycart = data['Table']
this.my_Cart_Total_Amount = data['Table1'][0]['TOTAL_AMOUNT']
if(this.mycart && this.mycart.length)
$('#exampleModal').modal('show')
});
});
}
But there is a simple workaround in this case. Try to toggle display property of CSS instead of removing it from DOM using *ngIf.
<div [ngStyle]="{display: mycart && mycart.length ? 'block':'none'}" class="modal fade modalstyle">
</div>