Vasil Emilov
Vasil Emilov

Reputation: 47

Control the confirm button in bootstrap confirm modal

Confirm modal example

I changed the example to a simple one. I want to call a delete function when I click on "Remove this {{item.id}}" button. The title successfully get the item.id value.

<h4 class="modal-title" id="exampleModalLabel">Do you want to remove  {{item.id}}</h4>

But the button doesn't get the item.id value and the function doesn't work. And instead of "Remove this item.id" it's only "Remove this ", and the function doesn't get the parameter too.

<button type="button" id="exampleModalLabel" class="btn btn-primary" ng-click="removeItem(item.id)">Remove this {{item.id}}</button>

What I have is this:

<tr ng-repeat="item in items">
    <td>{{item.id}}</td>
    <td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="{{item.id}}">Remove this item?</button></td>
</tr>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  <div class="modal-dialog" 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">&times;</span></button>
        <h4 class="modal-title" id="exampleModalLabel">Do you want to remove  {{item.id}}</h4>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" ng-click="removeItem(item)">Remove this {{item.id}}</button>
      </div>
    </div>
  </div>
</div>

//And this javascript
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var recipient = button.data('whatever');
var modal = $(this);
modal.find('.modal-title').text('New message to ' + recipient);
modal.find('.modal-body input').val(recipient);
});

I hope the info is enough. If you need some more info, please tell me.

Upvotes: 3

Views: 1760

Answers (2)

Paul Moldovan
Paul Moldovan

Reputation: 464

Try using this:

<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="button" id="removeButton" class="btn btn-primary">Remove this <span id="itemid"></span></button>
</div>

//JS
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var recipient = button.data('whatever');
var modal = $(this);
modal.find('.modal-title').text('New message to ' + recipient);
modal.find('.modal-body input').val(recipient);

modal.find('#itemid').html(recipient); // add this
modal.find('#removeButton').attr('ng-click', 'removeItem('+recipient+')'); // add this
});

Upvotes: 2

Gvidas
Gvidas

Reputation: 1964

Your modal is out of items scope. You need to assign your item to some temp variable inside controller. You should use ng-click to achieve that, something like this ng-click="tempItem = item". You probably also have to edit your removeItem function.

<tr ng-repeat="item in items">
    <td>{{item.id}}</td>
    <td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="{{item.id}}" ng-click="tempItem = item">Remove this item?</button></td>
</tr>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  <div class="modal-dialog" 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">&times;</span></button>
        <h4 class="modal-title" id="exampleModalLabel">Do you want to remove  {{tempItem.id}}</h4>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" id="exampleModalLabel" class="btn btn-primary" ng-click="removeItem(tempItem)">Remove this {{tempItem.id}}</button>
      </div>
    </div>
  </div>
</div>

//And this javascript
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var recipient = button.data('whatever');
var modal = $(this);
modal.find('.modal-title').text('New message to ' + recipient);
modal.find('.modal-body input').val(recipient);
});

Upvotes: 2

Related Questions