Home > Software engineering >  How to use laravel route with parameter in external JS file
How to use laravel route with parameter in external JS file

Time:03-22

I have an external js file .js and I want to use the following url which fetches me the json data to be used in the datatable.

https://url/admin/farmers/products-data/1 I fetch the json using the url parameter. I just don't know how to add it to the js file. Kindly assist me

var dt_project = dt_project_table.DataTable({
    ajax: {
        'url':"https://url.com/admin/farmers/products-data/{parameter}",
         dataSrc: 'products'
    },  // JSON file to add data
    ordering: false,
    columns: [
        // columns according to JSON
        { data: '' },
        { data: 'title' },
        { data: 'description' },
        { data: 'code' },
        { data: 'delivery_mode' }
    ]
});

CodePudding user response:

You can store the parameter in a data- attribute of an element on the page and then retrieve it with jQuery in the external js file. Try this

Do something like this in the HTML

<div id="some-element" data-id="1">
    <!-- some content -->
</div>

And this in the external js file

var parameter = $('#some-element').data('id');
var dt_project = dt_project_table.DataTable({
    ajax: {
        'url':"https://url.com/admin/farmers/products-data/" parameter,
         dataSrc: 'products'
    },  // JSON file to add data
    ordering: false,
    columns: [
        // columns according to JSON
        { data: '' },
        { data: 'title' },
        { data: 'description' },
        { data: 'code' },
        { data: 'delivery_mode' }
    ] 
});
  • Related