| 1 |
/** |
| 2 |
* Add / delete table row functionality, for repeaters |
| 3 |
* in tables. |
| 4 |
* |
| 5 |
* @package WPZincDashboardWidget |
| 6 |
* @author WP Zinc |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Adds a Table Row. |
| 11 |
* |
| 12 |
* @since 1.0.0 |
| 13 |
* |
| 14 |
* @param string selector Selector. |
| 15 |
* @param string table Table. |
| 16 |
*/ |
| 17 |
function wpzinc_table_row_add( selector, table ) { |
| 18 |
|
| 19 |
( function ( $ ) { |
| 20 |
|
| 21 |
// Get row. |
| 22 |
var row = $( 'tbody tr.' + selector, $( table ) ); |
| 23 |
|
| 24 |
// Clone row. |
| 25 |
$( 'tbody tr:last-child', $( table ) ).after( '<tr class="' + selector + '">' + $( row ).html() + '</tr>' ); |
| 26 |
|
| 27 |
} )( jQuery ); |
| 28 |
|
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Deletes the table row for the given button. |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
* |
| 36 |
* @param object button Delete Table Row Button. |
| 37 |
*/ |
| 38 |
function wpzinc_table_row_delete( button ) { |
| 39 |
|
| 40 |
( function ( $ ) { |
| 41 |
|
| 42 |
// Remove row. |
| 43 |
$( button ).closest( 'tr' ).remove(); |
| 44 |
|
| 45 |
} )( jQuery ); |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Register event listeners |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
*/ |
| 54 |
jQuery( document ).ready( |
| 55 |
function ( $ ) { |
| 56 |
|
| 57 |
/** |
| 58 |
* Add Table Row. |
| 59 |
*/ |
| 60 |
$( 'body' ).on( |
| 61 |
'click', |
| 62 |
'.wpzinc-add-table-row', |
| 63 |
function ( e ) { |
| 64 |
|
| 65 |
e.preventDefault(); |
| 66 |
|
| 67 |
wpzinc_table_row_add( |
| 68 |
$( this ).attr( 'data-table-row-selector' ), |
| 69 |
$( this ).closest( 'table' ) |
| 70 |
); |
| 71 |
|
| 72 |
} |
| 73 |
); |
| 74 |
|
| 75 |
/** |
| 76 |
* Delete Table Row. |
| 77 |
*/ |
| 78 |
$( 'body' ).on( |
| 79 |
'click', |
| 80 |
'.wpzinc-delete-table-row', |
| 81 |
function ( e ) { |
| 82 |
|
| 83 |
e.preventDefault(); |
| 84 |
|
| 85 |
wpzinc_table_row_delete( this ); |
| 86 |
|
| 87 |
} |
| 88 |
); |
| 89 |
|
| 90 |
} |
| 91 |
); |
| 92 |
|