简体   繁体   English

从数据库和表中删除行但保留在同一页面Laravel

[英]Delete row from db and table but stay on same page Laravel

hi all I've got a table displaying data in my view in laravel-4 . 大家好我有我的显示数据的表格viewlaravel-4 When the user selects a row to delete a pop up displays asking them are they sure they want to delete this row. 当用户选择要删除的行时,弹出显示询问他们是否确定要删除此行。 When they click confirm I remove the row and I would also like to remove the data from the database 当他们点击确认我删除了行,我也想从database删除数据

However this is my problem, How can I do this without navigating away from my current view and back again. 然而,这是我的问题,如何在不离开当前view再次返回。 I'd like the user to stay on the page. 我希望用户留在页面上。

I know it's not a good idea to call controller methods from within the view so I'd like to avoid that. 我知道从view调用controller方法不是一个好主意,所以我想避免这种情况。 I'm at a loss, I also put the code into my route thinking maybe it would execute and stay on the page but that did not work. 我很茫然,我也把代码放到我的route想也许它会执行并保留在页面上,但这不起作用。

This is my view 这是我的view

    <div class="panel panel-default">
        <div class="panel-heading">
            All Tweets
            <div class="pull-right btn-toolbar">
                <a href="#" class="btn btn-danger" id="delete_selected">Delete Selected</a>
            </div>
        </div>
        <div class="panel-body">
            <table class="table table-hover" id="tweets_table">
                <thead>
                    <tr>
                        <th>Select</th>
                        <th>Tweet</th>
                        <th>Username</th>
                        <th>Name</th>
                        <th>Tweeted at</th>
                    </tr>
                </thead>
                @foreach(Tweet::orderBy('created_at', 'DESC')->get() as $tweet)
                    <tr id="{{$tweet->tweet_id}}">
                        <td><a class="btn btn-danger" id="delete">Delete</a></td>
                        <td id="tweet_text">{{$tweet->tweet_text}}</td>
                        <td id="tweet_user">{{$tweet->screen_name}}</td>
                        <td>{{$tweet->name}}</td>
                        <td id="tweet_date">{{$tweet->created_at}}</td>
                    </tr>
                @endforeach
            </table>
        </div>
    </div>

    <!-- Twitter Bootstrap Modal -->
<div class="modal" id="myModal">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h4 class="modal-title">Warning: Delete Tweet</h4>
        </div>
        <div class="modal-body">
          <p>Are you sure you would like to delete this Tweet from the database?</p>
          <br/>
          <strong>Tweet: </strong><p class="modal_tweet_text"></p>
          <strong>Username: </strong><p class="modal_tweet_user"></p>
          <strong>Tweet Date: </strong><p class="modal_tweet_date"></p>
        </div>
        <div class="modal-footer">
          <a href="#" data-dismiss="modal" class="btn">No</a>
          <a href="{{action('AdminBaseController@deleteTweet')}}" class="btn btn-primary" id="confirm_btn">Yes</a>
        </div>
      </div>
    </div>
</div>
    <!-- PUT THIS INTO EXTERNAL JS -->
    <script>
        //When the delete button is clicked open the pop up.
        $('#delete').click(function(){
            $('#myModal').modal({show:true});
            //Get the clicked row
            var row = $(this).closest('tr');
            //Get the tweet, user and date
            var tweet = row.find('#tweet_text');
            var user = row.find('#tweet_user');
            var date = row.find('#tweet_date');

            //Display details in the pop up
            $('.modal_tweet_text').text(tweet.text());
            $('.modal_tweet_user').text(user.text());
            $('.modal_tweet_date').text(date.text());

            //Confirm Action 
            $('#confirm_btn').click(function(){
                row.remove();                   //remove the row
                $('#myModal').modal('hide');//Hide the popup
            });
        });
    </script>

This is my routes.php 这是我的routes.php

Route::get('dashboard/delete-tweet', 'AdminBaseController@deleteTweet');//delete tweet from db - dashboard.

This is the controller 这是controller

public function deleteTweet(){

        //Id of tweet to delete


        //return to dashboard


    }

I know I've not got any logic in the action, I just want to know how I could call this logic without navigating away from the view. 我知道我在动作中没有任何逻辑,我只是​​想知道如何在不离开视图的情况下调用这个逻辑。

You could take a look at JSON (get)-requests that perform these sorts of actions without having the need of reloading or redirecting back to the page. 您可以查看执行这些操作的JSON(get)请求,而无需重新加载或重定向回页面。 For instance, you could attach click events to your delete buttons. 例如,您可以将点击事件附加到删除按钮。 That event will trigger a JSON get-request to the url ./dashboard/delete-tweet/{id} . 该事件将触发对URL ./dashboard/delete-tweet/ {id}的JSON get-request。 Based on that ID, your 'tweet' can be deleted. 根据该ID,您的“推文”可以删除。 After that, you would normally use 之后,您通常会使用

