How can I use url parameters with ajax to make an API call in datatables.net-vue3
How can I use url parameters with ajax to make an API call in datatables.net-vue3
I have a working datatable that I'm trying to move over to utilize the vue3 component. The only issue I'm hanging up on is how to provide the url and parameters to ajax.
The currently working example uses the ajax as follows:
export default {
mounted() {
var table = $("#svcTable").DataTable({
ajax: {
url: "http://localhost:5050/api/v1/services2",
data: this.$route.query,
},
columns: [
{
className: "dt-control",
orderable: false,
data: null,
defaultContent: "",
},
{ data: "ipaddr" },
{ data: "hostname" },
{ data: "port" },
],
serverside: true,
ordering: false,
dom: "Bfrtip",
buttons: ["csv", "colvis"],
pageLength: 100,
columnDefs: [
{
targets: [8, 12, 13, 14, 15, 16],
visible: false,
},
{
className: "control",
orderable: false,
targets: 0,
},
],
scrollX: true,
responsive: {
details: {
type: "column",
target: "tr",
},
},
});
the url and query are provided separately and the parameters are pulled from the current url string.
the ajax request works when hard coded as follows:
<DataTable
class="display"
:columns="columns"
ajax="http://localhost:5050/api/v1/services2?port=8080&ipaddr=10.1.1.4"}'
:options="{ select: true }"
ref="table"
width="100%"
>
but any attempts to split the url and query params fails miserably.
side-note: the currently working instance is using server-side functionality but I plan to try scroller to see if I can avoid the server side componentry
Answers
This seems to accomplish the goal. Please let me know if theres an easier way.
If the parameters change you will want to use
ajax.data
as a function like this example.Kevin
The
ajax
parameter of the Vue3 parameter is the correct way to do it. It maps to theajax
parameter, which in turn maps to jQuery'sajax
option. So anything it can do, you can assigned to the Vue3 component'sajax
parameter.As Kevin notes, the
ajax.data
parameter is key and we extend it with the ability to be a function.Allan