Opening dialog() not working

Asked

Viewed 373 times

1

Well, in a way I made some changes to my code, which is instead of using a table i am using a list hierarchy in HTML li.

The code in the fiddle is exactly the same as my code within the system (with the difference that the li are generated dynamically).

But no way mine $('#dialog').dialog() opens when I go to test inside the system. This is strange, because in fiddle it works normally.

On the console he shoots me the next stack:

Uncaught TypeError: undefined is not a function  service:1084
 (anonymous function)   jquery.js:4618
 jQuery.event.dispatch   jquery.js:4302
 elemData.handle

I’ve tried to change the place of the div that contains the id="dialog", but I was unsuccessful.

  • 1

    Is jsFiddle’s HTML the same as the original code? Check it out here: http://jsfiddle.net/a66fx2L5/

  • Hi Sergio, it’s actually almost that. But in mine is a list. I created altered the file to look similar as I am using in the project. Each item in the list has an automatically generated ID, I just want that when the user click "Remove" an AJAX call is made and with the success of it the line is removed. Is it possible to do it with the list? Here is the link: http://jsfiddle.net/joaoManolo/a66fx2L5/1/

  • 1

    João this HTML is very different from the question... I think it was worth changing the question. However, this new HTML lacks the div #dialog. Take a look here: http://jsfiddle.net/a66fx2L5/5/ - if that’s what you want I can put an answer explaining better what was missing.

  • @Sergio, I agree with you. Well, the "disappear" part of the component works normally, as does the opening of the dialog(), but when I pass to my system the same does not work, that is, the dialog does not open and triggers an error on the console. I edited the question with the stack of errors for easy viewing.

  • 1

    Because in jsFiddle I added jQueryUI, .js and .css, that’s what made the dialog work. I’ll see if I can find the Boostrap modal and put it here...

  • So @Sergio, I use jQuery. I found the Bootstrap dialog (BootstrapDialog.show({});), but the same does not work. He accuses BootstrapDialog is not defined.

  • 1

    Well, I’m preparing an answer, but I’m not finished today. I’ll do it tomorrow. It either makes jQuery UI or Bootstrap have limitations but has the advantage of no more files to upload...

  • Thank you for your attention @Sergio. I will await your reply, but in parallel to this I will try to resolve also.

Show 3 more comments

2 answers

3


The problem and some confusion of yours (and mine, because I had to study jQuery UI and Bootstrap to answer) is that both Bootstrap and jQuery UI have dialogs/modals. So in his code he was loading Bootstrap but using Javascript/jQuery code for a jQueryUI dialog.

My answer assumes that you continue to use Bootstrap, and removes part of the code you had that was for jQuery UI.

So you need to change:

#1 - join the modal/dialog HTML somewhere on the page, for example:

<!-- 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">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        Tem a certeza?
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
        <button type="button" class="btn btn-primary">Apagar</button>
      </div>
    </div>
  </div>
</div>

#2 - change the HTML of the buttons to have Bootstrap attributes:

data-toggle="modal" data-target="#myModal"

#3 - change Javascript to use the Bootstrap library way

$('#myModal').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget) // botão que abriu o modal
    var aRemover = button.closest('tr');
    $('#confirmar').on('click', function () {
        $('#myModal').modal('hide')
        aRemover.fadeOut(500, function () {
            aRemover.remove();
        });
        // aqui pode chamar o seu AJAX
    });
});

$('#myModal').on('hide.bs.modal', function (event) {
    $('#confirmar').off('click');
});

Note that in this case/example I used the suggested HTML in the Bootstrap documentation, and gave an ID to the delete button to be more convenient. The HTML I added is:

<!-- 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">&times;</span><span class="sr-only">Close</span>

                </button>
                 <h4 class="modal-title" id="myModalLabel">Modal title</h4>

            </div>
            <div class="modal-body">Tem a certeza?</div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                <button type="button" id="confirmar" class="btn btn-primary">Confirmar</button>
            </div>
        </div>
    </div>
</div>

P.S.: I saw now that you edited the question to use lists instead of table. Then you need to change:

var aRemover = button.closest('tr');

for

var aRemover = button.closest('li');
  • Hello @Sergio. I apologize for the delay, but I ended up traveling and did not give time to answer. It worked perfectly. Thanks again for the help. I think the confusion was even in defining which dialog/modal to use, jQuery or Bootstrap.

1

Using your basic jsfiddle I made some modifications. I believe I have understood correctly what you want.

I put the remove() outside the successfrom AJAX for you to see working, then just put it inside back.

Link jsfiddle

  • Hi Daniel is almost right, but I wanted to accomplish the same with a list. in the comment of my question I put a finddle that illustrates exactly what I was trying to do, but in case it doesn’t work I will adapt my code to work with the table. Thank you for your attention

Browser other questions tagged

You are not signed in. Login or sign up in order to post.