return Redirect::to('page');

at the end of your controller, but in this case you can use 在您的控制器的末尾,但在这种情况下,您可以使用

return Response::json(array('message' => 'Success, or w/e'));

In your JSON requests, you can check wether it was executed successfully - so that you can also delete the row from your view. 在您的JSON请求中,您可以检查它是否已成功执行 - 这样您也可以从视图中删除该行。 See this page for more information on how to setup these JSON requests. 有关如何设置这些JSON请求的详细信息,请参阅页面。

You are going to need to fire an ajax call which will make a request to Laravel which will delete the record, and then probably return some kind of success message. 您将需要触发一个ajax调用,该调用将向Laravel发出请求,该请求将删除该记录,然后可能返回某种成功消息。

One issue I see currently is all of your element ids should be unique, so you will probably want to change that as well for this to work correctly. 我目前看到的一个问题是你的所有元素ID都应该是唯一的,所以你可能想要改变它以使其正常工作。 I added a class to the delete rows called tweet_delete to give you something to grab and modified the jquery, as well as added the relevant ajax and a data-id attribute which will hold the id of the tweet so we know which one to delete. 我在一个名为tweet_delete的删除行中添加了一个类,以便为您提供抓取和修改jquery的功能,并添加相关的ajax和data-id属性,该属性将保存推文的ID,以便我们知道要删除哪一个。

<div class="panel panel-default">
    <div class="panel-heading">
        All Tweets
        <div class="pull-right btn-toolbar">
            <a href="#" class="btn btn-danger" id="delete_selected">Delete Selected</a>
        </div>
    </div>
    <div class="panel-body">
        <table class="table table-hover" id="tweets_table">
            <thead>
                <tr>
                    <th>Select</th>
                    <th>Tweet</th>
                    <th>Username</th>
                    <th>Name</th>
                    <th>Tweeted at</th>
                </tr>
            </thead>
            @foreach(Tweet::orderBy('created_at', 'DESC')->get() as $tweet)
                <tr id="{{$tweet->tweet_id}}">
                    <td><a class="btn btn-danger tweet_delete" data-id="{{ $tweet->tweet_id }}">Delete</a></td>
                    <td id="tweet_text">{{$tweet->tweet_text}}</td>
                    <td id="tweet_user">{{$tweet->screen_name}}</td>
                    <td>{{$tweet->name}}</td>
                    <td id="tweet_date">{{$tweet->created_at}}</td>
                </tr>
            @endforeach
        </table>
    </div>
</div>

<!-- Twitter Bootstrap Modal -->
<div class="modal" id="myModal">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h4 class="modal-title">Warning: Delete Tweet</h4>
        </div>
        <div class="modal-body">
          <p>Are you sure you would like to delete this Tweet from the database?</p>
          <br/>
          <strong>Tweet: </strong><p class="modal_tweet_text"></p>
          <strong>Username: </strong><p class="modal_tweet_user"></p>
          <strong>Tweet Date: </strong><p class="modal_tweet_date"></p>
        </div>
        <div class="modal-footer">
          <a href="#" data-dismiss="modal" class="btn">No</a>
          <a href="{{action('AdminBaseController@deleteTweet')}}" class="btn btn-primary" id="confirm_btn">Yes</a>
        </div>
      </div>
    </div>
</div>
<!-- PUT THIS INTO EXTERNAL JS -->
<script>
    //When the delete button is clicked open the pop up.
    $('.tweet_delete').click(function(){
        $('#myModal').modal({show:true});
        //Get the clicked row
        var row = $(this).closest('tr');
        //Get the tweet, user and date
        var tweet = row.find('#tweet_text');
        var user = row.find('#tweet_user');
        var date = row.find('#tweet_date');

        //Display details in the pop up
        $('.modal_tweet_text').text(tweet.text());
        $('.modal_tweet_user').text(user.text());
        $('.modal_tweet_date').text(date.text());

        //Confirm Action 
        $('#confirm_btn').click(function() {
            row.remove();                   //remove the row
            $('#myModal').modal('hide');    //Hide the popup

            // Fire ajax call to delete row from database.
            $.post('dashboard/delete-tweet', {id: $(this).attr('data-id')}, function(data, textStatus, xhr) {
                if(data.success)
                    alert('Tweet successfully deleted');
                else
                    alert('Something went wrong!');
            });
        });
    });
</script>

And your route would look something like this... 你的路线看起来像这样......

Route::post('dashboard/delete-tweet', function()
{
    $tweet = Tweet::find(Input::get('id'));
    $tweet->delete();
    return Response::json(array('success' => true));
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM