How can I merge a serialized form with aoData?

How can I merge a serialized form with aoData?

ak47ak47 Posts: 16Questions: 5Answers: 2
edited March 2011 in General
I am trying to use DataTables with AJAX and the fnServerData option in Rails. However, I can't do a GET as each request is actually posting a serialized form from the page. It's no problem to do the POST with the fnServerData option though. However, I run into a problem trying to figure out how to merge aoData, which appears to be an array of objects, and my call to:

[code]$("#attributeform").serialize()[/code]

Is there some way I can put these together so that I can submit them to my controller?

FYI I'm doing this in Rails without the RailsTable method.

Replies

  • ak47ak47 Posts: 16Questions: 5Answers: 2
    Allan, I want to pay you for your fine work and any help you can offer, but I went about it backwards. I posted my question here, then donated through the Donate button above. You should see that come through shortly.
  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    Hi ak47,

    Thanks very much for the donation :-). What I think you want to do in this case is use $.serializeArray ( http://api.jquery.com/serializeArray/ ), rather than just $.serialize. This will give you the name/value pairs needed for the jQuery Ajax. Something like this:

    [code]
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    aoData.concat( $("#attributeform").serializeArray() );
    $.ajax( {
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": fnCallback
    } );
    }
    [/code]
    That will combine the aoData array from DataTables with the serliazed form.

    Regards,
    Allan
  • ak47ak47 Posts: 16Questions: 5Answers: 2
    Hey, that looks like a winner. Thanks Allan.
This discussion has been closed.