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 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块
  • ¥15 nhanes加权logistic回归,svyglm函数