i want to show my table made of unknown rows and 5 columns in a modal. i’ve tried many ways explained below after the code:
The start button:
<button class="btn btn-warning" onclick="EditHistory('@item.OriginalCode')">Start</button>
JS:
function EditHistory(OriginalCode)
{
var postData = {
'SearchKey': OriginalCode,
}
$.ajax({
contentType: 'application/x-www-form-urlencoded',
dataType: 'json',
type: "POST",
url: "Index2",
data: postData,
success: function (data)
{
if (data.isSuccess == true)
{
const b = [];
const c = [];
const d = [];
const e = [];
const f = [];
for (let i=0; i<data.data["countHistory"]; i++) //countHistory is number of rows in my table retrieved from DB.
{
const a = data.data["aPsHistory"][i]; //aPsHistory is a list in a dictionary sent by JSON. it's my table retrieved from DB
b[i] = a["originalCode"];
c[i] = a["currentCode"];
d[i] = a["changes"];
e[i] = a["editTime"];
f[i] = a["userId"];
document.getElementById("OriginalCode").innerHTML = b[i];
document.getElementById("CurrentCode").innerHTML = c[i];
document.getElementById("Changes").innerHTML = d[i];
document.getElementById("EditTime").innerHTML = e[i];
document.getElementById("UserId").innerHTML = f[i];
$('#EditHistory').modal('show');
}
else
{
swal.fire(
'هشدار!',
data.message,
'warning'
);
}
},
error: function (request, status, error)
{
alert(request.responseText);
}
});
}
The Modal:
<div class="modal fade" id="EditHistory" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Discription</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="col-xl-12 col-lg-12 col-md-12 mb-1">
<table class="table table-striped table-bordered zero-configuration dataTable" id = "DataTables_Table_0" role = "grid" aria - describedby="DataTables_Table_0_info" >
<thead style="font-size:small;" >
<tr role="row" >
<th class="sorting" tabindex = "0" aria - controls="DataTables_Table_0" rowspan = "1" colspan = "1" aria - label="کد انحصاری ابتدایی: activate to sort column ascending" style = "width: 401px;" > کد انحصاری ابتدایی </th>
<th class="sorting" tabindex = "0" aria - controls="DataTables_Table_0" rowspan = "1" colspan = "1" aria - label="کد انحصاری کنونی: activate to sort column ascending" style = "width: 401px;" > کد انحصاری کنونی </th>
<th class="sorting" tabindex = "0" aria - controls="DataTables_Table_0" rowspan = "1" colspan = "1" aria - label="تغییرات: activate to sort column ascending" style = "width: 401px;" > تغییرات </th>
<th class="sorting" tabindex = "0" aria - controls="DataTables_Table_0" rowspan = "1" colspan = "1" aria - label="تاریخ ویرایش: activate to sort column ascending" style = "width: 401px;" > تاریخ ویرایش </th>
<th class="sorting" tabindex = "0" aria - controls="DataTables_Table_0" rowspan = "1" colspan = "1" aria - label="کاربر ویرایش کننده: activate to sort column ascending" style = "width: 401px;" > کاربر ویرایش کننده </th>
</tr>
</thead>
<tbody style = "font-size:smaller;" >
<tr role="row" class="odd" >
<td id="OriginalCode" > </td>
< td id = "CurrentCode" > </td>
< td id = "Changes" > </td>
< td id = "EditTime" > </td>
< td id = "UserId" > </td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="modal-footer">
<a class="btn btn-secondary" data-dismiss="modal">close</a>
</div>
</div>
</div>
</div>
I wish if i could sent JSON data to a global variable defined in C#, then write a @foreach on it in HTML and see the result in modal. but in JS i cannot see C# variables to put the JSON data in it (at least i tried).
OR
if i could put the JSON data to a global JS variable, but this variable cannot be seen in HTML! remember that i want to run a @foreach loop on it in HTML, so if i use getElementById method, i cannot use it in loop or any condition. or maybe idk how to do this? for example is something like this possible?:
@foreach (var item in HTML Tag Attribute (Id))
i want something like this. i ran the code above and it worked fine. but the problem is that my code is ok for only 1 row of 5 column data. if i want to put a for loop in HTML modal to add more rows to the table, idk how to put the counter? up to what number my for loop should count? because my table rows count is unknown. i can get the count in JSON (countHistory) but idk how should i send it to the HTML to put it as the counter in for loop.
or maybe there is another better way to show JSON data in modal. please help me. thanks