I have a table in my project, I loop the trs in this table. There are textboxes in the table. When I make changes and want to save, it takes action based on the first record. I share the image
The second record in the picture above "toptan" I changed it to. but the record that came to me "Perakendee"
So my code always gets the value of the first record. Is there a way to solve this? Or a more Logical coding
HTML codes
<table id="kullanicilistesi" class="table mb-3 table-center">
<thead>
<tr>
<th class="border-bottom text-start py-3">Şirket Adı</th>
<th class="border-bottom text-start py-3">Database (Veri Tabanı)</th>
<th class="border-bottom text-center py-3">Şube</th>
<th class="border-bottom text-center py-3">Diğer</th>
</tr>
</thead>
<tbody>
@foreach (var item in AdminStatic.GetIsletme()) {
<tr>
<td class="">
<input type="text" id="isletmeadi" class="form-control" value="@item.IsletmeAdi" />
</td>
<td class=""> <input type="text" id="veritabani" class="form-control" value="@item.VeriTabani" /></td>
<td class=""> <input type="text" id="sube" class="form-control" value="@item.Sube" /></td>
<td class="">
<a style="color:limegreen;" href`your text`="javascript:void(0)" data-idd="@item.ID" onclick="IsletmeDuzenle(this)">
<i class="uil uil-check"></i>
</a>
<a style="color: rgb(217, 37, 37); " href="javascript:void(0)" onclick="IsletmeSil(@item.ID)">
<i class="uil uil-trash"></i>
</a>
</td>
</tr>
}
</tbody>
</table>
Javascript Codes
function IsletmeDuzenle(t) {
var id = $(t).data('idd');
alert($("#isletmeadi").val());
getValue($baseUrl + "/B2BAdmin/IsletmeDuzenle",
{ id: id, isletmeAdi: $("#isletmeadi").val(), veritabani: $("#veritabani").val(), sube: $("#sube").val() },
function (data, err) {
if (data=="Basarili") {
Swal.fire({
title: 'Başarılı',
html: 'İşletme Başarıyla Güncellenmiştir',
icon: 'success'
}).then(function (result) {
window.location.reload();
});
}
})
}
2
Answers
I’m not good with turkish but it looks like trying to update or delete rows.
On checkmark: onclick="IsletmeDuzenle(this)"
On trashcan: onclick="IsletmeSil(@item.ID)"
I’m guessing its the checkmark that is not behaving as you want it to. If it’s for an update, you could pass the ID like with the trashcan.
If all the rows are imported and only need to change and/or get deleted, I think a function that does that that takes the ID of the entry is the best way. But then you need to have the ID in that object, even if you don’t show it.
So,
On checkmark: onclick="somefunction(@item.ID)"
Just to illustrate you can do this without id’s and have multiple I did a fake mock-up and some ugly colors with some event handlers NOT embedded in the HTML.
I also removed empty
class=""
just to have a cleaner visual of the HTML.