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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