skip to Main Content

I am working on a laravel project, i am trying to load my datatable, i wanna i have a column used for sending a post request, here is my script:

var datatable = $('#table').on('xhr.dt', function(e, settings, json, xhr) {
            $("#loadingAnimation").hide();
        }).DataTable({
ajax: {
                url: "/xxxxxxxxx",
                type: "POST",
                dataType: "JSON",
            },
 columns: [
                
                {
                    data: 'id',
                    name: 'id',
                },
{
                    data: null, 
                    "render":function(row, type, val, meta){
                            return `
                            <form method="post" action="/xx/yyy/"+${row[`id`]}>
                            @csrf
                            <button>send</button>
                            </form>
                            `;
                            return '';
                    },
                    "orderable": false
                },
]
});

so in the render part, I wanna i can call the route(/xx/yyy/{id}), i tried to call "/xx/yyy/1" and it worked, but when i wanna tried to use the variable row[id] it cannot work, can you please help me to figure it out? Many THX!

3

Answers


  1. This is JavaScript right? If so then I think you should use:

    return '
    <form method="post" action="/xx/yyy/'+ row["id"] +'">
    @csrf
    <button>send</button>
    </form>
    ';
    

    If it is JavaScript you should not have to use $ and {} for passing the variable and also the variable was outside the "" meaning that it was not added to the value of the action attribute.

    Login or Signup to reply.
  2. Your combining a Blade Directives (@csrf) with Javascript. For this to work the script has to be part on the blade template page (not external). I don’t know if you’re doing this since little code is given.

    Login or Signup to reply.
  3. Here is correct way of adding parameters in blade

    <form method="post" action="/xx/yyy/{{row[`id`]}}"> 
    
    Then form here
    </form>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search