简体   繁体   English

更新多个条目表的特定行

[英]Update specific row of table of multiple entries

I have a table of customers. 我有一张顾客桌子。 Each customer has a first and a last name. 每个客户都有名字和姓氏。 The two text fields of the table are editable. 该表的两个文本字段是可编辑的。 So users can update the information when they press Save. 因此,用户可以在按保存时更新信息。 The problem is that I cannot get the specific row information,I only get the first row results 问题是我无法获取特定的行信息,只能获取第一行结果

I tried to match to the names with the input field but I had no success. 我尝试将名称与输入字段匹配,但没有成功。

<?php foreach($customer as $each){ ?>
    <td class="first_name" id="first" contenteditable="true"><?php echo 
    $each['first_name']; ?></td>
    <td class="last_name" id="last"  contenteditable="true"><?php echo 
    $each['last_name']; ?></td>
    <td >   <button type="button" onclick="save('<?php echo $each['first_name'];? 
    >','<?php echo $each['last_name'];?>');" >Save</button></td>
    <? } ?>

<script type="text/javascript">
    function save(first,second) {
      <?php foreach($customer as $each){?>
        var first_name = "<?php echo $each['first_name']?>";
        var last_name = "<?php echo $each['last_name']?>";
        if (first_name==first && last_name == second){
          var fname = document.querySelectorAll(".first_name");
          console.log(fname[0]);
        }
        <?php } ?>

      }
 </script>

You would have to use a different query selector. 您将不得不使用其他查询选择器。 Assign a classname or an attribute to the elements you want to select (eg name for querying with .name ) then querySelectorAll method will return an array of the elements that matched your query. 为要选择的元素分配一个类名或属性(例如,使用.name进行查询的name ),然后querySelectorAll方法将返回与查询匹配的元素数组。

The main problem that I see is that you create unnecessary foreach loop inside a javascript function using php . 我看到的主要问题是,您使用phpjavascript函数内创建了不必要的foreach循环。

You can dynamically create your table and the contents inside it, that's fine. 您可以动态创建表及其中的内容,这很好。 But the javascript does not care about that and you should not create javascript with php. 但是javascript并不关心这一点,因此您不应该使用php创建javascript。 So I would do it this way. 所以我会这样做。

I wrap the td's in tr's cause i'm assuming you are putting that data in tr. 我将td封装在tr的原因中,我假设您将这些数据放入tr中。

<?php foreach($customer as $each){ ?>
  <tr class="customer-row">
    <td class="first_name" contenteditable="true"><?php echo 
    $each['first_name']; ?></td>
    <td class="last_name" contenteditable="true"><?php echo 
    $each['last_name']; ?></td>
    <td><button type="button" class="save">Save</button></td>
  </tr>
<? } ?>

Then outside the php foreach loop i would create my script. 然后在php foreach循环之外,我将创建我的脚本。

<script type="text/javascript">
var saveBtn = document.querySelectorAll(".save");

for(var i = 0; i < saveBtn.length; i++) {
  // attach click event to every button.
  saveBtn[i].addEventListener("click", function() {
    var _this = this;
    var _tr = _this.closest(".customer-row");
    var fname = _tr.querySelector(".first_name").innerText;
    var lname = _tr.querySelector(".last_name").innerText;
    console.log("Name: ", fname + " " + lname");
    // below you can implement your check name logic...
  }
}
</script>

I'm not 100% sure if my js will not throw errors, but it should give you an indication that you should separate your server-side from client-side logic. 我不确定我的js是否不会引发错误,但不能100%地确定您应该将server-side client-side逻辑与client-side逻辑分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM