响应式表单表插件 jquery.manage.Form.tables.js

  • 源码大小:15.49KB
  • 所需积分:1积分
  • 源码编号:19JP-3183
  • 浏览次数:848次
  • 最后更新:2023年05月15日
  • 所属栏目:表格
本站默认解压密码:19jp.com 或 19jp_com

简介

manage.form.tables.js是一个简单而强大的jQuery表操作插件,用于在web应用程序上创建动态表单表。

 表单表通常用于web应用程序,用户需要一次将数据输入多个字段,例如填写调查或注册帐户时。通过以表格格式呈现输入,用户更容易看到所有需要的输入,并以结构化的方式输入必要的信息。例如,在显示客户信息的表中,每一行可能代表不同的客户,并以表单输入客户的姓名、地址、电话号码等。

该插件的关键功能之一是能够动态添加和删除行,从而可以根据需要轻松插入/删除表行。它还使用jQuery验证引擎插件来验证行输入,确保所有字段都是有效的,并且数据是干净准确的。

如何使用它:

1.在文档中加载所需的jQuery库和验证引擎插件。

  1. <!-- jQuery Is Required -->
  2. <script src="/path/to/cdn/jquery.min.js"></script>
  3.  
  4. <!-- Validation Engine plugin -->
  5. <link href="/path/to/cdn/validationEngine.jquery.min.css" rel="stylesheet"/>
  6. <script src="/path/to/cdn/languages/jquery.validationEngine-en.min.js"></script>
  7. <script src="/path/to/cdn/jquery.validationEngine.min.js"></script>

2.加载manage.form.tables.js插件的文件。

  1. <link href="src/jquery.manage.form.resposive-tables.css" rel="stylesheet"/>
  2. <script src="src/jquery.manage.form.tables.js"></script>

3.在插件的示例页面上使用了Bootstrap 5和Font Awesome Iconic Font来实现快速造型。可选。

  1. <!-- Bootstrap 5 -->
  2. <link rel="stylesheet" href="/path/to/cdn/bootstrap.min.css" />
  3. <script src="/path/to/cdn/bootstrap.bundle.min.js"></script>
  4.  
  5. <!-- Font Awesome -->
  6. <link rel="stylesheet" href="/path/to/font-awesome/all.min.css" />

4.在表单中添加一个空表。

  1. <form id="formID" method="post" action="#">
  2. <table class="table table-striped table-hover table-clone-row " >
  3. <thead>
  4. <tr>
  5. <th scope="col">
  6. <!-- Add Row Button -->
  7. <button class="btn btn-success add-row"><i class="fa fa-plus"></i></button>
  8. </th>
  9. <th scope="col">NAME</th>
  10. <th scope="col">EMAIL</th>
  11. <th scope="col">PHONE</th>
  12. <th scope="col">ACTIONS</th>
  13. </tr>
  14. </thead>
  15. <tbody></tbody>
  16. <tfoot>
  17. <tr>
  18. <td colspan="5" class="text-center">
  19. <button class="btn btn-success sender">SEND FORM</button>
  20. </td>
  21. </tr>
  22. </tfoot>
  23. </table>
  24. </form>

5.为行输入创建一个模板,如下所示:

  1. const template = `
  2. <tr role="row">
  3. <td role="cell" data-label="#i" >
  4. <a href="javascript:void(0);" class="btn btn-danger btn-sm remove">
  5. <i class="fa fa-times"></i>
  6. </a>
  7. </td>
  8. <td role="cell" data-label="Name">
  9. <input type="name" name="name[]" class="form-control" data-validation-engine="validate[required,custom[onlyLetterSp],maxSize[20]]"
  10. />
  11. </td>
  12. <td role="cell" data-label="Email">
  13. <input type="email" name="email[]" class="form-control" data-validation-engine="validate[required,custom[email]]"
  14. />
  15. </td>
  16. <td role="cell" data-label="Phone">
  17. <input type="text" name="phone[]" class="form-control" data-validation-engine="validate[required,custom[phone]]"
  18. />
  19. </td>
  20. <td role="cell" data-label="Actions">
  21. <a href="javascript:void(0);" class="btn btn-warning btn-sm lock">
  22. <i class="fa fa-unlock"></i>
  23. </a>
  24. <a href="javascript:void(0);" class="btn btn-success btn-sm edit">
  25. <i class="fa fa-edit"></i>
  26. </a>
  27. </td>
  28. </tr>
  29. `;

6.初始化表单元素上的插件。

  1. $('.table-clone-row').manageFormTables({
  2.  
  3. // row template
  4. templateRow: template,
  5.  
  6. // selector of remove button
  7. removeRowTarget: '.remove',
  8.  
  9. // selector of add button
  10. addRowTarget: '.add-row',
  11.  
  12. // min number of visible rows
  13. minRowsVisible: 1,
  14. // selector of submit button
  15. senderTarget: '.sender',
  16.  
  17. // form title
  18. tableFormTitle: 'Formulario',
  19. // regex
  20. indexRowPattern: /#i/g,
  21. // debug mode
  22. debug: 0,
  23. // callbacks
  24. onSubmit: function (form) {},
  25. onErrorRowsVisible(element, form) {},
  26.  
  27. });

7.在中创建自定义操作事件大堆

  1. $('.table-clone-row').manageFormTables({
  2. events:[
  3. {
  4. // lock button
  5. targetName: '.lock',
  6. eventName: 'click',
  7. onEvent: function () {
  8. const _this = $(this);
  9. const tr = _this.closest("tr");
  10. if (_this.hasClass('in-lock')) {
  11. tr.find('input').removeAttr('readonly').removeClass('disabled');
  12. tr.find('.remove').removeClass('disabled');
  13. _this.removeClass('in-lock btn-info').addClass('btn-warning');
  14. _this.html('<i class="fa fa-unlock"></i>');
  15. } else {
  16. tr.find('input').attr('readonly', true).addClass('disabled');
  17. _this.addClass('in-lock btn-info').removeClass('btn-warning');
  18. tr.find('.remove').addClass('disabled');
  19. _this.html('<i class="fa fa-lock"></i>');
  20. }
  21. }
  22. }
  23. ]
  24. });

预览截图