dooid3005 2016-05-04 15:36
浏览 53
已采纳

Laravel和jquery数据表删除行

I'm using laravel 5.2, Jquery 1.10 and MYSQL 5.7.

I'm using RESTful services in laravel for my message model.

I am trying to render a list of messages in a Jquery datatable and provide a delete button on each row which when pressed should fire the destroy method on MessageController.php, deleting the row then re-loading the message index page to display the updated datatable.

The datatable is being generated correctly initially but the delete button does not fire the delete method on the controller. I'm assuming my ajax code for generating the delete button must be wrong.

Thanks in advance for your help.

My datatable ajax script is-

  $(document).ready( function () {
    $('#message_table').DataTable({

        "searchable": false,
        "ajax": {
            "url": "/api/message",
            "type": "POST",
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        },
        "columns": [
            { "data": "id"},
            { "data": "subject",
                "render": function(data,type,row,meta) {
                    return '<a href="/message/'+row.id+'">'+data+'</a>';
                }
            },
            { "data": "created_at",
                "render": function ( data, type, full, meta ) {
                    // instantiate a moment object and hand it the string date
                    var d = moment(data);
                    var month = d.month() +1 < 10 ? "0" + (d.month() +1) : d.month() +1;
                    var day = d.date()  < 10 ? "0" + (d.date()): d.date();
                    return month + "/" + day + "/" + d.year();
                }
            },
            {"defaultContent": "null", "render": function(data,type,row,meta) {
                return '<button action="' + $(location).attr('protocol') + $(location).attr('host') + '/message/'+row.id+ '"' + 'name="_method"' + 'method="post"' +'type="submit"' + 'value="Delete"'+'>'+ 'Delete</button>';
            }
            }
        ]
    });
} );

The generated HTML for the delete button from the last function is-

<button action="http:localhost:8000/message/6" name="_method" method="post" type="submit" value="Delete">Delete</button>

Message controller delete method-

   public function destroy($id)
{
    Message::destroy($id);

    return Redirect::route('message.index');
}
  • 写回答

1条回答 默认 最新

  • dongpu1881 2016-05-04 15:54
    关注

    If you're using laravel's restful controller then the destroy method is by delete http method. So the best option is to create small form like:

    <form action="http:localhost:8000/message/6" method="post">
        <input type="hidden" name="_method" value="delete">
        <button type="submit">Delete</button>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么制作一个人脸识别门禁系统
  • ¥20 大华dss监控平台网络关闭登不进去
  • ¥15 请使用蚁群算法解决下列问题,并给出我完整的代码
  • ¥20 关于php录入完成后,批量更新数据库
  • ¥15 请教往复密封润滑问题
  • ¥15 cocos creator发布ios包
  • ¥15 comsol压电材料数据
  • ¥35 用python实现除法算法中goldschmidt算法
  • ¥15 汇编代码转换成C代码
  • ¥15 除法算法中的归一化具体是怎么变的?