从CSV中复制多行粘贴到HTML表格中的输入栏。

问题描述 投票:0回答:1

我正在尝试使用Jquery将CSV文件中的数据复制粘贴到HTML表单中。我的表单有一个输入字段数组,所以我可以在提交时同时进行多次插入。

现在,假设我将CSV文件中的多行复制粘贴到表格中第一行的第二列,表格的第一行正确地显示了数据,但在第二行中,粘贴的数据从第一列本身开始,其中应该从第二行开始,就像表格的第一行那样

CSV行和单元格

1   4   a
2   5   b
3   6   c

截图

enter image description here

            function csv_paste_datagrid(event){
                $(document).ready(function() {
                    $('input').bind('paste', null, function (e) {
                        $this = $(this);
                        setTimeout(function () {
                            var columns = $this.val().split(/\s+/);
                            var i;
                            var input = $this;
                            for (i = 0; i < columns.length; i++) {
                                input.val(columns[i]);
                                if( i % 3 !== 2){
                                    input = input.parent().parent().parent().parent().parent().next().find('input');
                                    } else{
                                    input = input.parent().parent().parent().parent().parent().parent().next().find('input').first();
                                }
                            }
                        }, 0);
                    });
                });

超文本标记语言

            <form style="width : 100%;" id="system_validations" name="system_validations" accept-charset="utf-8" method="POST" class="form-control" enctype="multipart/form-data">
               <table style="display : inline;width : 100%;"></table>
               <table id="" class="system_form_tables_parent">
                  <tbody>
                     <tr>
                        <th></th>
                        <td>
                           <table id="form_table[0]" class="system_form_tables_child" style="margin-left:auto; margin-right:auto;">
                              <tbody>
                                 <tr>
                                    <td style=" " id="container_validation_options[0]">
                                       <table>
                                          <tbody>
                                             <tr id="tr_validation_options[0]" style="">
                                                <th class="th_class1"><span class=""> validation_options  </span></th>
                                             </tr>
                                             <tr>
                                                <td class="td_class">   <input type="text" id="validation_options[0]" name="validation_options[0]" placeholder="" class="" value=""> </td>
                                             </tr>
                                             <tr>
                                                <th></th>
                                             </tr>
                                             <tr>
                                                <th></th>
                                             </tr>
                                             <tr>
                                                <td class="val_error"></td>
                                             </tr>
                                          </tbody>
                                       </table>
                                    </td>
                                    <td style=" " id="container_validation_display[0]">
                                       <table>
                                          <tbody>
                                             <tr id="tr_validation_display[0]" style="">
                                                <th class="th_class1"><span class=""> validation_display  </span></th>
                                             </tr>
                                             <tr>
                                                <td class="td_class">   <input type="text" id="validation_display[0]" name="validation_display[0]" placeholder="" class="" value=""> </td>
                                             </tr>
                                             <tr>
                                                <th></th>
                                             </tr>
                                             <tr>
                                                <th></th>
                                             </tr>
                                             <tr>
                                                <td class="val_error"></td>
                                             </tr>
                                          </tbody>
                                       </table>
                                    </td>
                                    <td style=" " id="container_blocked_modules[0]">
                                       <table>
                                          <tbody>
                                             <tr id="tr_blocked_modules[0]" style="">
                                                <th class="th_class1"><span class=""> blocked_modules  </span></th>
                                             </tr>
                                             <tr>
                                                <td class="td_class">   <input type="text" id="blocked_modules[0]" name="blocked_modules[0]" placeholder="" class="" value=""> </td>
                                             </tr>
                                             <tr>
                                                <th></th>
                                             </tr>
                                             <tr>
                                                <th></th>
                                             </tr>
                                             <tr>
                                                <td class="val_error"></td>
                                             </tr>
                                          </tbody>
                                       </table>
                                    </td>
                                 </tr>
                                 <tr>
                                    <td style=" " id="container_validation_options[0]">
                                       <table>
                                          <tbody>
                                             <tr id="tr_validation_options[1]" style="">
                                                <th class="th_class1"><span class=""> validation_options  </span></th>
                                             </tr>
                                             <tr>
                                                <td class="td_class">   <input type="text" id="validation_options[1]" name="validation_options[1]" placeholder="" class="" value=""> </td>
                                             </tr>
                                             <tr>
                                                <th></th>
                                             </tr>
                                             <tr>
                                                <th></th>
                                             </tr>
                                             <tr>
                                                <td class="val_error"></td>
                                             </tr>
                                          </tbody>
                                       </table>
                                    </td>
                                    <td style=" " id="container_validation_display[0]">
                                       <table>
                                          <tbody>
                                             <tr id="tr_validation_display[1]" style="">
                                                <th class="th_class1"><span class=""> validation_display  </span></th>
                                             </tr>
                                             <tr>
                                                <td class="td_class">   <input type="text" id="validation_display[1]" name="validation_display[1]" placeholder="" class="" value=""> </td>
                                             </tr>
                                             <tr>
                                                <th></th>
                                             </tr>
                                             <tr>
                                                <th></th>
                                             </tr>
                                             <tr>
                                                <td class="val_error"></td>
                                             </tr>
                                          </tbody>
                                       </table>
                                    </td>
                                    <td style=" " id="container_blocked_modules[0]">
                                       <table>
                                          <tbody>
                                             <tr id="tr_blocked_modules[1]" style="">
                                                <th class="th_class1"><span class=""> blocked_modules  </span></th>
                                             </tr>
                                             <tr>
                                                <td class="td_class">   <input type="text" id="blocked_modules[1]" name="blocked_modules[1]" placeholder="" class="" value=""> </td>
                                             </tr>
                                             <tr>
                                                <th></th>
                                             </tr>
                                             <tr>
                                                <th></th>
                                             </tr>
                                             <tr>
                                                <td class="val_error"></td>
                                             </tr>
                                          </tbody>
                                       </table>
                                    </td>
                                 </tr>
                                 <tr style="">
                                    <td style="text-align : left;padding-left:0.5em">
                                       <table id="submit_table">
                                          <tbody>
                                             <tr>
                                                <td><input type="button" class="common_button" id="system_validations_back" name="system_validations_back" style="" value="Back" onclick="" title="Back">
                                                   <input type="reset" class="common_button" id="system_validations_reset" name="system_validations_reset" style="" value="Reset" title="Reset">
                                                   <input type="button" class="common_button" id="submit" name="system_validations_submit" onclick="" style="" value="Submit" title="Submit">
                                                </td>
                                             </tr>
                                          </tbody>
                                       </table>
                                    </td>
                                 </tr>
                              </tbody>
                           </table>
                        </td>
                     </tr>
                  </tbody>
               </table>
            </form>
javascript jquery
1个回答
0
投票

对,我必须清除一些双 id并在你的HTML中先添加一个类属性(contTD)到您的 "主 "容器 <td>s. 之后,整个事情就相当容易地落到了实处。

  • 为了防止实际的TSV文本被直接粘贴到第一个输入字段,我使用了... e.preventDefault()
  • 然后我用 .split() 在 TSV 字符串上使用两次,将其转化为 2D 数组。vals
  • 我确定了 .closest() td.contTD元素(--> td)及其列号和行号(colrow),找到 .index()td 和它的父行。
  • .closest('tbody')然后,我又一路向下,通过 .slice()开始的行数 row 以及从列开始的子输入元素(切片)。col.
  • .each() 循环,然后我将 vals-数组到输入元素,但只有当 val[i][j] 存在!

脚本还可以进一步改进,因为它将运行所有的 <tr>从行到表的 row 到最后。但我希望这对你来说是一个起点,并给你提供了一些关于如何使用jquery的想法。

在我的脚本中,我使用了一个委托粘贴事件绑定到 <form> 元素。这应该能很好地与动态表配合使用。我没有把它打包成一个额外的功能。但是,当然,当你在你的网站中使用它时,它应该放在你的onload部分。

最后:在我的第二个 .split() 我正在寻找一个tab字符作为列的分隔符,所以这个例子将适用于TSV文件格式。如果你想把它应用在空格或逗号分隔的值上,你应该把正则表达式改成类似于 /\s//,/ .

$('form').on('paste', 'input', function (e) {
  e.preventDefault(); // do not paste the contents into the first cell ...
  // convert TSV from clipboard into a 2D array:
  let vals=event.clipboardData.getData('text').trim().split(/\r?\n */).map(r=>r.split(/\t/));
  let td=$(this).closest('.contTD'); // closest  container TD and work from there
  let col=td.index(), row=td.parent().index(), tbdy=td.closest('tbody');
  // modify input fields of rows >= row and columns >= col: 
  tbdy.children('tr').slice(row).each((i,tr)=>{
    $(tr).find('td input:text').slice(col).each((j,ti)=>{
     if(vals[i]&&vals[i][j]!=null) ti.value=vals[i][j] } 
  )});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form style="width : 100%;" id="system_validations" name="system_validations" accept-charset="utf-8" method="POST" class="form-control" enctype="multipart/form-data">
  <table style="display : inline;width : 100%;"></table>
  <table id="" class="system_form_tables_parent">
    <tbody>
        <tr>
          <th></th>
          <td>
            <table id="form_table[0]" class="system_form_tables_child" style="margin-left:auto; margin-right:auto;">
              <tbody>
                  <tr>
                    <td class="contTD"><table>
                      <tbody><tr><th class="th_class1"><span class="">extra column</span></th></tr>
                      <tr><td class="td_class"><input type="text" value="00A"> </td></tr>
                      <tr><th></th></tr>
                      <tr><th></th></tr>
                      <tr><td class="val_error"></td></tr></tbody>
                    </table></td>
                    <td class="contTD" id="container_validation_options[0]">
                        <table>
                          <tbody>
                              <tr id="tr_validation_options[0]">
                                <th class="th_class1"><span class=""> validation_options  </span></th>
                              </tr>
                              <tr>
                                <td class="td_class">   <input type="text" id="validation_options[0]" name="validation_options[0]" placeholder="" class="" value="01"> </td>
                              </tr>
                              <tr>
                                <th></th>
                              </tr>
                              <tr>
                                <th></th>
                              </tr>
                              <tr>
                                <td class="val_error"></td>
                              </tr>
                          </tbody>
                        </table>
                    </td>
                    <td class="contTD" id="container_validation_display[0]">
                        <table>
                          <tbody>
                              <tr id="tr_validation_display[0]">
                                <th class="th_class1"><span class=""> validation_display  </span></th>
                              </tr>
                              <tr>
                                <td class="td_class">   <input type="text" id="validation_display[0]" name="validation_display[0]" placeholder="" class="" value="02"> </td>
                              </tr>
                              <tr>
                                <th></th>
                              </tr>
                              <tr>
                                <th></th>
                              </tr>
                              <tr>
                                <td class="val_error"></td>
                              </tr>
                          </tbody>
                        </table>
                    </td>
                    <td class="contTD" id="container_blocked_modules[0]">
                        <table>
                          <tbody>
                              <tr id="tr_blocked_modules[0]">
                                <th class="th_class1"><span class=""> blocked_modules  </span></th>
                              </tr>
                              <tr>
                                <td class="td_class">   <input type="text" id="blocked_modules[0]" name="blocked_modules[0]" placeholder="" class="" value="03"> </td>
                              </tr>
                              <tr>
                                <th></th>
                              </tr>
                              <tr>
                                <th></th>
                              </tr>
                              <tr>
                                <td class="val_error"></td>
                              </tr>
                          </tbody>
                        </table>
                    </td>
                  </tr>
                  <tr>
                    <td class="contTD"><table>
                      <tbody><tr><th class="th_class1"><span class="">extra column</span></th></tr>
                      <tr><td class="td_class"><input type="text" value="00A"> </td></tr>
                      <tr><th></th></tr>
                      <tr><th></th></tr>
                      <tr><td class="val_error"></td></tr></tbody>
                    </table></td>
                    <td class="contTD" id="container_validation_options[1]">
                        <table>
                          <tbody>
                              <tr id="tr_validation_options[1]">
                                <th class="th_class1"><span class=""> validation_options  </span></th>
                              </tr>
                              <tr>
                                <td class="td_class">   <input type="text" id="validation_options[1]" name="validation_options[1]" placeholder="" class="" value="04"> </td>
                              </tr>
                              <tr>
                                <th></th>
                              </tr>
                              <tr>
                                <th></th>
                              </tr>
                              <tr>
                                <td class="val_error"></td>
                              </tr>
                          </tbody>
                        </table>
                    </td>
                    <td class="contTD" id="container_validation_display[1]">
                        <table>
                          <tbody>
                              <tr id="tr_validation_display[1]">
                                <th class="th_class1"><span class=""> validation_display  </span></th>
                              </tr>
                              <tr>
                                <td class="td_class">   <input type="text" id="validation_display[1]" name="validation_display[1]" placeholder="" class="" value="05"> </td>
                              </tr>
                              <tr>
                                <th></th>
                              </tr>
                              <tr>
                                <th></th>
                              </tr>
                              <tr>
                                <td class="val_error"></td>
                              </tr>
                          </tbody>
                        </table>
                    </td>
                    <td class="contTD" id="container_blocked_modules[1]">
                        <table>
                          <tbody>
                              <tr id="tr_blocked_modules[1]">
                                <th class="th_class1"><span class=""> blocked_modules  </span></th>
                              </tr>
                              <tr>
                                <td class="td_class">   <input type="text" id="blocked_modules[1]" name="blocked_modules[1]" placeholder="" class="" value="06"> </td>
                              </tr>
                              <tr>
                                <th></th>
                              </tr>
                              <tr>
                                <th></th>
                              </tr>
                              <tr>
                                <td class="val_error"></td>
                              </tr>
                          </tbody>
                        </table>
                    </td>
                  </tr>
                  <tr>
                    <td style="text-align : left;padding-left:0.5em">
                        <table id="submit_table">
                          <tbody>
                              <tr>
                                <td><input type="button" class="common_button" id="system_validations_back" name="system_validations_back" value="Back" title="Back">
                                    <input type="reset" class="common_button" id="system_validations_reset" name="system_validations_reset" value="Reset" title="Reset">
                                    <input type="button" class="common_button" id="submit" name="system_validations_submit" value="Submit" title="Submit">
                                </td>
                              </tr>
                          </tbody>
                        </table>
                    </td>
                  </tr>
              </tbody>
            </table>
        </td>
      </tr>
  </tbody>
</table>
</form>
© www.soinside.com 2019 - 2024. All rights reserved.