can anyone explain me how can I build a string out of the number of values inside an array with their elements.
For example right now I have this table, it pulls data out of the table if the we type a value inside the txtbox, anyway, this builds an array of data of the rows
function setValueAttr(el) {
el.setAttribute('value', el.value)
}
function aplicar() {
var myTab = document.querySelectorAll('#tableID tbody tr .txtID:not([value=""])');
var tableData = [];
Array.from(myTab).forEach(input => {
var tds = input.closest('tr').children;
var obj = {};
obj.A = tds[0].textContent;
obj.B = tds[1].textContent;
obj.C = tds[2].textContent;
obj.D = tds[3].textContent;
obj.E = input.value;
tableData.push(obj);
});
console.log(tableData);
}
<table class="table table-bordered" width="100%" cellspacing="0" id="tableID">
<thead>
<tr>
<th>num</th>
<th>mon</th>
<th>doc</th>
<th>type</th>
<th>val</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">val1</td>
<td align="center">val2</td>
<td align="center">val3</td>
<td align="center">1500</td>
<td align="center" class="myID"><input type="number" name="txtID" class="txtID" oninput="setValueAttr(this)" value="" min="0" max="1000"></td>
</tr>
<tr>
<td align="center">val1</td>
<td align="center">val2</td>
<td align="center">val3</td>
<td align="center">1500</td>
<td align="center" class="myID"><input type="number" name="txtID" class="txtID" oninput="setValueAttr(this)" value="" min="0" max="1000"></td>
</tr>
<tr>
<td align="center">val1</td>
<td align="center">val2</td>
<td align="center">val3</td>
<td align="center">1500</td>
<td align="center" class="myID"><input type="number" name="txtID" class="txtID" oninput="setValueAttr(this)" value="" min="0" max="1000"></td>
</tr>
</tbody>
</table>
<form>
<button type="button" onclick="aplicar()">Aplicar</button>
</form>
I need to create a string out of this data like this
total[]=&num1={}&mon1={}&doc1={}&type1={}&val1={}&num2={}&mon2={}&doc2={}&type2={}&val2={}
Where
total = *n* the number of elements inside tableData[]
And
numN monN typeN valN = the values inside an element of tableData[]
Used that structure because of the output of the html example
[
{
"A": "val1",
"B": "val2",
"C": "val3",
"D": "1500",
"E": "1000"
}
]
Currently using this to post into Controller
<script>
function setValueAttr(el) {
el.setAttribute('value', el.value)
}
function aplicar() {
var myTab = document.querySelectorAll('#DocumentosAbiertos tbody tr .txtID:not([value=""]):valid');
var tableData = [];
Array.from(myTab).forEach(input => {
var tds = input.closest('tr').children;
var obj = {};
obj.A = tds[8].textContent;
obj.B = tds[1].textContent;
obj.C = tds[6].textContent;
obj.D = tds[5].textContent;
obj.E = input.value;
tableData.push(obj);
});
console.log(tableData);
data = JSON.stringify(tableData)
$.ajax({
url: '~/Controller/myView',
type: 'post',
contentType: 'application/json',
data: data,
success: function (response) {
console.info(response);
},
error: function (error) {
console.error(error);
}
});
}
</script>
Therefor my Controller looks like this.
[HttpPost]
public JsonResult myView(IList<ModelPagosRecibidos> models)
{
comercial.Models.ModelPagosRecibidos modelPagosRecibidos = new Models.ModelPagosRecibidos();
return Json(modelPagosRecibidos);
}
And this is my model
public class ModelPagosRecibidos
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public int D { get; set; }
public string E { get; set; }
}
Where it hits on this dataset, which is the whole point of the question, how to distribute the data inside this dataset string I build with the values of the array I post using AJAX dynamically.
public System.Data.DataSet linkDoc()
{
string operacion = A;
string metodo = B;
string monto = C;
string fecha = D;
string fecha = E;
string sURL = ("total_registros=" + operacion + "&num_pago1=" + metodo + "&monto1=" + monto + "&num_documento1=" + fecha + "&tipo_documento1=" + E + "&");
DataSet ds = utilities.get(sURL);
return ds;
}