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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制