jquery on show and on hide not working with class selector - jquery-selectors

I am trying to enter certain roles in a text area and have it highlighted in a popup block using bootstrap.
The expected behavior is that the selected roles must be highlighted, the code works when I use the id selector to the jquery, but the same code does not work with class selector
<textarea name="options" id="options" cols="30" rows="10">admin user</textarea>
<a class="btn" data-toggle="modal" href="#myModal">Launch Modal</a>
<div class="modal hide fade" id="myModal" class="myModal">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Modal header</h3>
</div>
<div class="modal-body">
<div>
<ul>
<li class="mlist">admin</li>
<li class="mlist">user</li>
<li class="mlist">guest</li>
<li class="mlist">blank</li>
</ul>
</div>
</div>
<div class="modal-footer">
Close
</div>
</div>
<script type="text/javascript">
$(document).ready(function (){
console.log('Inside onready');
$('.myModal').on('show', function () {
console.log('Inside show');
var option = $('#options').val();
var options = option.split(" ");
console.log('Selected roles: ' + options);
for (var index = 0; index < options.length; index++) {
highlight(options[index]);
};
});
$('.myModal').on('hide', function () {
console.log('Inside onhide');
$('.mlist').removeClass('btn-primary');
});
});
function highlight(parOptions) {
$('.mlist:contains("' + parOptions +'")').addClass('btn-primary');
}
</script>

I guess it's something to do with your div having two class tags.
<div class="modal hide fade" id="myModal" class="myModal">
Change to
<div class="modal hide fade myModal" id="myModal">

Related

Button behaving globally and not for each item in Salesforce LWC

