1

I am using Datatables. I have a table with multiple inputs and I want to post array of objects to controller via ajax.

var data = table.$('input, select').serialize();

The result:

row-1-location=123&row-1-lot=231545&row-2-location=2323&row-2-lot=5523&row-3-location=23232&row-3-lot=23235

I am assuming I need to split the string at every second '&', then split again. The question is, is this the only way to convert it to array of objects?

The result I want is array of objects:

[{location : 123, lot: 231545}, {location: 2323, lot: 5523}......]

HTML:

<tbody>
    <tr role="row" class="odd">
        <td><input type="text" id="row-5-location" name="row-5-location" value=""></td>
        <td><input type="text" id="row-5-lot" name="row-5-lot" value=""></td>
    </tr>
    <tr role="row" class="even">
        <td><input type="text" id="row-6-location" name="row-5-location" value=""></td>
        <td><input type="text" id="row-6-lot" name="row-5-lot" value=""></td>
    </tr>
</tbody>

Thanks!

5
  • In this: post array of objects to controller. do you mean POST as in "to controller on server via HTTP"? If so, then data simply becomes the BODY of your POST request. Commented May 29, 2019 at 15:00
  • I want to post to function in mvc via ajax Commented May 29, 2019 at 15:02
  • It would make more sense to just build the array of objects directly from the DOM elements. If you can show us what the HTML looks like I'm sure we can give you an example of how to do that Commented May 29, 2019 at 15:04
  • Updated with html code. Commented May 29, 2019 at 15:10
  • Thanks, I added an answer below Commented May 29, 2019 at 15:15

1 Answer 1

1

To create the array of objects you require it would make more sense to just build the structure from the DOM directly instead of serialising it, then picking apart the resulting string.

To achieve this you can select the parent tr elements then use map() to build the objects. The only HTML change which would make it simpler still would be to put common classes on the input elements. Something like this:

var arr = $('table tr').map(function() {
  var $tr = $(this);
  return {
    location: $tr.find('.location').val(),
    lot: $tr.find('.lot').val()
  }
}).get();

console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr role="row" class="odd">
      <td><input type="text" class="location" id="row-5-location" name="row-5-location" value="123"></td>
      <td><input type="text" class="lot" id="row-5-lot" name="row-5-lot" value="231545"></td>
    </tr>
    <tr role="row" class="even">
      <td><input type="text" class="location" id="row-6-location" name="row-5-location" value="2323"></td>
      <td><input type="text" class="lot" id="row-6-lot" name="row-5-lot" value="5523"></td>
    </tr>
  </tbody>
</table>

Sign up to request clarification or add additional context in comments.

2 Comments

There is one drawback in this approach. We can only get data from rows that presented in DOM. But if I use searchbox then I will not be able to get previously entered data.
Potentially, but using serialize() has the same drawback as that is only populated from the DOM too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.