Hi all I'm wondering how I might go about this...I am building a laravel4
application and I would like to call a function in my controller
that deletes all of the selected Items in the list.
This is my view
<div class="panel panel-default">
<div class="panel-heading">
Twitter Winners
<div class="pull-right btn-toolbar">
<a href="{{action('AdminBaseController@deleteSelectedTweets')}}" class="btn btn-danger">Delete Selected</a>
<a href="#" class="btn btn-primary">Confirm Winners</a>
<a href="#" class="btn btn-primary">Generate New List</a>
</div>
</div>
<div class="panel-body">
<table class="table table-hover">
<thead>
<tr>
<th>Delete</th>
<th>Tweet</th>
<th>Username</th>
<th>Name</th>
<th>To</th>
<th>From</th>
</tr>
</thead>
@foreach(Tweet::all() as $tweet)
<tr>
<td><input type="checkbox" name="delete_tweet" value="0"/></td>
<td>{{$tweet->tweet_text}}</td>
<td>{{$tweet->screen_name}}</td>
<td>{{$tweet->name}}</td>
<td><select name="origin">
<option value="0">Origin...</option>
<option value="1">Station 1</option>
<option value="2">Station 2</option>
<option value="3">Station 3</option>
</select>
</td>
<td>
<select name="destination">
<option value="0">Destination...</option>
<option value="1">Station 1</option>
<option value="2">Station 2</option>
<option value="3">Station 3</option>
</select>
</td>
</tr>
@endforeach
</table>
</div>
</div>
Ideall I'd like to execute the function
when the user selects this link
:
<a href="{{action('AdminBaseController@deleteSelectedTweets')}}" class="btn btn-danger">Delete Selected</a>
I don't want to navigate away from the page and that is the only way I see being able to do it.
That is an AJAX call you going to need.
Eg
On click of the delete button: - Get the selected items - Add to AJAX data to be passed - Call AJAX function (the function you reference) - Remove items - Update Dom.
There is too much code there to write on stack overflow, but suggest you look into your Jquery / Javascript side of things with some demos before jumping right in.
With Ajax you need: - Controller function that does the job ex: AjaxController@deleteTweet($id)
Assume that you use post form to transfer selected tweets.
View: First add 1 more class to each tag.
<a href="{{action('AdminBaseController@deleteSelectedTweets')}}" tweet-id={{$tweetID}} class="tweet btn btn-danger">Delete Selected</a>
$(".tweet").click(function(e){
e.preventDefault();
var tweetId = $(this).attr('tweet-id');
var node = $(this);
$.ajax({
url: {{ route_to_ajax_controller }} + "/" + tweetId,
success: function(data){
node.remove();
},
error: function(error){
console.log(error);
}
});
})
Ajax is best solution for this. In case you want to avoid Ajax, you can do something like this:
in AdminController
public function deleteSelectedTweets(){
// yours code
return View::make("view-name"); // View from which you came here
}
This is easy solution, and your page should load fast, and you stay on same page.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.