How to get values out of Ajax Request from Datatables(JQuery)

How to get values out of Ajax Request from Datatables(JQuery)

tomspleesterstomspleesters Posts: 3Questions: 2Answers: 0

I have a Datatable that has the option to edit/remove a row.
When i have edited a row, the modal from Datatables sends a request to my servlet(java) which looks like this:

action: edit
data[34][office]: qsdfqsd
data[34][username]: wqsdfqsdf
data[34][password]: QRSHy7

Now I want to get the values of this request which are:

id=34
office="qsdfqsd"
username="wqsdfqsdf"
password="wqsdfqsdf"

So I can put these values in the databank using java(Servlet).

Is there anyone who can help?

Kind Regards

Answers

  • rf1234rf1234 Posts: 2,956Questions: 87Answers: 416

    Well I don't know any Java so I can't tell you how to get those values into Java. But in Java Script it is fairly easy.

    You could use the 'postSubmit' event to get hold of those values like this:

    Editor
          .on( 'postSubmit', function (e, json, data, action, xhr) {
               var keyArr = Object.keys(data.data);
               var id = keyArr[0];
               var office = data.data.id.office;
               var username = data.data.id.username;
               var password = data.data.id.password;
            })
    

    Something similar should work in Java too. It is just object notation.

  • allanallan Posts: 63,332Questions: 1Answers: 10,436 Site admin
    edited March 2018

    You can modify the data that is being submitted to the server using the ajax.data option as a function. There are examples on that page which show how you can effectively flatten the object.

    Note that if you do flatten it like that, you loose the ability to do multi-row editing.

    Allan

  • tomspleesterstomspleesters Posts: 3Questions: 2Answers: 0

    Ye, so I looked into ajax.data and I'm going for the "Add data to the request by manipulating the data object" method but now I still can't find a way to access the in-form input id's to send it with the request.
    I gave my fields an ID and now I tried to do this:

    editor = new $.fn.dataTable.Editor( {
    "ajax": {
    url: "GetNotaryServlet",
    dataSrc: 'data',
    data: function ( d ) {
    d.office = $('#office').val();
    }
    },
    "table": "#notaryTable",
    "idSrc": 'id',
    "fields": [ {
    label: "Kantoor:",
    name: "office",
    id: "office"
    }, {
    label: "Username:",
    name: "username"
    }, {
    label: "Password:",
    name: "password"
    }
    ]
    } );

    Unfortunately, it gives me an empty value "office = "
    Is there something I'm doing wrong?

  • allanallan Posts: 63,332Questions: 1Answers: 10,436 Site admin
    data: function ( d ) {
      $.each( d.data, function ( key, values ) {
        d.id = key;
        d.field1 = values.field1;
        ...
      } );
    

    Allan

This discussion has been closed.