0

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.

1
  • If that page can load up fast ( no much queries ), you could just write in that AdminController@deleteSelectedTweets function at the end return for same view. And your page will just refresh and do the job. Commented Sep 5, 2014 at 11:54

2 Answers 2

2

That is an AJAX call you going to need.

E.g.

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That's perfect, I will look into that so
-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.