I am returning an Json object from an local API, it's working fine, with the object I feed my table with rows and columns, each column has a search button, which should open a modal with more detailed info about that line, which was already loaded with the table.
Here is a sample of my button looks like:
<tr>
...
<td><button type="button" class="btn btn-primary form-control"
id="id5bb3b60d870f3809e8e4403d" data-toggle="modal"
data-target="#5bb3b60d870f3809e8e4403dmodal">+</button></td>
</tr>
And here is a modal example:
<div id="5bb3b60d870f3809e8e4403dmodal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="5bb3b60d870f3809e8e4403dLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="5bb3b60d870f3809e8e4403dLabel">whatever</h5>
</div>
<div class="modal-body"></div>
<div class="modal-footer"></div>
</div>
</div>
</div>
As you can see, my data target and div id are matching, I also tried to remove the aria-labelledby and aria-hidden, if I use the browser inspector I can see that the modal is already loaded. I also can successfully use the getElementById using my modal id.
I'm sorry that this is a noob mistake, but I already wasted a whole day trying to solve it. I get no exception, nor error, nor any view of the modal.
#David Liang was right, since I'm loading my table dynamically it didn't works.
To solve it using JQuery delegation I just needed to:
$("#rastreioTable tbody").on('click', 'button', function (event) {
$("#" + this.id + "modal").modal();
});
Where "rastreioTable" was my table Id and "tbody" was the element where the rows where being loaded. As far as I could understand the issue is that when we add a new item or modify an html element you broke the event handlers that were loaded initially.
If it was to be solved with vanilla JS an event listener would solve it, something like the code below:
document.getElementById("rastreioTable").addEventListener('click', function (e) {
if (e.target.type == "button") {
$("#" + e.target.id + "modal").modal();
}
}, false);
Here I would also need to add an if to check if the item pressed is a button, because the event would be triggered if the user clicked anywhere in the table.
Related
When I close my modal, the video keeps playing in the background. It is a really small program with only HTML and CSS, so I don't want to use the YouTube API. This is my modal code:
<div id="lucaModal" class="modal modal-fullscreen" role="dialog">
<button id="lucaButton" class="btn btn-danger closeIFrame" data-dismiss="modal">
CLOSE
</button>
<div class="modal-content">
<iframe class="trailer" id="trailer" title="YoutubeVideoPlayer"
style="height: 100%; width: 100%;"
src="https://www.youtube.com/embed/mYfJxlgR2jw" frameborder="0">
</iframe>
</div>
</div>
I tried jQuery, but my modal wouldn't close this way:
$(document).ready(function(){
$("#lucaModal").modal('show');
$("#lucaButton").click(function(){
$("#lucaModal").modal('hide');
});
});
So I've played around with your code and found a way around this.
Below I used JQuery to solve your.
//below is click detector that checks if the user has clicked anywhere
$('body').on('click',function(){
//if the modal is no longer visible on the screen
if(!$('#lucaModal').is(':visible')){
//we get the src of the current video
var trailer = $('#trailer').attr('src');
//and reset it, that will sort out your problem
$('#trailer').attr('src',trailer);
}
});
Just copy and paste the code above to your js file and see if it works. If it does then play around with it, maybe you can create an even better way of handling this issue.
Thanks.
I have the following problem: I create a web application using Angular. I am making a search form that has several fields. I have it ready as a component. Code below:
<ng-template #filter>
<app-articles-filter #articlesFilterDesktop
(onFilterFormSubmit)="submitFilterForm()"
[categories]="categories"></app-articles-filter>
</ng-template>
To put this code on the page, I'm using ng-container: <ng-container *ngTemplateOutlet = "filter"> </ng-container>. By default, the search form is at the top of the page. I also wanted to do so that when the user resize the window (mobile devices), a button will appear in the place of the search form. After clicking on this button a modal window with this search form would appear. Modal window code (I'm using Bootstrap):
<div class="modal fade" id="filterArticlesModal" tabindex="-1" role="dialog" aria-labelledby="filterArticlesModalTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<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">
<ng-container *ngTemplateOutlet="filter"></ng-container>
</div>
</div>
</div>
</div>
Having the same component twice with ng-template in one HTML file, I get a warning in the console:
"Found 2 elements with non-unique id [duplicated id here]: [html from search form here]".
The fields completed in the search form are not automatically completed in the modal view. (When I complete the form on the page, narrow the browser window and open the modal window.. there are empty fields in the modal window and vice versa). As I understand it, Angular just pastes the code and that's it. Is it possible to somehow treat this code fragment (my search form component) as one reference and not a copy?
EDIT:
I'm not sure about the duplicate ID fiasco. You could remove id's and just go off of classes, or maybe have some property from the component added in to the ID that you can control: id="searchBar{{foo}}"?
Solving the data consistency when you switch between searches can definitely be done.
If you want to have the search value act as a singleton I recommend putting that value in a service, and referencing it from the components. This can be done a few different ways, and there is one thing to consider:
Do you want the search to persist after the page in navigated to/from? If yes then keep the service that stores the search global, if not define it in the providers: [] property of the component that houses these search components (that way it gets deleted each time the component is destroyed)
EDIT FINAL:
Along with the accepted answer, I had to figure out how to close the modal which was not the same as just hiding a dialog in this case.
This bit of javascript helped me to get the modal closed, hide the backdrop, and close it to the point where it would let me reopen another different one on the page.
<script>
$(document).on("click", ".btnCloseDeviceActivityModal", function () {
var deviceActivityModal = $('.deviceActivityModal');
deviceActivityModal.hide();
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
});
</script>
EDIT: Wanted to point out that when I debug it shows me that it is calling back into my partial view controller every time I close the modal. So it is loading it entirely including going back to the controller and querying the database. It is actually calling the javascript method every time I close the window.
Also wanted to add that I tried changing to an onclick="doModal()" way of clicking the button and named the method in javascript to doModal(). I still get the same behavior of the reopening when closing.
Some more observations is the button loses all text and is just a little square after it is clicked the first time. I can see this in the background of the modal. The other interesting thing is the background (outside) of the modal gets darker and darker each time I click the close button on the modal.
I have a modal window that is displaying a partial view. Every time I click the close button it is just opening right back up. I've tried setting it with 'hide' in javascript. I tried some completely boilerplate modal so I think it is how I am calling the modal that is the problem.
This is how the modal is being initiated off of this button click:
<button data-url="#Url.Action("DeviceActivity","Devices", new { #id = item.DeviceId })" class="btn btn-primary btnOpenDeviceActivityModal">Activity</button>
Here is the javascript that loads the modal window from the data retrieved in the controller. It displays the partial view in the modal successfully:
#* Display the device activity in a modal window. Data is loaded from the partial View *#
<script>
$('.btnOpenDeviceActivityModal').click(function () {
var url = $(this).data("url");
$(this).load(url, function () {
$("#deviceActivityModal").modal("show");
});
})
</script>
And just for fun even though it doesn't seem to matter, here is my modal partial view:
<div class="modal fade" id="deviceActivityModal" style="margin-left:100px; margin-top:80px">
<div class="modal-dialog-scrollable" style="top:180px; right:200px">
<div class="modal-content" style="width:1000px; max-height:800px;">
<div class="modal-header">
<button id="closeModal" type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
<div class="modal-body">
<h2 style="text-align:center">Device Activity Records</h2>
<table class="table table-striped table-bordered">
<thead class="thead-light">
<tr>
<th>
Activity Date
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.DeviceActivityRecords)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ActivityDate)
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
What is it that is causing my modal window to reopen each time it is closed. It also seems that anywhere I click on the modal will close (and reopen) it. I dunno if it is related so that's why I bring it up. How can I get it to close and stay closed? Other answers here on SO have some obvious problems with modal code, but I am not seeing what is wrong with how I am calling the modal (which seems to be the common problem because I replaced the modal code entirely to test).
$(this).load is injecting your modal markup inside the button - that's probably not what you want. I'm assuming you meant to use $.get, which does work a bit differently.
$('.btnOpenDeviceActivityModal').on('click', function () {
var url = $(this).data("url");
$.get(url)
.done(function (response) {
$(response).modal("show");
});
});
This will use the markup returned from the AJAX call as the target of the .modal() function - probably want to add some sanity checks around that.
For reference:
$(selector).load() : https://api.jquery.com/load/
$.get(): https://api.jquery.com/jQuery.get/
I have a View/View-Model pair that implements a popover custom attribute. My specific goals include dismissing the popover upon both button click within the popover itself, and from clicking anywhere else on the page.
I can get my doSomething() VM function to work on the element level in the view, but not within an attribute containing an element. I've explained the issue further in the code comments. I'd appreciate some guidance. Thanks!
blog.html
<template>
<require from="../popover/popover"></require>
...
<!-- doSomething() works here! -->
<button type='button' click.trigger='doSomething()'>ClickMe</button>
<!-- doSomething() does not work here! -->
<!-- `click.trigger`/`click.delegate` does not trigger anything, while `onclick` shows
"Uncaught ReferenceError: doSomething is not defined" -->
<span class="glyphicon glyphicon-star" popover="title.bind: blogpost.title; placement.bind: 'top'"
data-content='<button type="button" click.trigger="doSomething()">ClickMe</button>' ></span>
...
</template>
blog.ts
...
doSomething() {
console.log('doing something');
}
popover.ts
...
bind() {
$(this.element).popover({
title: this.title,
placement: this.placement,
content: this.content,
trigger: 'click',
html: true
});
}
I recently went through the same problem. I'm currently working on a Bootstrap port for Aurelia, it is not done yet, and I haven't written any documentation, but the popover is already implemented.
You are more than welcome to take a look:
https://github.com/tochoromero/aurelia-bootstrap/tree/master/src/popover
The way you are trying to implement the popover is going to be very complicated (if even possible), things are going to get messy with the scopes. If you only had text then it would be fine, but you basically want to be able to put anything in the popover and have it bound to the right View-Model.
The way I solved this was having a couple of Custom Elements that represent the popover, I have AubsCustomPopover, AubsPopoverTitle and AubsPopoverContent.
With this 3 custom elements you will create the bootstrap markup for the popover, and because you are adding them directly in the view they will have the right View-Model, and you can do whatever you want inside them.
And then, there is a custom attribute, this is AubsPopover, this is the one that will be in charge of showing and hiding the custom attribute depending on the trigger action you specify (hover, click, focus, outsideClick).
The code using it looks something like this:
<aubs-custom-popover model.bind="customPopover">
<aubs-popover-title>
This is my awesome title <i class="fa fa-star"></i>
</aubs-popover-title>
<aubs-popover-content>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" text.bind="password"
placeholder="Password">
</div>
<button class="btn btn-default" click.delegate="isOpen = false">
Close
</button>
</aubs-popover-content>
</aubs-custom-popover>
<button class="btn btn-primary"
aubs-popover="custom-model.bind: customPopover;
trigger: outsideClick;
position: bottom;
open.bind:isOpen">
Custom Popover
</button>
As I said the Popover is fully implemented but I haven't written any documentation, I want to have a couple of more components before I do that.
If you want to use it, I can give you some help setting it up.
I'm trying to add a BootStrap modal to an already-existing page and I'm running into the following problem:
The modal displays exactly as I would expect it to, content in place looking quite beautiful but I have two problems. If I specify only class="modal" the modal displays by default when the page loads and will not close. If I include class="modal hide" then the modal does not display at page load but also doesn't close when the appropriate buttons are clicked.
I'm not making any big departures from the modals sample code on the Bootstrap site, any ideas what's going wrong?
Here's the button that's supposed to launch the modal:
<a class="btn" data-toggle="modal" href="#testmodal" >About</a>
And here's the modal itself:
<div class="modal hide fade in" id="testmodal">
<div class="modal-header">
<button class="close" data-dismiss="modal">x</button>
<h3>Modal Header</h3>
</div>
<div class="modal-body">
<p>body</p>
</div>
<div class="modal-footer">
Close
</div>
</div>
And for the record, yes I did remember to include bootstrap-modal.js and bootstrap-transition.js
Glad your problem is resolved.
Also if you want the default bootstrap functionality without customizing anything just use the big download button on the main page. This zip file contains a default bootstrap.min.js file that includes all the plugins (like the modal dialogs etc). If you use that one instead of all the seperate javascript files your page has to make less requests and load faster.
your close link should be like this:
<a class="close" data-dismiss="modal">×</a>
since, data-dismiss is a custom HTML5 data attribute. Here it is used to close the modal window.