D
D
davidnum952014-03-29 18:53:01
JavaScript
davidnum95, 2014-03-29 18:53:01

How to remove a block by clicking on a link inside it?

In the view, a list of records is generated by the CListView widget:

<? $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
    'ajaxUpdate'=>false,
    'template'=>"{items}"
));

Partial view _view.php:
<div class="something">
    <b><?= $data->username ?></b><br>
    <?= $data->email ?>
    <a href="#" id="deleteSomething">Delete</a>
    <input id="_id" style="display: none;" value="<?= $data->id?>">
</div>
<hr>


It is necessary to delete the something block by clicking on the link, of which there can be many. The difficulty is to collect the URL for sending an Ajax get request to the delete action, and also to make a callback function, i.e. visually remove this same block from the page, or update the widget.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
enchikiben, 2014-03-29
@davidnum95

nothing complicated, in the div add a hidden field with the id record at the link, make the class better. cannot be found by id when there are many records. on click (through on) you hang ajax, and in ajax complete you call update $.fn.yiiListView.update("id-list"); and the list will be updated

$(body),on("click",".deleteSomething",function(){
$.ajax({
  url: "htt://site.ru/delete",
  data:{"id":$(this).closest(".something").find(".id").val()}
})
  .done(function( data ) {
     $(this).closest(".something").remove();
     $.fn.yiiListView.update("id-list"); 
  });
})

somehow offhand

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question