skip to Main Content

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

enter image description here

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


  1. 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)"

    Login or Signup to reply.
  2. 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.

    function goGetThem(them) {
      console.log("Called:", them);
      return; // just to not do the call
    
      getValue(them.getUrl, them.callParameters,
        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();
            });
          }
        });
    }
    $('.green-container')
      .on('click', '.go-green-button', function(event) {
        console.log('check clicked');
        const $editRow = $(this).closest('.business-container');
        const $tableContainer = $(event.delegateTarget);
        console.log($editRow.find('.database-name').val());
        const baseurl = $tableContainer.data("baseurl");
        const them = {
          getUrl: baseurl + "/B2BAdmin/IsletmeDuzenle",
          callParameters: {
            id: $(this).data('idd'),
            isletmeAdi: $editRow.find(".isletmeadi").val(),
            veritabani: $editRow.find(".veritabani").val(),
            sube: $editRow.find(".sube").val()
          }
        };
        goGetThem(them);
      }).on('click', ".remove-button", function(event) {
        let mytd = $(this).closest('td');
        mytd.toggleClass("delete-me");
        // for fun we just toggle the data and make it "pretty" on clicks
        $(this).closest('.business-container').get(0).dataset.todelete = mytd.hasClass('delete-me') ? "yes" : "no";
        //console.log('Go remove:', this.innerHTML);
      });
    .remove-button.btn {
      color: #D92525;
    }
    
    .go-green.button {
      color: #32CD32;
    }
    
    .delete-me {
      border: 1px solid red;
    }
    
    .business-container:not([data-todelete]) {
      background-color: #22222204;
    }
    
    .business-container[data-todelete="yes"] {
      background-color: #FF000008;
    }
    
    .business-container[data-todelete="no"] {
      background-color: #00FF0008;
    }
    
    .link-container .uil {
      font-size: 1.5rem;
      border: solid 1px lime;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
    
    <table class="table mb-3 table-center green-container" data-baseurl="/fun/urlforgreen/">
      <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 class="container">
        <tr class="container container-row business-container">
          <td><input type="text" class="form-control business-name isletmeadi" value="Happy business" /></td>
          <td><input type="text" class="form-control database-name veritabani" value="Fun Database" /></td>
          <td><input type="text" class="form-control sube" value="Branch5" /></td>
          <td class="link-container">
            <a class="go-green-button btn" href="#" data-idd="branch-5">
              <i class="uil bi-check"></i>
            </a>
            <a class="remove-button btn" href="#" data-idd="branch-5">
              <i class="uil bi-trash"></i>
            </a>
          </td>
        </tr>
        <tr class="container container-row business-container">
          <td><input type="text" class="form-control business-name isletmeadi" value="Car business" /></td>
          <td><input type="text" class="form-control database-name veritabani" value="Car Database" /></td>
          <td><input type="text" class="form-control sube" value="branch454" /></td>
          <td class="link-container">
            <a class="go-green-button btn" href="#" data-idd="row-454">
              <i class="uil bi-check"></i>
            </a>
            <a class="remove-button btn" href="#" data-idd="row-454">
              <i class="uil bi-trash"></i>
            </a>
          </td>
        </tr>
      </tbody>
    </table>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search