通过datatables ajax属性传递给控制器以供使用的Id。

huangapple go评论95阅读模式
英文:

Passing Id to be used in controller via datatables ajax property

问题

I'm trying to get products variants using the product's id in the controller and displaying them using datatables server-side processing as follows:

$("#productVariants").DataTable({
    responsive: true,
    lengthChange: false,
    autoWidth: false,
    dom: "Bfrtip",
    buttons: ["pageLength", "copy", "csv", "excel", "pdf", "print"],
    processing: true,
    serverSide: true,
    ajax: {
        url: "/shop/product/variants/",
        type: "GET",
        'data': {
            id: $("#productVariants").attr("rel"),
        },
    },
    columns: [
        { data: "description", name: "description" },
        { data: "cost", name: "cost" },
        { data: "status", name: "status" },
        {
            data: "id",
            render: function(data, type, row) {
                return (
                    '<div class="dropdown dropdown-action">' +
                    '<a href="#" class="action-icon dropdown-toggle"'+
                    'data-toggle="dropdown" aria-expanded="false">' +
                    '<i class="fa fa-ellipsis-v"></i></a>' +
                    '<div class="dropdown-menu dropdown-menu-left">' +
                    '<a href="/shop/product/variants/' +
                    data +
                    '/edit"' +
                    'class="dropdown-item" data-toggle="tooltip"' +
                    'data-placement="top" title="Edit">' +
                    '<i class="fa fa-pen"' +
                    'aria-hidden="true"></i> Edit</a>' +
                    '<a rel="' +
                    data +
                    '" rel1="delete" href="javascript:"' +
                    'class="dropdown-item deleteProductVariant"' +
                    'data-toggle="tooltip" data-placement="top" title="Delete">' +
                    '<i class="fa fa-trash"></i> Delete</a>' +
                    '</div>' +
                    '</div>'
                );
            },
            name: "Action",
            orderable: false,
            searchable: false,
            printable: false
        }
    ],
    stateSave: true,
    bDestroy: true
});

I do have a route configured as follows:

Route::get('/variants/{id}', [ProductController::class, 'variants']);

In the controller, the function gets the id from the route as follows:

if ($request->ajax()) {
    $data = ProductVariant::join('statuses', 'product_variants.status_id',
      'statuses.id')->join('variant_types', 'product_variants.variant_type_id',
      'variant_types.id')->join('variant_categories', 'variant_types.cat_id',
      'variant_categories.id')->select('product_variants.id As id',
      'variant_types.name As name','product_variants.cost As cost',
      'variant_categories.name As category', 'statuses.name As status')
      ->where(['product_variants.p_id'=>$id])->get();
    return Datatables::of($data)->addIndexColumn()->make(true);
}

When I run it, I get the following error in the console:

GET http://127.0.0.1:8000/shop/product/variants/?draw=1&columns%5B0%5D%5Bdata%5D=category&columns%5B0%5D%5Bname%5D=category&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=name&columns%5B1%5D%5Bname%5D=name&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=cost&columns%5B2%5D%5Bname%5D=cost&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=status&columns%5B3%5D%5Bname%5D=status&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=id&columns%5B4%5D%5Bname%5D=Action&columns%5B4%5D%5Bsearchable%5D=false&columns%5B4%5D%5Borderable%5D=false&columns%5B4%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B4%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&id=742&_=1677158506437 404 (Not Found)

From my understanding, a new URL is being formed, which is not found, yet I expected the supplied URL to be used, and the id passed just like it is done in Laravel routing. How can I get to pass the id correctly and achieve the desired output?

英文:

Im trying to get products variants using the product's id in the controller and displaying them using datatables server side processing as follows

      $(&quot;#productVariants&quot;).DataTable({
responsive: true,
lengthChange: false,
autoWidth: false,
dom: &quot;Bfrtip&quot;,
buttons: [&quot;pageLength&quot;, &quot;copy&quot;, &quot;csv&quot;, &quot;excel&quot;, &quot;pdf&quot;, &quot;print&quot;],
processing: true,
serverSide: true,
ajax: {
url: &quot;/shop/product/variants/&quot;,
type: &quot;GET&quot;,
&#39;data&#39;: {
id: $(&quot;#productVariants&quot;).attr(&quot;rel&quot;),
},
},
columns: [
{ data: &quot;description&quot;, name: &quot;description&quot; },
{ data: &quot;cost&quot;, name: &quot;cost&quot; },
{ data: &quot;status&quot;, name: &quot;status&quot; },
{ data: &quot;id&quot;,
render: function(data, type, row) {
return (
&#39;&lt;div class=&quot;dropdown dropdown-action&quot;&gt;&#39; +
&#39;&lt;a href=&quot;#&quot; class=&quot;action-icon dropdown-toggle&quot;&#39;+ 
&#39;data-toggle=&quot;dropdown&quot; aria-expanded=&quot;false&quot;&gt;&#39;+
&#39;&lt;i class=&quot;fa fa-ellipsis-v&quot;&gt;&lt;/i&gt;&lt;/a&gt;&#39; +
&#39;&lt;div class=&quot;dropdown-menu dropdown-menu-left&quot;&gt;&#39; +
&#39;&lt;a href=&quot;/shop/product/variants/&#39; +
data +
&#39;/edit&quot;&#39; +
&#39;class=&quot;dropdown-item&quot; data-toggle=&quot;tooltip&quot;&#39; +
&#39;data-placement=&quot;top&quot; title=&quot;Edit&quot;&gt;&#39; +
&#39;&lt;i class=&quot;fa fa-pen&quot;&#39; +
&#39;aria-hidden=&quot;true&quot;&gt;&lt;/i&gt; Edit&lt;/a&gt;&#39; +
&#39;&lt;a rel=&quot;&#39; +
data +
&#39;&quot; rel1=&quot;delete&quot; href=&quot;javascript:&quot;&#39; +
&#39;class=&quot;dropdown-item deleteProductVariant&quot;&#39; +
&#39;data-toggle=&quot;tooltip&quot; data-placement=&quot;top&quot; title=&quot;Delete&quot;&gt;&#39; +
&#39;&lt;i class=&quot;fa fa-trash&quot;&gt;&lt;/i&gt; Delete&lt;/a&gt;&#39; +
&quot;&lt;/div&gt;&quot; +
&quot;&lt;/div&gt;&quot;
);
},
name: &quot;Action&quot;,
orderable: false,
searchable: false,
printable: false
}
],
stateSave: true,
bDestroy: true
});

