Danil Pyatnitsev
Danil Pyatnitsev

Reputation: 2292

How to pass serialized form data to datatables?

I use datatables with server-side handler. Major code:

$("#dt-flats-build").dataTable({
   processing: true,
   serverSide: true,
   ajax: {
                url: "/api.json",
                data: $('form#filter').serialize()
   },
   });

and I have a form with filter. I pass form data to the api.json as addential params.

So, I can pass only fields one by one, but not all fields from form. Do you have any ideas?

Upvotes: 2

Views: 8060

Answers (3)

Angelicheart
Angelicheart

Reputation: 11

I'm using net core with ViewModel, which required to post with "." as form values. I have below instead, hopefully this helps anyone came across something similar.

ajax: {
  url: "/api.json",
  data: function (d) {
   return $('form#filter').serialize() + "&" + $.param(d);
  }
 }

Upvotes: 1

Danil Pyatnitsev
Danil Pyatnitsev

Reputation: 2292

I think, that I find out solution:

ajax: {
  url: "/api.json",
  data: function ( d ) {
  d.form = $('form#filter').serializeArray();
  }
 }

form puts in var "form"

Upvotes: 6

sid
sid

Reputation: 157

Check out here - https://datatables.net/examples/server_side/post.html

By default, the Ajax request that DataTables makes to obtain server-side processing data is an HTTP GET request. However, there are times when you might wish to use POST. This is very easily done by using the type option of the ajax initialisation option.

Use -

"ajax": {
        url: "/api.json",
        "type": "POST",
        data: $('form#filter').serialize()
    },

Upvotes: 1

Related Questions