有办法连接两个不同表中添加的行吗?

huangapple go评论47阅读模式
英文:

is there way to connect the two added tr in different table?

问题

以下是代码的部分翻译:

// 定义变量
var $item = $('input[name="Item"]');
var item = $("input[name^='Item']");
var $quantity = $('input[name="Quantity"]');
var $desc = $('input[name="Description"]');
var $item2 = $('input[name="Item2"]');
var item2 = $("input[name^='Item2']");
var $qtyent = $('input[name="Qtyent"]');
var $qtyext = $('input[name="Qtyext"]');
var $balqty = $('input[name="Balanceqty"]');
var $DOT = $('input[name="DatenTime"]');
var $input = $('[name="Item2"],[name="Qtyent"],[name="Qtyext"],[name="Balanceqty"],[name="Guard"],[name="DatenTime"]');
var itemarray, item2array;
var seconditemarray;

// 监听第一个表格中的输入变化
$(document).on('change', "tbody.tbody td input.firsttable", function() {
  itemarray = [];
  item2array = [];

  $(".tbody tr").each(function(i, v) {
    itemarray[i] = $(this).find('input.firsttable').get().map(function(input) {
      $item2.val($item.val());
      $balqty.val($quantity.val());
      $qtyent.val($quantity.val());
      return input.value;
    });
  });

  $(".tablebody tr").each(function(j, k) {
    item2array[j] = $(this).find('input.secondtable').get().map(function(input) {
      return input.value;
    });
  });

  console.log(itemarray, item2array);
});

// 监听添加按钮点击事件
$(document).on('click', '.item-add-onclick', function() {
  var cols = "";
  var col = "";

  cols += `<tr><td>
    <input type="text" name="Item" class="firsttable" value required>
  </td>
  <td>
    <input type="number" name="Quantity" class="firsttable" value required>
  </td>
  <td>
    <input type="text" name="Description" class="firsttable" value required>
  </td></tr>`;
  $('.tbody').append(cols);

  col += `<tr class="tablerow">
     <td>
         <input type="text" name="Item2" class="secondtable" id="item2" value disabled>
     </td>
     <td>
         <input type="number" name="Qtyent" class="secondtable" id="qtyent" value disabled>
     </td>
     <td>
         <input type="text" name="Qtyext" class="secondtable" id="qtyext" value required>
     </td>
     <td>
         <input type="number" name="Balanceqty" class="secondtable" id="blnqty" min="0" value readonly>
     </td>
     <td>
         <input type="text" name="Guard" class="secondtable" id="GoD" value required>
     </td>
     <td>
         <input type="datetime-local" name="DatenTime" class="secondtable" id="DoT" value required>
     </td>
 </tr>`;

  $('.tablebody').append(col);
});

这是代码的一部分翻译,包括变量定义以及事件监听的部分。

英文:

The problem of the code is I don't know a way to make the added tr same as the original tr that insert the value of the ITEM and QUANTITY into another table using the val() method.

