The most practical way I can think of is for you to call modal where necessary, by doing so within a function where you first collect the data you need and send it to modal.
Remove the modal call via attribute from your code data
:
<a data_toggle="modal"/> <!-- não usar isto, vamos chamar com JavaScript -->
jQuery
$(document).ready(function(){
// anexamos o evento à classe de CSS "modal-trigger"
$('.modal-trigger').on("click", function(e) {
e.preventDefault();
// recolhemos os dados que pretendemos
var $this = $(this),
$modal = $($this.data("target")),
name = $this.data("name"),
action = $this.data("action"),
title = $this.closest("tr").find("td:first-child").html();
// se a markup da modal existe, alteramos o que pretendemos
if ($modal.size()==1) {
$modal.find('#myModalLabel').html("A " + action + " " + title);
$modal.find('.modal-body').html("Hei " + name + ", é para " + action + "?");
// chamamos a modal
$modal.modal('show');
}
});
});
In the code above and example below, what is done:
- Attach click event so that by clicking an element of the page that contains the CSS class
modal-trigger
a certain code is executed;
- We cancel the default behavior of this element using the method
preventDefault()
;
- We collect data from the button using the method
.data()
, additionally we collect a text from the table row where the button is;
- We check whether the modal is present in the DOM by counting the total of elements with the ID of the DOM, see method
.size()
;
- If there is we will change the title of modal and the contents thereof with the data received;
- Finally, we call the modal making use of the method
.modal('show')
.
Demonstrations
$(document).ready(function(){
$('.modal-trigger').on("click", function(e) {
e.preventDefault();
var $this = $(this),
$modal = $($this.data("target")),
name = $this.data("name"),
action = $this.data("action"),
title = $this.closest("tr").find("td:first-child").html();
if ($modal.size()==1) {
$modal.find('#myModalLabel').html("A " + action + " " + title);
$modal.find('.modal-body').html("Hei " + name + ", é para " + action + "?");
$modal.modal('show');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<table class="table table-bordered">
<tbody>
<tr>
<td>
Coisas boas na vida
</td>
<td>
<button type="button" class="btn btn-primary btn-sm modal-trigger" data-target="#myModal" data-name="bubu" data-action="Editar">Editar</button>
</td>
<td>
<button type="button" class="btn btn-danger btn-sm modal-trigger" data-target="#myModal" data-name="bubu" data-action="Apagar">Apagar</button>
</td>
</tr>
</tbody>
</table>
<!-- Modal -->
<div class="modal fade" id="myModal" 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"><span aria-hidden="true">×</span><span class="sr-only">Fechar</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
<button type="button" class="btn btn-primary">Siga...</button>
</div>
</div>
</div>
</div>
Demonstration also in Jsfiddle.
You want to call the method that returns a
ActionResult
in Controller and display a modal in JS, that’s it?– Leonel Sanches da Silva
The modal already works as shown in the code snippet above when I click Edit it generates the modal for me, but I can’t get the values of the line I clicked. Hence somehow returning these values to the modal I believe should be done by Actionresult.
– Rabelos
Va truth the return is by the same JS.
– Leonel Sanches da Silva