I am quite new to Lighting Web Component. However, I have some experience with other component base frameworks.
I'm creating a button for each item and when the button is clicked for each item. It applies to all items. In other words, when one clicks 'Extend' it is meant to show a date input for that specific item but it shows too all of them. This is how my code looks like:
<template>
<template if:true={subscriptionData}>
<template for:each={subscriptionData} for:item="subscriptionItem">
<div key={subscriptionItem.Id}>
<div>
<div class="slds-grid slds-gutters slds-grid_vertical-align-center">
<div class="slds-col custom-card-title-container">
<h2 class="custom-title">{subscriptionItem.SBQQ__ProductName__c}</h2>
</div>
<div class="slds-col button-custom">
<lightning-button label="Extend" title="Non-primary action" onclick={handleShowEditEndDate} class="slds-m-right_none custom-margin"></lightning-button>
</div>
</div>
<div class="slds-grid slds-m-top_xx-small">
<div class="slds-col">
<div class="slds-grid slds-grid_vertical">
<div class="slds-col slds-m-bottom_xx-small">
<span>Start Date</span>
</div>
<div class="slds-col">
<span>{subscriptionItem.SBQQ__StartDate__c}</span>
</div>
</div>
</div>
<div class="slds-col">
<div class="slds-grid slds-grid_vertical">
<div class="slds-col slds-m-bottom_xx-small">
<span>End Date</span>
</div>
<div class="slds-col">
<span>{subscriptionItem.SBQQ__EndDate__c}</span>
</div>
</div>
</div>
<div class="slds-col">
<div class="slds-grid slds-grid_vertical">
<div class="slds-col slds-m-bottom_xx-small">
<span>Quantity</span>
</div>
<div class="slds-col">
<span>{subscriptionItem.SBQQ__Quantity__c}</span>
</div>
</div>
</div>
</div>
<template if:true={showEditField}>
<lightning-layout multiple-rows class="slds-m-top_xx-small">
<lightning-layout-item size="8">
<lightning-input
type="date"
name="endDateInput"
label="End Date"
variant="label-inline"
>
</lightning-input>
</lightning-layout-item>
</lightning-layout>
</template>
<hr class = "custom-line">
</div>
</div>
</template>
</template>
export default class ExtendCPQSubscriptionLWC extends LightningElement {
#api recordId;
#track subscriptionData;
#track columns = columns;
showEditField = false
#wire(getSubscriptionsLWC, { recordId: '$recordId'})
loadFoundSubscriptions({error, data}) {
if (data) {
console.log('OK');
console.log(data);
this.subscriptionData = data;
} else if (error) {
console.log('ERR ' + JSON.stringify(error));
}
}
I would appreciate any input thanks in advance.
You have common showEditField used to control visibility/addition of date input. My recommendation is to move that information inside subscriptionData and update the track variable. This would trigger re-render of UI and only show that field for item, for which Extend button was clicked

Delegated EventHandler - specific modal depending on tab - Active

I'm wanting to display one of three modals upon the click of one button in the navbar, depending on which of the the three tabs is active. I am trying to use the delegated eventhandler, however my computer skills are relatively immature and I am getting all three modals come up.
The modal ids are id="createModal", "createModalD" and "createModalL" for the employee, department and location data.
//SHOW ADD EMPLOYEE MODAL
$("nav").on("click", "#Ebutton", function (e) {
e.preventDefault();
$("#createModal").modal("show");
//$("#createModal").modal("");
});
$(".nav-tab loc").on("click", "createModal", function (e) {
$("#createModalD").modal("hide");
$("#createModalL").modal("hide");
});
//SHOW ADD DEPARTMENT MODAL
$("nav").on("click", ".Dbutton", function (e) {
e.preventDefault();
$("#createModalD").modal("show");
//$("#createModalD").modal("");
});
$(".Dbutton").on("click", "createModalD", function (e) {
$("#createModalL").modal("hide");
$("#createModal").modal("hide");
});
// //SHOW ADD LOCATION MODAL
$("nav").on("click", "#Ebutton", function (e) {
e.preventDefault();
$("#createModalL").modal("show");
// $("#createModalL").modal("");
});
$("#Ebutton").on("click", "createModalL", function (e) {
$("#createModalD").modal("hide");
$("#createModal").modal("hide");
});
!-- NAVIGATION BAR -->
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
<div class="container">
<!-- Add Button -->
<button role="button" class="Dbutton"id="Ebutton"><i class="fas fa-plus"></i></button>
<button class="navbar-toggler fixed" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse " id="navbarSupportedContent">
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="index-tab" data-bs-toggle="tab" data-bs-target="#index" type="button" role="tab" aria-controls="index" aria-selected="true">Employee</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link " id="department-tab" data-bs-toggle="tab" data-bs-target="#department" type="button" role="tab" aria-controls="department" aria-selected="false">Department</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="loc-tab" data-bs-toggle="tab" data-bs-target="#loc" type="button" role="tab" aria-controls="loc" aria-selected="false">Location</button>
</li>
</ul>
</div>
</nav>

Symfony 4: Display dynamic value in modal

I want to display the details of an object (from Controller classe) in modal but after doing the code below i got this error:
Variable "doaDetails" does not exist.
<a data-href="{{ path("details_dao", { 'id': dao.id } ) }}" data-toggle="modal" data-target="#detailsModal" class="fas fa-info-circle fa-1x" title="Détails" style="color:white;"></a>
my modal in the view :
<!-- Modal -->
<div class="modal fade" id="detailsModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document" >
<div class="modal-content">
<div class="modal-header" >
<h5 class="modal-title">Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="accordion" id="accordionExample">
<div class="card" style="background:transparent;">
<div class="card-header" id="headingOne">
<h2 class="mb-0">
<button class="btn btn-ok collapsed" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Détails
</button>
</h2>
</div>
<div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
Nature : {{ doaDetails.natureDAO}}<hr>
Type :{{ doaDetails.typeDAO}}<hr>
Nom du Marché : {{ doaDetails.nomMarche}}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
My script after clicke on button:
Détails of modal
$('#detailsModal').on('show.bs.modal', function(e) {
$(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
});
action details in Controller class:
/**
* #Route("admin/dao/{id}/details_dao", name="details_dao")
*/
public function detailsDAO(MarcheDAO $dao, MarcheDAORepository $repo ){
$daoDetails= $repo->find($dao);
return $this->render('admin/dao/liste_dao.html.twig', [
'doaDetails'=>$daoDetails
]);
}
As you can see, the link is dynamic with the ID of the recod. Now, I want to open a modal with dynamic values for the details action. How I can do this.
Help!!

bootstrap 3 modal in meteor not showing

I'm trying to get a bootstrap 3 modal to pop in a meteor app without any success at all. Everything seems to be in place, I've scoured here and else where and it simply won't work.
The HTML is
<!-- A modal that contains the bigger view of the image selected -->
<template name="projectImageModal">
<div class="modal fade in show" id="projectImageModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<p> Hi There</p>
<div class="modal-dialog">
<div class="modal-content">
<p> Hi There again</p>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Big Image</h4>
</div>
<div class="modal-body">
<img src="{{cfsFileUrl 'bigProjectImage' file=image}}" alt="..." class="img-rounded">
</div>
</div>
</div>
</div>
</template>
Which is triggered from a click event on an image thumbnail (which is working according to console.log
The code trying to show the dialog is
Template.projectImageItem.events = {
"click .open-modal" : function(e,t) {
e.preventDefault();
Session.set("selectedImageId", t.data._id);
console.log("Image ID: "+ Session.get("selectedImageId"));
//var stuff=$('#projectImageModal');
//console.log(stuff);
//stuff.modal('show');
// $('#projectImageModal').modal().modal("show");
$("#projectImageModal").modal("show");
//$('#projectImageModal').modal('show');
//$('.projectImageModal').modal('show');
}
};
Which is largely pulled directly from the cfs-file-handler example (which doesn't used bootstrap and calls the modal().modal("show") version to get the modal to pop).
You can see the variations I've tried. The console shows that the event is fired, the selector seems to be working but.. the modal NEVER pops.
Thanks. Peter.
I have tried the code below and it works as expected. If I add show to class="modal fade in" the modal immediately appears. If in your case it doesn't you are probably missing something else that is not shown in the sample code.
HTML
<head>
<title>modal</title>
</head>
<body>
{{> projectImageItem}}
{{> projectImageModal}}
</body>
<!-- A modal that contains the bigger view of the image selected -->
<template name="projectImageModal">
<div class="modal fade in" id="projectImageModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Big Image</h4>
</div>
<div class="modal-body">
<img src="{{cfsFileUrl 'bigProjectImage' file=image}}" alt="..." class="img-rounded">
</div>
</div>
</div>
</div>
</template>
<template name="projectImageItem">
<input type="button" class="open-modal" value="Show modal." />
</template>
JS
if (Meteor.isClient) {
Template.projectImageItem.events = {
"click .open-modal" : function(e,t) {
e.preventDefault();
$("#projectImageModal").modal("show");
}
};
}

Getting index of a clicked element

Hi I have this code. I need to get the 0 based index of the clicked element.
This is my code. I always get -1 as the index.
Ideally clicking first link would print 0 and second would print 1.
I am using jquery 1.3.2. JavaScript code is fine too.
What I am missing?
<script type="text/javascript">
function handleClick(id) {
var par = document.getElementById('par'+id);
alert('Index= ' + $('#clips').index(par));
}
</script>
<div id="clips" style="clear:both">
<div id="par30" class="alb rc32">
<div class="fl rc32 thm">
<a id="30" onclick="handleClick(30);">Link 1</a>
</div>
</div>
<div id="par40" class="alb rc32">
<div class="fl rc32 thm">
<a id="40" onclick="handleClick(40);">Link 2</a>
</div>
</div>
<div id="par50" class="alb rc32">
<div class="fl rc32 thm">
<a id="50" onclick="handleClick(50);">Link 3</a>
</div>
</div>
</div>
This works:
<script type="text/javascript">
function handleClick(id) {
var $par = $('#par' + id);
alert($par.index());
}
<div id="clips" style="clear:both">
<div id="par30" class="alb rc32">
<div class="fl rc32 thm">
Link 1
</div>
</div>
<div id="par40" class="alb rc32">
<div class="fl rc32 thm">
Link 2
</div>
</div>
<div id="par50" class="alb rc32">
<div class="fl rc32 thm">
Link 3
</div>
</div>
</div>
You are using the index function wrong.
Try
<script type="text/javascript">
function handleClick(id) {
var par = document.getElementById('par'+id);
alert('Index= ' + $('#clips > div').index(par));
}
</script>
see http://api.jquery.com/index/
If .index() is called on a collection
of elements and a DOM element or
jQuery object is passed in, .index()
returns an integer indicating the
position of the passed element
relative to the original collection.