I do have a route configured as follows

      Route::get(&#39;/variants/{id}&#39;, [ProductController::class, &#39;variants&#39;]);

In the controller, the function gets the id from the route as follows

      if ($request-&gt;ajax()) {
$data = $data = ProductVariant::join(&#39;statuses&#39;,&#39;product_variants.status_id&#39;,
&#39;statuses.id&#39;)-&gt;join(&#39;variant_types&#39;,&#39;product_variants.variant_type_id&#39;,
&#39;variant_types.id&#39;)-&gt;join(&#39;variant_categories&#39;,&#39;variant_types.cat_id&#39;,
&#39;variant_categories.id&#39;)-&gt;select(&#39;product_variants.id As id&#39;,
&#39;variant_types.name As name&#39;,&#39;product_variants.cost As cost&#39;,
&#39;variant_categories.name As category&#39;, &#39;statuses.name As status&#39;)
-&gt;where([&#39;product_variants.p_id&#39;=&gt;$id])-&gt;get();
return Datatables::of($data)-&gt;addIndexColumn()-&gt;make(true);
}

When I run it I get the following error in the console

       GET http://127.0.0.1:8000/shop/product/variants/?draw=1&amp;columns%
5B0%5D%5Bdata%5D=category&amp;columns%5B0%5D%5Bname%
5D=category&amp;columns%5B0%5D%5Bsearchable%5D=true&amp;columns%
5B0%5D%5Borderable%5D=true&amp;columns%5B0%5D%5Bsearch%5D%5Bvalue
%5D=&amp;columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&amp;columns%5B1
%5D%5Bdata%5D=name&amp;columns%5B1%5D%5Bname%5D=name&amp;columns
%5B1%5D%5Bsearchable%5D=true&amp;columns%5B1%5D%5Borderable
%5D=true&amp;columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&amp;columns%5B1
%5D%5Bsearch%5D%5Bregex%5D=false&amp;columns%5B2%5D%5Bdata
%5D=cost&amp;columns%5B2%5D%5Bname%5D=cost&amp;columns%5B2%5D%
5Bsearchable%5D=true&amp;columns%5B2%5D%5Borderable%5D=true&amp;
columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&amp;columns%5B2%
5D%5Bsearch%5D%5Bregex%5D=false&amp;columns%5B3%5D%5Bdata%5D=
status&amp;columns%5B3%5D%5Bname%5D=status&amp;columns%5B3%5D%
5Bsearchable%5D=true&amp;columns%5B3%5D%5Borderable%5D=true&amp;
columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&amp;columns%5B3%5D%5
Bsearch%5D%5Bregex%5D=false&amp;columns%5B4%5D%5Bdata%5D=id&amp;
columns%5B4%5D%5Bname%5D=Action&amp;columns%5B4%5D%5Bsearchable
%5D=false&amp;columns%5B4%5D%5Borderable%5D=false&amp;columns%
5B4%5D%5Bsearch%5D%5Bvalue%5D=&amp;columns%5B4%5D%5Bsearch%5D
%5Bregex%5D=false&amp;order%5B0%5D%5Bcolumn%5D=0&amp;order%
5B0%5D%5Bdir%5D=asc&amp;start=0&amp;length=10&amp;search%5Bvalue%5D=&amp;
search%5Bregex%5D=false&amp;id=742&amp;_=1677158506437 404 (Not Found)

from my understanding, a new URL is being formed which is not found yet I expected the supplied URL to be used and the id passed just like is done in laravel routing.
How can I get to pass the id correctly and achieve the desired output?

答案1

得分: 0

对于任何遇到相同问题的人,这是我解决问题的方式。
我将路由方法更改为POST,将路由中的id变量移除。
然后,我将数据表代码更改如下:

$("#productVariants").DataTable({
    responsive: true,
    lengthChange: false,
    autoWidth: false,
    dom: "Bfrtip",
    buttons: ["pageLength", "copy", "csv", "excel", "pdf", "print"],
    processing: true,
    serverSide: true,
    ajax: {
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url: "/shop/product/variants/",
        type: "POST",
        'data': {
            id: $("#productVariants").attr("rel"),
        },
    },
    columns: [
        { data: "category", name: "category" },
        { data: "name", name: "name" },
        { data: "cost", name: "cost" },
        { data: "quantity", name: "quantity" },
        { data: "status", name: "status" },
        { data: "id",
            render: function(data, type, row) {
                return (
                    '<div class="dropdown dropdown-action">' +
                    '<a href="#" class="action-icon dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="fa fa-ellipsis-v"></i></a>' +
                    '<div class="dropdown-menu dropdown-menu-left">' +
                    '<a href="/shop/product/variants/' +
                    data +
                    '/edit"' +
                    'class="dropdown-item" data-toggle="tooltip"' +
                    'data-placement="top" title="Edit"><i class="fa fa-pen"' +
                    'aria-hidden="true"></i> Edit</a>' +
                    '<a rel="' +
                    data +
                    '" rel1="delete" href="javascript:"' +
                    'class="dropdown-item deleteProductVariant"' +
                    'data-toggle="tooltip" data-placement="top" title="Delete"><i class="fa fa-trash"></i> Delete</a>' +
                    "</div>" +
                    "</div>"
                );
            },
            name: "Action",
            orderable: false,
            searchable: false,
            printable: false
        }
    ],
    stateSave: true,
    bDestroy: true
});

请注意,唯一显著的变化是将方法更改为POST。

英文:

For anyone having the same issue, here is how I solved the problem.
I changed the route method to post removing the id variable from the route
I then changed the datatable code as follows

        $(&quot;#productVariants&quot;).DataTable({
responsive: true,
lengthChange: false,
autoWidth: false,
dom: &quot;Bfrtip&quot;,
buttons: [&quot;pageLength&quot;, &quot;copy&quot;, &quot;csv&quot;, &quot;excel&quot;, &quot;pdf&quot;, &quot;print&quot;],
processing: true,
serverSide: true,
ajax: {
headers: {
&#39;X-CSRF-TOKEN&#39;: $(&#39;meta[name=&quot;csrf-token&quot;]&#39;).attr(&#39;content&#39;)
},
url: &quot;/shop/product/variants/&quot;,
type: &quot;POST&quot;,
&#39;data&#39;: {
id: $(&quot;#productVariants&quot;).attr(&quot;rel&quot;),
},
},
columns: [
{ data: &quot;category&quot;, name: &quot;category&quot; },
{ data: &quot;name&quot;, name: &quot;name&quot; },
{ data: &quot;cost&quot;, name: &quot;cost&quot; },
{ data: &quot;quantity&quot;, name: &quot;quantity&quot; },
{ data: &quot;status&quot;, name: &quot;status&quot; },
{ data: &quot;id&quot;,
render: function(data, type, row) {
return (
&#39;&lt;div class=&quot;dropdown dropdown-action&quot;&gt;&#39; +
&#39;&lt;a href=&quot;#&quot; class=&quot;action-icon dropdown-toggle&quot; data-toggle=&quot;dropdown&quot; aria-expanded=&quot;false&quot;&gt;&lt;i class=&quot;fa fa-ellipsis-v&quot;&gt;&lt;/i&gt;&lt;/a&gt;&#39; +
&#39;&lt;div class=&quot;dropdown-menu dropdown-menu-left&quot;&gt;&#39; +
&#39;&lt;a href=&quot;/shop/product/variants/&#39; +
data +
&#39;/edit&quot;&#39; +
&#39;class=&quot;dropdown-item&quot; data-toggle=&quot;tooltip&quot;&#39; +
&#39;data-placement=&quot;top&quot; title=&quot;Edit&quot;&gt;&lt;i class=&quot;fa fa-pen&quot;&#39; +
&#39;aria-hidden=&quot;true&quot;&gt;&lt;/i&gt; Edit&lt;/a&gt;&#39; +
&#39;&lt;a rel=&quot;&#39; +
data +
&#39;&quot; rel1=&quot;delete&quot; href=&quot;javascript:&quot;&#39; +
&#39;class=&quot;dropdown-item deleteProductVariant&quot;&#39; +
&#39;data-toggle=&quot;tooltip&quot; data-placement=&quot;top&quot; title=&quot;Delete&quot;&gt;&lt;i class=&quot;fa fa-trash&quot;&gt;&lt;/i&gt; Delete&lt;/a&gt;&#39; +
&quot;&lt;/div&gt;&quot; +
&quot;&lt;/div&gt;&quot;
);
},
name: &quot;Action&quot;,
orderable: false,
searchable: false,
printable: false
}
],
stateSave: true,
bDestroy: true
});

Note that the only significant change is the change of method to post

huangapple
  • 本文由 发表于 2023年2月23日 21:29:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定