the first table is the primary table where I inputted the value, and the second table is the secondary table were getting some of the value on the first table.
sample
firsttable
item quantity description
| apple | 1 | Sweet |
| orange | 1 | Sweet |
second table
|--item--|Quantity ety | Quantity ext | Balance qty | guard | Datetime
|--apple--|------1------|---------------|--------1----|--------|-----------|
|--orange--|------1------|---------------|------1------|--------|-----------|
i want this program like this
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var $item = $(&#39;input[name =&quot;Item&quot;]&#39;)
var item = $(&quot;input[name^=&#39;Item&#39;]&quot;)
var $quantity = $(&#39;input[name =&quot;Quantity&quot;]&#39;)
var $desc = $(&#39;input[name =&quot;Description&quot;]&#39;)
var $item2 = $(&#39;input[name =&quot;Item2&quot;]&#39;)
var item2 = $(&quot;input[name^=&#39;Item2&#39;]&quot;)
var $qtyent = $(&#39;input[name =&quot;Qtyent&quot;]&#39;)
var $qtyext = $(&#39;input[name =&quot;Qtyext&quot;]&#39;)
var $balqty = $(&#39;input[name =&quot;Balanceqty&quot;]&#39;)
var $DOT = $(&#39;input[name =&quot;DatenTime&quot;]&#39;)
var $input = $(&#39;[name =&quot;Item2&quot;],[name =&quot;Qtyent&quot;],[name =&quot;Qtyext&quot;],[name =&quot;Balanceqty&quot;],[name=&quot;Guard&quot;],[name=&quot;DatenTime&quot;]&#39;)
var itemarray, item2array;
var seconditemarray;
$(document).on(&#39;change&#39;, &quot;tbody.tbody td input.firsttable&quot;, function() {
itemarray = [];
item2array = [];
$(&quot;.tbody tr&quot;).each(function(i, v) {
itemarray[i] = $(this).find(&#39;input.firsttable&#39;).get()
.map(function(input) {
$item2.val($item.val())
$balqty.val($quantity.val())
$qtyent.val($quantity.val())
return input.value
});
});
$(&quot;.tablebody tr&quot;).each(function(j, k) {
item2array[j] = $(this).find(&#39;input.secondtable&#39;).get()
.map(function(input) {
return input.value
});
});
console.log(itemarray, item2array)
})
$(document).on(&#39;click&#39;, &#39;.item-add-onclick&#39;, function() {
var cols = &quot;&quot;;
var col = &quot;&quot;;
cols += `&lt;tr&gt;&lt;td&gt;
&lt;input type=&quot;text&quot; name=&quot;Item&quot; class=&quot;firsttable&quot; value required&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;number&quot; name=&quot;Quantity&quot; class=&quot;firsttable&quot; value required&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;text&quot; name=&quot;Description&quot; class=&quot;firsttable&quot; value required&gt;
&lt;/td&gt;&lt;/tr&gt;`;
$(&#39;.tbody&#39;).append(cols);
col += `&lt;tr class=&quot;tablerow&quot;&gt;
&lt;td&gt;
&lt;input type=&quot;text&quot; name=&quot;Item2&quot; class=&quot;secondtable&quot; id=&quot;item2&quot; value disabled&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;number&quot; name=&quot;Qtyent&quot; class=&quot;secondtable&quot; id=&quot;qtyent&quot; value disabled&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;text&quot; name=&quot;Qtyext&quot; class=&quot;secondtable&quot; id=&quot;qtyext&quot; value required&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;number&quot; name=&quot;Balanceqty&quot; class=&quot;secondtable&quot; id=&quot;blnqty&quot; min=&quot;0&quot; value  readonly&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;text&quot; name=&quot;Guard&quot; class=&quot;secondtable&quot; id=&quot;GoD&quot; value required&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;datetime-local&quot; name=&quot;DatenTime&quot; class=&quot;secondtable&quot; id=&quot;DoT&quot; value required&gt;
&lt;/td&gt;
&lt;/tr&gt;`
$(&#39;.tablebody&#39;).append(col)
})

<!-- language: lang-html -->

&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot;&gt;
&lt;script src=&quot;	https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js&quot; integrity=&quot;sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;td&gt;Item&lt;/td&gt;
&lt;td&gt;Quantity&lt;/td&gt;
&lt;td&gt;Description&lt;/td&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody class=&quot;tbody&quot;&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type=&quot;text&quot; name=&quot;Item&quot; class=&quot;firsttable&quot; id=&quot;item&quot; value required&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;number&quot; name=&quot;Quantity&quot; class=&quot;firsttable&quot; id=&quot;quantity&quot; value required&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;text&quot; name=&quot;Description&quot; class=&quot;firsttable&quot; id=desc value required&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;tfoot&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;button class=&quot;btn btn-primary item-add-onclick&quot; id=&quot;addrow&quot;&gt;Add&lt;/button&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tfoot&gt;
&lt;/table&gt;
&lt;Table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;td&gt;Item&lt;/td&gt;
&lt;td&gt;Qty Enter&lt;/td&gt;
&lt;td&gt;Qty Exit&lt;/td&gt;
&lt;td&gt;Balance Qty&lt;/td&gt;
&lt;td&gt;Guard On Duty&lt;/td&gt;
&lt;td&gt;Date and Time&lt;/td&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody class=&quot;tablebody&quot;&gt;
&lt;tr class=&quot;tablerow&quot;&gt;
&lt;td&gt;
&lt;input type=&quot;text&quot; name=&quot;Item2&quot; class=&quot;secondtable&quot; id=&quot;item2&quot; value disabled&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;number&quot; name=&quot;Qtyent&quot; class=&quot;secondtable&quot; id=&quot;qtyent&quot; value disabled&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;text&quot; name=&quot;Qtyext&quot; class=&quot;secondtable&quot; id=&quot;qtyext&quot; value required&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;number&quot; name=&quot;Balanceqty&quot; class=&quot;secondtable&quot; id=&quot;blnqty&quot; min=&quot;0&quot; value readonly&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;text&quot; name=&quot;Guard&quot; class=&quot;secondtable&quot; id=&quot;GoD&quot; value required&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;datetime-local&quot; name=&quot;DatenTime&quot; class=&quot;secondtable&quot; id=&quot;DoT&quot; value required&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/Table&gt;
&lt;/body&gt;
&lt;script src=&quot;table.js&quot;&gt;
&lt;/script&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 0

首先,我想使用一个模板库,以避免在我们的JavaScript中包含所有HTML标记。我将选择Handlebars

我们将创建两个Handlebars模板:一个用于每个模板的行:

<script id="Table1RowTemplate" type="text/template">
  <tr>
    {{!-- Table 1 Row markup goes here --}}
  </tr>
</script>

<script id="Table2RowTemplate" type="text/template">
  <tr>
    {{!-- Table 2 Row markup goes here --}}
  </tr>
</script>

在Table2模板中,我们将在输入元素上使用属性[data-ref],以便我们知道它与Table1中的哪个输入元素相连。例如,在Table1中,input[name="Item"]将链接到Table2中的input[data-ref="Item"]

现在我们有了这些模板,我们将从我们的表格中移除标记。我们将使用JavaScript在页面加载时填充这些表格,使用我们刚刚定义的模板。

两个表格中的<tbody></tbody>元素现在将是空的。

我们还将为每个表格分配一个ID以便于引用。我们将使用"Table1"和"Table2"。

在我们的JavaScript中,我们将定义一个函数来将行添加到我们的表格:

const table1RowTemplate = Handlebars.compile(document.getElementById('Table1RowTemplate').innerHTML);

const table2RowTemplate = Handlebars.compile(document.getElementById('Table2RowTemplate').innerHTML);

const appendRows = () => {
  $('#Table1 tbody').append(table1RowTemplate());
  $('#Table2 tbody').append(table2RowTemplate());
};

我们可以调用这个函数来用它们的第一行填充我们的表格:appendRows();

我们还将使用这个函数作为.item-add-onclick按钮的点击处理程序,以便点击按钮将向每个表格添加一行:

$('.item-add-onclick').on('click', appendRows);

最后是change事件处理程序:

// 监听#Table1中输入的更改
$('#Table1').on('change', 'input', function () {
  // 获取发生更改的输入
  const $this = $(this);
  // 获取输入的名称
  const name = $this.attr('name');
  // 获取输入的新值
  const value = $this.val();
  // 获取输入的行索引
  const index = $('#Table1').find(`input[name="${name}"]`).index($this);
  // 获取#Table2中与索引对应的行
  const row = $('#Table2 tbody tr').get(index);
  
  // 我们使用属性`data-ref`将Table2中的输入与Table1中的输入链接在一起 - Table1中的`[name]`对应于Table2中的`[data-ref]`
  // 因此,我们可以轻松地找到配对并更新其值
  $(row).find(`input[data-ref="${name}"]`).val(value);
});

这里是一个fiddle供参考。

英文:

First, I would like to use a templating library so as to avoid including all of that HTML markup in our JavaScript. I will go with Handlebars.

We will create two Handlebars templates: a row for each of our templates:

&lt;script id=&quot;Table1RowTemplate&quot; type=&quot;text/template&quot;&gt;
&lt;tr&gt;
{{!-- Table 1 Row markup goes here --}}
&lt;/tr&gt;
&lt;/script&gt;
&lt;script id=&quot;Table2RowTemplate&quot; type=&quot;text/template&quot;&gt;
&lt;tr&gt;
{{!-- Table 2 Row markup goes here --}}
&lt;/tr&gt;
&lt;/script&gt;

In the Table2 template, we will use an attribute, [data-ref], on the input so that we know which input in Table1 it is linked to. For example input[name=&quot;Item&quot;] in Table1 will be linked to input[data-ref=&quot;Item&quot;] in Table2.

Now that we have these templates, we will remove the markup from our tables. We will use JavaScript to populate these tables on page load, using the templates we just defined.

The &lt;tbody&gt;&lt;/tbody&gt; elements in both tables will now be empty.

We will also give each table an ID for easy reference. We will go with "Table1" and "Table2".

In our JavaScript, we will define a function to append rows to our tables:

const table1RowTemplate = Handlebars.compile(document.getElementById(&#39;Table1RowTemplate&#39;).innerHTML);
const table2RowTemplate = Handlebars.compile(document.getElementById(&#39;Table2RowTemplate&#39;).innerHTML);
const appendRows = () =&gt; {
$(&#39;#Table1 tbody&#39;).append(table1RowTemplate());
$(&#39;#Table2 tbody&#39;).append(table2RowTemplate());
};

And we can invoke this function to populate our tables with their first rows: appendRows();.

We will also use this function as the handler for a click of our .item-add-onclick button, so that clicking the button will add a row to each table:

$(&#39;.item-add-onclick&#39;).on(&#39;click&#39;, appendRows);

And finally the change handler:

// listen to input changes in #Table1
$(&#39;#Table1&#39;).on(&#39;change&#39;, &#39;input&#39;, function () {
// get the input that was changed
const $this = $(this);
// get the name of the input
const name = $this.attr(&#39;name&#39;);
// get the new value of the input
const value = $this.val();
// get the index of the row of the input
const index = $(&#39;#Table1&#39;).find(`input[name=&quot;${name}&quot;]`).index($this);
// get the corresponding row in #Table2 with the index
const row = $(&#39;#Table2 tbody tr&#39;).get(index);
// we have used an attribute, `data-ref`, to link the inputs in
// Table2 to their partners in Table1 - the `[name]` in Table1
// corresponds to the `[data-ref]` in Table2
// so we can easily find the partner and update its value
$(row).find(`input[data-ref=&quot;${name}&quot;]`).val(value);
});

And here is a fiddle for reference.

huangapple
  • 本文由 发表于 2023年2月16日 08:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定