Passing parameters to Modal in jQuery

5

I have the following button:

echo '<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-cliente='.$r->idCliente.' data-datainicial='.$_GET['data_inicial'].' data-datafinal='.$_GET['data_final'].' data-target="#myModal">Boleto</button>';

I would like to recover data-cliente, data-datainicial, data-datafinal and put it within modal in div :

<div...>
        <div class="modal-footer">
          <a href="teste/cliente/datainicial/datafinal"><button type="button" class="btn btn-danger" data-dismiss="modal">Gerar PDF</button></a> <---- Aqui preciso recuperar estas 3 variaveis. 
          <a href="teste/cliente/datainicial/datafinal"><button type="button" class="btn btn-primary" data-dismiss="modal">Enviar E-Mail</button></a> <---- Aqui preciso recuperar estas 3 variaveis. 
        </div>
</div>

How can I recover this data?

    
asked by anonymous 27.11.2015 / 02:37

2 answers

2

I did not find a way to know which button opened dialog , but using a variable to pass this element is already possible. Try this:

var handler;
$("#dialog").dialog({
    autoOpen: false
}).on("dialogopen", function (event, ui) {
    var $btn = $(handler);
    var id = $btn.data('cliente');
    var dataInicial = $btn.data('datainicial');
    var dataFinal = $btn.data('datafinal');
    var buttons = $(event.target).find('.btn').attr({
        'data-cliente': id,
        'data-datainicial': dataInicial,
        'data-datafinal': dataFinal
    });
});;


$('.btn').click(function () {
    handler = this;
    $('#dialog').dialog('open');
});

I'll explain the code sooner, now it took longer than I anticipated and I was in a hurry to leave ...

    
27.11.2015 / 07:46
0

I got the solution this way, if anyone needs it too:

<script>
    $(document).ready(function(){
        $(".boleto_campo").click(function(){ 
            $("#cliente").val($(this).data('cliente'));
            $("#datainicial").val($(this).data('datainicial'));
            $("#datafinal").val($(this).data('datafinal'));
            var cliente = $(this).data('cliente');
            var datainicial = $(this).data('datainicial');
            var datafinal = $(this).data('datafinal');
            $("#form_modal").modal('show');

            $(".gerar_pdf").click(function(){
                $('#myModal').modal('hide');
                window.location.href = "<? echo base_url('boleto/gerar/'); ?>/" + cliente + "/" + datainicial + "/" + datafinal;    
            });
        });
    });
</script>
    
27.11.2015 / 15:08