How can I send a value from one table to another table at the same level inside the blade

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

How can I send a value from one table to another table at the same level inside the blade

问题

以下是您要的翻译部分:

I have two tables <table> I want to pass the values that are selected from the first table in the third field as shown in the figure

How can I send a value from one table to another table at the same level inside the blade

note: that there is a button that adds a new input row by jquery on click (Add Cells)

//here first table code in blade

<table class="table table-bordered table_field4" id="table_field4" style="overflow-x: auto;white-space: nowrap;">
    <thead>
        <tr class="thead-dark">
            <th>#</th>
            <th style="width: 250px !important">first</th>
            <th style="width: 250px !important">second</th>
            <th style="width: 200px !important">third</th>
            <th style="width: 200px !important"></th>
        </tr>
    </thead>
    <tbody>
        <tr class="trr">
            <?php $x=1;?>
            <td>1</td>
            <td style="display:none;"><input type="text" name="prod_idd[]" style="display:none;" value="{{$lastid->id}}"></td>
            <td>
                <input type="text" style="width: 250px !important" class="sub_code form-control getAllCode putCode" name="sub_code[]">
                @error('sub_code.*')
                <div class="alert alert-danger" role="alert">
                    {{$message}}
                </div>
                @enderror
            </td>
            <td>
                <input type="text" style="width: 250px !important" class="form-control" name="count_pices[]">
                @error('.*')
                <div class="alert alert-danger" role="alert">
                    {{$message}}
                </div>
                @enderror
            </td>
            <td>
                <select style="width: 150px !important" class="form-control getUnit" name="unit_id[]">
                    @foreach ($units as $unit)
                        <option value="{{$unit->id}}" name="{{$unit->unit_code}}">{{$unit->unit_name . ' ( ' . $unit->unit_code . ' ) '}}</option>
                    @endforeach
                </select>
            </td>
            <td style="width: 200px !important"><input type="button" class="btn btn-success" name="add4" id="add4" value="Add Cells"></td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
        </tr>
    </tfoot>
</table>

// jquery to add new row for first table that have column (third)

$('#add4').click(function(){
    var size = $('#table_field4 tbody').children().length + 1;
    var html = '<tr class="trr"><td>'+size+'</td><td style="display:none;"><input type="text" name="prod_idd[]" style="display:none;" value="{{$lastid->id}}"></td><td><input type="text" class="form-control sub_code putCode" name="sub_code[]"></td><td><input type="text" class="count_pices form-control" name="count_pices[]"></td><td><select style="width: 150px !important" class="form-control getUnit" name="unit_id[]">@foreach ($units as $unit)<option value="{{$unit->id}}" name="{{$unit->unit_code}}">{{$unit->unit_name . ' ( ' . $unit->unit_code . ' ) '}}</option>@endforeach</select></td><td><input type="button" class="btn btn-danger" name="remove" id="remove" value="delete" style="color:black;background-color:red;"></td></tr>';
    $('#table_field4').append(html);
});

$('#table_field4').on('click','#remove',function(){
    $(this).closest('tr').remove();
});

The first part related to the first table has been completed. The following is the second table that is created by calculating the number of rows in the first table and creating equal rows using JQuery.

//here second table

<table class="table table-bordered table_field5" id="table_field5" style="overflow-x: auto;white-space: nowrap;">
    <thead>
        <tr class="thead-dark">
            <th style="width: 100px !important">C</th>
            <th style="width: 100px !important">الكويت</th>
            <th style="width: 100px !important">A</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="text" name=""></td>
            <td><input type="text" name=""></td>
            <td><input type="text" name=""></td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
        </tr>
    </tfoot>
</table>

Here is the jquery code that counts the number of rows in the first table and adds rows equal to it according to the number of columns in the second table.

$('#add4').click(function(){
    var rowCount = $('#table_field4 .trr').length + 1;
    var rowCountone = $('#table_field4 .trr').length;
    var size = $('#table_field4 .trr').length + 1;
    var html = '<tr><td><input type="text" value=""></td><td><input type="text" name="value[]"></td><td><input type="text" name="value[]"></td></tr>';
    $('#table_field5').append(html);
});
英文:

I have two tables <table> I want to pass the values that are selected from the first table in the third field as shown in the figure

