Bootstrap 3 modal table contains rows with input, textarea and select elements. All rows have same columns with same elements. Elements in every row have same names.
Table data should be send using jquery ajax call using button click. I tried
$.ajax("api/Raport",
{
contentType: "application/json",
data: JSON.stringify({
elements: { param1: 1, param2: 2} ,
variable: $("#reportVariablesTable").serializeArray()
}),
type: 'POST'
});
but variable property is empty array.
How to serialize table column values to variable property like:
[
{ name: "variable1", valuetostore: "a-b", totaltype: "Lowest" },
...
]
and if possible not submit hidden row (may be not first row in table)
This is ASP .NET MVC4 application. Html code can re-factored if this helps.
View:
<div class="modal" id="reportVariables" tabindex="-1" role="dialog" aria-labelledby="reportVariablesLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-body">
<table class="table table-condensed table-striped" id="reportVariablesTable">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Calculate</th>
</tr>
</thead>
<tbody>
<!-- table contains one hidden rows which should not -->
<tr style='display:none'>
<td>
<input type="text" name="name">
</td>
<td>
<textarea name="valuetostore"></textarea>
</td>
<td>
<select class="totaltype-select" id="totaltype" name="totaltype">
<option value=""></option>
<option value="Sum">Summary</option>
<option value="Lowest">Smallest</option>
<option value="Highest">Biggers</option>
</select>
</td>
</tr>
<!-- other are data rows which should sent -->
<tr>
<td>
<input type="text" name="name" value="variable1">
</td>
<td>
<textarea name="valuetostore">a-b</textarea>
</td>
<td>
<select class="totaltype-select" id="totaltype" name="totaltype">
<option value=""></option>
<option value="Sum">Summary</option>
<option value="Lowest" selected>Smallest</option>
<option value="Highest">Biggers</option>
</select>
</td>
</tr>
... remaining rows
</tbody>
</table>
</div>
</div>
</div>
</div>
I put table into form and used
variable: $("#reportVariablesForm").serialize(),
it ise serialized into huge arrvy containing name/value objects:
How to fix this so that form is serialized to array containing one element for every row with property names from row element names:
[
{ name: "variable1", valuetostore: "a-b", totaltype: "Lowest" },
...
]
The .serializeArray() method creates a JavaScript array of objects, ready to be encoded as a JSON string. It operates on a jQuery collection of forms and/or form controls.
You need a form
tag in your HTML.
$(function() {
$("#btnShow").on("click", function() {
console.log($("#myForm").serializeArray());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="modal" id="reportVariables" tabindex="-1" role="dialog" aria-labelledby="reportVariablesLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-body">
<form id="myForm">
<table class="table table-condensed table-striped" id="reportVariablesTable">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>Calculate</th>
</tr>
</thead>
<tbody>
<!-- table contains one hidden rows which should not -->
<tr style='display:none'>
<td>
<input type="text" name="name">
</td>
<td>
<textarea name="valuetostore"></textarea>
</td>
<td>
<select class="totaltype-select" id="totaltype" name="totaltype">
<option value=""></option>
<option value="Sum">Summary</option>
<option value="Lowest">Smallest</option>
<option value="Highest">Biggers</option>
</select>
</td>
</tr>
<!-- other are data rows which should sent -->
<tr>
<td>
<input type="text" name="name" value="variable1">
</td>
<td>
<textarea name="valuetostore">a-b</textarea>
</td>
<td>
<select class="totaltype-select" id="totaltype" name="totaltype">
<option value=""></option>
<option value="Sum">Summary</option>
<option value="Lowest" selected>Smallest</option>
<option value="Highest">Biggers</option>
</select>
</td>
</tr>
... remaining rows
</tbody>
</table>
<button id="btnShow" type="button">
Show
</button>
</form>
</div>
</div>
</div>
</div>