【问题标题】:How to pass user_id in ajax route datatable Laravel如何在 ajax 路由数据表 Laravel 中传递 user_id
【发布时间】:2021-05-27 13:10:42
【问题描述】:

我在 Laravel 中有一个数据表,这是我的代码

$(function() {
        var user_id = $(this).data('user_id');
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        var table = $('.data-table').DataTable({
            paging: true,
            info: true,
            autoWidth: false,
            responsive: true,       
            processing: true,
            serverSide: true,
            ajax: "{{ route('detail.user', 'user_id')}}",
         
            columns: [{
                    data: 'DT_RowIndex',
                    name: 'DT_RowIndex',
                    orderable: false,
                    searchable: false,
                },
                {
                    data: 'title',
                    name: 'title',
                    orderable: false,
                },
                {
                    data: 'content',
                    name: 'content',
                    orderable: false,
                    visible: false,
                },
                {
                    data: 'progress',
                    name: 'progress'
                },
                {
                    data: 'status',
                    name: 'status'
                },
                {
                    data: 'kpi',
                    name: 'kpi'
                },
                {
                    data: 'target_selesai',
                    name: 'target_selesai'
                },
                {
                    data: 'action',
                    name: 'action',
                    orderable: false,
                    searchable: false
                },
            ]
        });

        $('body').on('click', '.detailProduct', function() {
           ...
        });
        $('#saveBtn').click(function(e) {
           ...
        });

        $('body').on('click', '.deleteProduct', function() {
           ...
        });

    });

我想从 url 参数中获取 user_id 值(例如:http://127.0.0.1:8000/detail/1000000004),我已经尝试过上面的代码,但它会返回空值。 好像我写了错误的代码来获取 url 参数。因为,如果我将ajax: "{{ route('detail.user', 'user_id')}}", 更改为ajax: "{{ route('detail.user', '1000000004')}}",,程序将按预期工作。我该怎么办?提前致谢

【问题讨论】:

  • 为什么要把用户名放在 html root 上?

标签: laravel datatable datatables laravel-8 yajra-datatable


【解决方案1】:

您不能使用 JavaScript 在 PHP 代码中设置变量。您不应该在动态 JavaScript 代码上使用 PHP 代码。有两种选择:

  1. 使用静态路径代替 Laravel 路由,只需将 ajax: "{{ route('detail.user', 'user_id')}}" 更改为 ajax: "/detail/"+user_id

  2. 使用 Laravel 刀片视图 arg 发送 user_id 变量。在您的控制器上定义 user_id,如下所示:

    return view('index',
            ['user_id', 1000000004]
        );
    

    然后在刀片上调用它作为 php 变量 ($user_id) 示例 ajax: "{{ route('detail.user', $user_id)}}"

【讨论】:

    【解决方案2】:
    1. 确保 url 在 routes/web.php 中,提供一个变量传递,例如 /name-routes/{id}
    2. 然后在您的控制器函数中获取数据 ID,函数 function_name($id){ }
    3. 最后确保格式返回 json,格式类似于 { data : [ name : "value name 1", name : "value_name2" ] }

    【讨论】:

      猜你喜欢
      • 2018-06-05
      • 2017-07-05
      • 2015-10-15
      • 1970-01-01
      • 2015-04-06
      • 2020-09-04
      • 2020-02-05
      • 2016-11-25
      • 2017-01-30
      相关资源
      最近更新 更多