How can I send a value from one table to another table at the same level inside the blade

note: that there is a button that adds a new input row by jquery on click (Add Cells)

//here first table code in blade

&lt;table class=&quot;table table-bordered table_field4&quot; id=&quot;table_field4&quot; style =&quot;overflow-x: auto;white-space: nowrap;&quot;&gt;
&lt;thead&gt;
&lt;tr class=&quot;thead-dark&quot;&gt;
&lt;th&gt; #&lt;/th&gt;
&lt;th style=&quot;width: 250px; !important&quot;&gt;first&lt;/th&gt;
&lt;th style=&quot;width: 250px; !important&quot;&gt;second &lt;/th&gt;
&lt;th style=&quot;width: 200px; !important&quot;&gt;third&lt;/th&gt;
&lt;th style=&quot;width: 200px; !important&quot;&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr class=&quot;trr&quot;&gt;
&lt;?php $x=1;?&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td style=&quot;display:none;&quot;&gt;&lt;input type=&quot;text&quot;  name=&quot;prod_idd[]&quot; style=&quot;display:none;&quot; value=&quot;{{$lastid-&gt;id}}&quot;&gt;&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;text&quot; style=&quot;width: 250px; !important&quot; class=&quot;sub_code form-control getAllCode putCode&quot; name=&quot;sub_code[]&quot;&gt;
@error(&#39;sub_code.*&#39;)
&lt;div class=&quot;alert alert-danger&quot; role=&quot;alert&quot;&gt;
{{$message}}
&lt;/div&gt;
@enderror
&lt;/td&gt;
&lt;!-- heeree --&gt;
&lt;td&gt;
&lt;input type=&quot;text&quot; style=&quot;width: 250px; !important&quot; class=&quot; form-control  &quot; name=&quot;count_pices[]&quot;&gt;
@error(&#39;.*&#39;)
&lt;div class=&quot;alert alert-danger&quot; role=&quot;alert&quot;&gt;
{{$message}}
&lt;/div&gt;
@enderror
&lt;/td&gt;
&lt;td&gt;
&lt;select style=&quot;width: 150px; !important&quot;   class=&quot;form-control getUnit&quot;
name=&quot;unit_id[]&quot;&gt;
@foreach ($units as $unit)
&lt;option value=&quot;{{$unit-&gt;id}}&quot; name=&quot;{{$unit-&gt;unit_code}}&quot;&gt;{{$unit-&gt;unit_name . &#39; ( &#39; . $unit-&gt;unit_code . &#39; ) &#39;}}
&lt;/option&gt;
@endforeach
&lt;/select&gt;
&lt;/td&gt;
&lt;td style=&quot;width: 200px; !important&quot;&gt;&lt;input type=&quot;button&quot; class=&quot;btn btn-success&quot; name=&quot;add4&quot; id=&quot;add4&quot; value=&quot;Add Cells&quot; &gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;tfoot&gt;
&lt;tr&gt;
&lt;/tr&gt;
&lt;/tfoot&gt;
&lt;/table&gt;

// jquery to add new row for first table that have column (third)

$(&#39;#add4&#39;).click(function(){
var  size = $(&#39;#table_field4 tbody&#39;).children().length +1;
var html = &#39;&lt;tr class=&quot;trr&quot;&gt;&#39;+&#39;&lt;td&gt;&#39;+size+&#39;&lt;/td&gt;&#39;+&#39;&lt;td style=&quot;display:none;&quot;&gt;&lt;input type=&quot;text&quot;  name=&quot;prod_idd[]&quot; style=&quot;display:none;&quot; value=&quot;{{$lastid-&gt;id}}&quot;&gt;&lt;/td&gt;&lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;form-control sub_code putCode&quot; name=&quot;sub_code[]&quot;&gt;&lt;/td&gt;&lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;count_pices form-control&quot; name=&quot;count_pices[]&quot;&gt;&lt;/td&gt;&lt;td&gt;&lt;select style=&quot;width: 150px; !important&quot;   class=&quot;form-control getUnit&quot; name=&quot;unit_id[]&quot;&gt;@foreach ($units as $unit)&lt;option value=&quot;{{$unit-&gt;id}}&quot; name=&quot;{{$unit-&gt;unit_code}}&quot;&gt;{{$unit-&gt;unit_name . &#39; ( &#39; . $unit-&gt;unit_code . &#39; ) &#39;}}&lt;/option&gt;@endforeach&lt;/select&gt;&lt;/td&gt;&lt;td&gt;&lt;input type=&quot;button&quot; class=&quot;btn btn-danger&quot; name=&quot;remove&quot; id=&quot;remove&quot; value=&quot;delete&quot; style=&quot;color:black;background-color:red;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;&#39;;
$(&#39;#table_field4&#39;).append(html);
// var rowCount = $(&#39;#table_field4 .trr&#39;).length;
// console.log(rowCount);
});
$(&#39;#table_field4&#39;).on(&#39;click&#39;,&#39;#remove&#39;,function(){
$(this).closest(&#39;tr&#39;).remove();
});

The first part related to the first table has been completed
The following is the second table that is created by calculating the number of rows in the first table and creating equal rows using JQuery.

//here second table
How can I send a value from one table to another table at the same level inside the blade

//second table in blade

                                                                    &lt;table class=&quot;table table-bordered table_field5&quot; id=&quot;table_field5&quot; style =&quot;overflow-x: auto;white-space: nowrap;&quot;&gt;
&lt;thead&gt;
&lt;tr class=&quot;thead-dark&quot;&gt;
&lt;th style=&quot;width: 100px; !important&quot;&gt;C&lt;/th&gt;
&lt;th style=&quot;width: 100px; !important&quot;&gt;الكويت&lt;/th&gt;
&lt;th style=&quot;width: 100px; !important&quot;&gt;A&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td &gt;&lt;input type=&quot;text&quot; name=&quot;&quot; &gt;&lt;/td&gt;
&lt;td &gt;&lt;input type=&quot;text&quot; name=&quot;&quot; &gt;&lt;/td&gt;
&lt;td &gt;&lt;input type=&quot;text&quot; name=&quot;&quot; &gt;&lt;/td&gt;                                                                           
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;tfoot&gt;
&lt;tr&gt;
&lt;/tr&gt;
&lt;/tfoot&gt;
&lt;/table&gt;

Here is the jquery code that counts the number of rows in the first table and adds rows equal to it according to the number of columns in the second table

$(&#39;#add4&#39;).click(function(){
var rowCount = $(&#39;#table_field4 .trr&#39;).length + 1;
var rowCountone = $(&#39;#table_field4 .trr&#39;).length ;
var  size = $(&#39;#table_field4 .trr&#39;).length + 1;
var html = &#39;&lt;tr&gt;&#39;+&#39;&lt;td&gt;&lt;input type=&quot;text&quot; value=&quot;&quot; &gt;&lt;/td&gt;&lt;td &gt;&lt;input type=&quot;text&quot; name=&quot;value[]&quot; &gt;&lt;/td&gt;&lt;td &gt;&lt;input type=&quot;text&quot; name=&quot;value[]&quot; &gt;&lt;/td&gt;&lt;/tr&gt;&#39;;
$(&#39;#table_field5&#39;).append(html);
});

答案1

得分: 1

我可以理解你想做的事情,但可能不是完全明白。我会根据我理解的问题来解决。

当用户点击“Add Cells”按钮时,您希望在底部表格添加新的行(这部分您已经做到了),并在底部表格中的先前最后一行中填充“C”列输入的值。这个输入的值应该与顶部表格中最后一行第三列选择菜单中所选择的值相同。

要实现这个,我会按照以下方式实现第二个 #add4 点击处理程序:

$(&#39;#add4&#39;).click(function() {
  // 获取顶部表格行的总数
  const numRows = $(&#39;#table_field4 .trr&#39;).length;
  // 获取先前最后一行的索引
  // (因为刚刚添加了一行,所以需要从 numRows 中减去 2)
  const prevRowIndex = numRows - 2;
  // 获取先前的最后一行
  const prevRow = $(&#39;#table_field4 .trr&#39;).eq(prevRowIndex);
  // 从该行的选择菜单中获取 unit_id 的值
  const unitId = prevRow.find(&#39;select[name=&quot;unit_id[]&quot;]&#39;).val();
  
  // 将 unitId 的值设置到第二个表格中对应的行/输入中
  $(&#39;#table_field5 tbody tr&#39;).eq(prevRowIndex).find(&#39;input&#39;).first().val(unitId);
  
  // 如之前一样,将新行追加到底部表格
  // 为了提高可读性,我使用了模板字符串
  $(&#39;#table_field5&#39;).append(`&lt;tr&gt;
  	&lt;td&gt;
    	&lt;input type=&quot;text&quot; value=&quot;&quot;&gt;
    &lt;/td&gt;
    &lt;td&gt;
    	&lt;input type=&quot;text&quot; name=&quot;value[]&quot;&gt;
    &lt;/td&gt;
    &lt;td&gt;
    	&lt;input type=&quot;text&quot; name=&quot;value[]&quot;&gt;
    &lt;/td&gt;
  &lt;/tr&gt;`);
});

我为参考创建了一个fiddle。但请注意,它没有包含 Blade 模板插值。

额外要求

如果您想要使第二个表格中的值与第一个表格中所选内容保持同步,您需要在第一个表格上附加一个事件监听器,监听表格中所有 select[name=&quot;unit_id[]&quot;] 元素的更改。当它检测到更改时,它必须确定元素更改所在行的索引,并使用这个索引更新第二个表格中相应的行。

代码应该是:

$(&#39;#table_field4&#39;).on(&#39;change&#39;, &#39;select[name=&quot;unit_id[]&quot;]&#39;, function () {
  const $this = $(this);
  const rowIndex = $this.closest(&#39;tr&#39;).index();
  const unitId = $this.val();
  
  $(&#39;#table_field5 tbody tr&#39;).eq(rowIndex).find(&#39;input&#39;).first().val(unitId)
});

这是包含此添加内容的一个fiddle

英文:

It is possible that I don't fully understand what you are trying to do, but I will address the problem as I understand it.

I think that when the user clicks the "Add Cells" button, that you wish to add a new row to the bottom table (which you already have working) and to populate in the previously last row in the bottom table the value of the input in the "C" column. The value of this input should be the same as what was selected in the select menu in the third column of the last row in the top table.

To do this, I would implement the second #add4 click handler as follows:

$(&#39;#add4&#39;).click(function() {
// get the total count of rows in the top table
const numRows = $(&#39;#table_field4 .trr&#39;).length;
// get the index of the previously last row
// (we need to subtract 2 from the numRows because we have just added a row)
const prevRowIndex = numRows - 2;
// get the previously last row
const prevRow = $(&#39;#table_field4 .trr&#39;).eq(prevRowIndex);
// get the unit_id value from the select in that row
const unitId = prevRow.find(&#39;select[name=&quot;unit_id[]&quot;]&#39;).val();
// set the unitId value in the corresponding row/input in the second table
$(&#39;#table_field5 tbody tr&#39;).eq(prevRowIndex).find(&#39;input&#39;).first().val(unitId);
// append the new row to the bottom table as you were doing before
// I am using a template string for readability
$(&#39;#table_field5&#39;).append(`&lt;tr&gt;
&lt;td&gt;
&lt;input type=&quot;text&quot; value=&quot;&quot;&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;text&quot; name=&quot;value[]&quot;&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;input type=&quot;text&quot; name=&quot;value[]&quot;&gt;
&lt;/td&gt;
&lt;/tr&gt;`);
});

I have created a fiddle for reference. But note that it doesn't have the Blade template interpolation.

Additional Requirements

If you want to keep the values in the second table in sync with what is selected in the first table, you will need to attach an event listener on the first table that listens for changes to all select[name=&quot;unit_id[]&quot;] elements in the table. When it detects a change, it must determine the index of the row of the element that was changed and use this index to update the corresponding row in the second table.

The code would be:

$(&#39;#table_field4&#39;).on(&#39;change&#39;, &#39;select[name=&quot;unit_id[]&quot;]&#39;, function () {
const $this = $(this);
const rowIndex = $this.closest(&#39;tr&#39;).index();
const unitId = $this.val();
$(&#39;#table_field5 tbody tr&#39;).eq(rowIndex).find(&#39;input&#39;).first().val(unitId)
});

Here is a fiddle with this addition.

huangapple
  • 本文由 发表于 2023年2月20日 00:29:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501626.html
匿名

发表评论

匿名网友

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

确定