jQuery插件 复制和恢复表行 Dynamicrows

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

简介

Dynamicrows是一个小型jQuery插件,只需单击一下就可以克隆和删除表行中的任何元素(例如表单字段)。

还提供了通过拖放来使用表行的功能。需要jQuery UI可排序小部件。

更多功能:

  • 允许在表单字段中键入克隆值。
  • 允许在添加/删除时淡入/淡出表行。
  • 添加/删除/移动/更新时的回调函数。

如何使用它:

1.下载并加载动态Crows.min.js在jQuery之后。

  1. <script src="/path/to/cdn/jquery.min.js"></script>
  2. <script src="/path/to/js/dynamicrows.js"></script>

2.如果您需要可排序功能,请加载jQuery UI库(可排序小部件)。

  1. <script src="https://cdn.jsdelivr.net/npm/[email protected]/jquery-ui.min.js"></script>

3.将克隆/删除按钮和拖动手柄添加到HTML表中,如下所示:

  1. <table data-dynamicrows>
  2. <thead>
  3. <tr>
  4. <th>Firstname</th>
  5. <th>Lastname</th>
  6. <th>E-Mail</th>
  7. <th>Actions</th>
  8. </tr>
  9. </thead>
  10. <tbody>
  11. <tr>
  12. <td><input type="text" name="contacts[0][firstname]"></td>
  13. <td><input type="text" name="contacts[0][lastname]"></td>
  14. <td><input type="text" name="contacts[0][email]"></td>
  15. <td>
  16. <i class="fa fa-minus" data-remove></i>
  17. <i class="fa fa-arrows" data-move></i>
  18. <i class="fa fa-plus" data-add></i>
  19. </td>
  20. </tr>
  21. </tbody>
  22. </table>

4.附加功能动态Crows()到HTML表。完成。

  1. $(function() {
  2. $('[data-dynamicrows]').dynamicrows();
  3. });

5.通过覆盖默认配置来自定义插件,如下所示:

  1. $('[data-dynamicrows]').dynamicrows({
  2.  
  3. // CSS selector of table row
  4. row: 'tr',
  5.  
  6. // CSS selector of tbody
  7. rows: 'tbody',
  8.  
  9. // minimum number of table rows
  10. minrows: 1,
  11.  
  12. // custom template while cloning
  13. copyRow : null,
  14.  
  15. // clone form values
  16. copyValues: false,
  17.  
  18. // element to hold row numbering
  19. increment : null,
  20.  
  21. // prefix for form element that is ignored when the names are updated
  22. form_prefix: '',
  23.  
  24. // prevent renaming form elements
  25. prevent_renaming : false,
  26.  
  27. // add/remove/sort attributes
  28. handle_add: '[data-add]:not(.disabled)',
  29. handle_remove: '[data-remove]:not(.disabled)',
  30. handle_move: '[data-move]:not(.disabled)',
  31.  
  32. // start index
  33. index_start: 0,
  34.  
  35. // enable fade animation
  36. animation: false, // 'fade'
  37. animation_speed: 300
  38. });

6.回调函数。

  1. $('[data-dynamicrows]').dynamicrows({
  2.  
  3. // parameter: $row
  4. beforeAdd: null,
  5. beforeRemove: null,
  6. beforeMove: null,
  7. beforeFormUpdateNames: null,
  8. beforeAll: null,
  9. afterAdd: null,
  10. afterRemove: null,
  11. afterMove: null,
  12. afterFormUpdateNames: null,
  13. afterAll: null,
  14.  
  15. });

更新日志:

2023-01-29

  • 修复名称检查

2020-10-03

  • 新选项form_prefix和prevent_renaming

2020-10-03

  • Bugfix重置选择值

预览截图