89 $this->_columns = $columns;
90 $this->_items = $items;
93 $this->sortable =
false;
96 $this->_columns[self::COLUMN_ROW_MANAGE] =
'';
99 wp_enqueue_script(
'wpdk-dynamic-table', WPDK_URI_JAVASCRIPT .
'wpdk-dynamic-table.js', array(), WPDK_VERSION );
100 wp_enqueue_style(
'wpdk-dynamic-table', WPDK_URI_CSS .
'wpdk-dynamic-table.css', array(), WPDK_VERSION );
115 if ( func_num_args() > 0 ) {
116 $this->_items = func_get_arg( 0 );
119 return $this->_items;
146 $html_thead = $this->thead();
147 $html_tbody = $this->tbody();
148 $html_tfoot = $this->tfoot();
150 $class = $this->classes();
151 $data = $this->data();
154 <table
id=
"{$id}" {$data}
class=
"wpdk-dynamic-table {$class}" border=
"0" cellpadding=
"0" cellspacing=
"0">
175 private function classes() {
176 $stack = array( $this->_class );
177 if (
true == $this->sortable ) {
178 $stack[] =
'wpdk-dynamic-table-sortable';
180 return join(
' ', $stack );
190 private function data() {
192 if (
true == $this->sortable ) {
193 $stack[] = sprintf(
'data-sortable="true"' );
195 return join(
' ', $stack );
205 private function buttonAdd() {
206 $label = __(
'Add', WPDK_TEXTDOMAIN );
207 $title = __(
'Add a new empty row', WPDK_TEXTDOMAIN );
210 <input data-placement=
"left" title=
"{$title}" title-backup=
"{$title}" type=
"button" value=
"{$label}" class=
"wpdk-has-tooltip wpdk-dt-add-row">
222 private function buttonDelete() {
223 $label = __(
'Delete', WPDK_TEXTDOMAIN );
224 $title = __(
'Delete entire row', WPDK_TEXTDOMAIN );
227 <input data-placement=
"left" title=
"{$title}" content=
"{$title}" type=
"button" value=
"{$label}" class=
"wpdk-has-tooltip wpdk-dt-delete-row">
243 private function thead() {
246 foreach ( $this->_columns as $key => $column ) {
247 if ( $key != self::COLUMN_ROW_MANAGE ) {
248 $ths .= sprintf(
'<th class="wpdk-dynamic-table-column-%s">%s</th>', $key, $column[
'table_title'] );
272 private function tbody() {
276 $trs .= sprintf(
'<tr class="wpdk-dt-clone">%s</tr>', $this->tbodyRow() );
278 if ( !empty( $this->_items ) ) {
279 foreach ( $this->_items as $item ) {
280 $trs .= sprintf(
'<tr>%s</tr>', $this->tbodyRow( $item ) );
283 $trs .= sprintf(
'<tr>%s</tr>', $this->tbodyRow() );
302 private function tbodyRow( $item = null ) {
304 foreach ( $this->_columns as $key => $column ) {
305 if ( self::COLUMN_ROW_MANAGE != $key ) {
307 if ( !is_null( $item ) && is_array( $item ) ) {
308 $column[
'value'] = isset( $item[$key] ) ? $item[$key] :
'';
314 $tds .= sprintf(
'<td class="wpdk-dynamic-table-cel-%s">%s</td>', $key, $field );
317 if ( is_null( $item ) ) {
318 $tds .= sprintf(
'<td class="%s">%s<span class="wpdk-dt-clone delete">%s</span></td>', $key, $this->buttonAdd(), $this->buttonDelete() );
321 $tds .= sprintf(
'<td class="%s">%s</td>', $key, $this->buttonDelete() );
336 private function tfoot() {
339 foreach ( $this->_columns as $key => $column ) {
340 if ( $key != self::COLUMN_ROW_MANAGE ) {
341 $tds .= sprintf(
'<td class="wpdk-dynamic-table-cel-%s"></td>', $key );
409 parent::__construct(
$id );
412 $this->
columns[self::COLUMN_ROW_MANAGE] =
'';
447 die( __METHOD__ .
' must be override in your subclass' );
455 private function _columns()
458 $columns[self::COLUMN_ROW_MANAGE] =
'';
473 public function items()
475 die( __METHOD__ .
' must be override in your subclass' );
489 public function draw()
493 <table
id=
"<?php printf( 'wpdk-dynamic-table-%s', $this->id ) ?>"
494 class=
"wpdk-dynamic-table <?php echo $this->sortable ? 'wpdk-dynamic-table-sortable' : '' ?>"
495 <?php echo $this->sortable ?
'data-sortable="true"' :
'' ?>
502 <?php $index = 0;
foreach ( $this->_columns() as $column_key => $column ) : ?>
503 <?php
if ( self::COLUMN_ROW_MANAGE != $column_key ) : ?>
504 <th <?php echo (
true == $this->sortable && empty( $index ) ) ?
'colspan="2"' :
'' ?>
505 class=
"wpdk-dynamic-table-column-<?php echo $column_key ?>">
506 <?php echo $column[
'_label']; $index++; ?>
514 <!-- This row is used
for clone -->
515 <tr
class=
"wpdk-dt-clone">
516 <?php $index = 0;
foreach ( $this->_columns() as $column_key => $column ) : ?>
518 <?php
if ( self::COLUMN_ROW_MANAGE == $column_key ) : ?>
519 <td
class=
"<?php echo $column_key ?>">
520 <?php echo $this->buttonAdd() ?>
521 <span
class=
"wpdk-dt-clone delete"><?php echo $this->buttonDelete() ?></span>
524 <?php
if( $this->sortable && empty( $index ) ) : ?>
527 <td
class=
"wpdk-dynamic-table-cel-<?php echo $column_key ?>">
536 <?php
foreach ( $this->
items() as $item ) : ?>
538 <?php $index = 0;
foreach ( $this->_columns() as $column_key => $column ) : $column[
'value'] = isset( $item[$column_key] ) ? $item[$column_key] :
'' ?>
540 <?php
if ( self::COLUMN_ROW_MANAGE == $column_key ) : ?>
541 <td
class=
"<?php echo $column_key ?>">
542 <?php echo $this->buttonDelete() ?>
545 <?php
if( $this->sortable && empty( $index ) ) : ?>
548 <td
class=
"wpdk-dynamic-table-cel-<?php echo $column_key ?>">
557 <!-- Extra last child row -->
559 <?php $index = 0;
foreach ( $this->_columns() as $column_key => $column ) : ?>
561 <?php
if ( self::COLUMN_ROW_MANAGE == $column_key ) : ?>
562 <td
class=
"<?php echo $column_key ?>">
563 <?php echo $this->buttonAdd() ?>
564 <span
class=
"wpdk-dt-clone delete"><?php echo $this->buttonDelete() ?></span>
567 <?php
if( $this->sortable && empty( $index ) ) : ?>
570 <td
class=
"wpdk-dynamic-table-cel-<?php echo $column_key ?>">
580 <?php
if( 1 == 0 ) : ?>
584 <?php $index = 0;
foreach ( $this->_columns() as $column_key => $column ) : ?>
586 <?php
if ( self::COLUMN_ROW_MANAGE != $column_key ) : ?>
587 <td <?php echo (
true == $this->sortable && empty( $index ) ) ?
'colspan="2"' :
'' ?>
588 class=
"wpdk-dynamic-table-cel-<?php echo $column_key ?>"></td>
589 <?php endif; $index++ ?>
612 private function buttonAdd()
616 class=
"wpdk-has-tooltip wpdk-dt-add-row"
617 title=
"<?php _e( 'Add a new empty row', WPDK_TEXTDOMAIN ) ?>"
618 title-backup=
"<?php _e( 'Add a new empty row', WPDK_TEXTDOMAIN ) ?>"
619 data-placement=
"left"
628 class=
"wpdk-has-tooltip wpdk-dt-add-row"
629 title=
"<?php _e( 'Add a new empty row', WPDK_TEXTDOMAIN ) ?>"
630 title-backup=
"<?php _e( 'Add a new empty row', WPDK_TEXTDOMAIN ) ?>"
631 data-placement=
"left"
632 value=
"<?php _e( 'Add', WPDK_TEXTDOMAIN ) ?>"
645 private function buttonDelete()
649 class=
"wpdk-has-tooltip wpdk-dt-delete-row"
650 title=
"<?php _e( 'Delete entire row', WPDK_TEXTDOMAIN ) ?>"
651 title-backup=
"<?php _e( 'Delete entire row', WPDK_TEXTDOMAIN ) ?>"
652 data-placement=
"left"
661 class=
"wpdk-has-tooltip wpdk-dt-delete-row"
662 title=
"<?php _e( 'Delete entire row', WPDK_TEXTDOMAIN ) ?>"
663 title-backup=
"<?php _e( 'Delete entire row', WPDK_TEXTDOMAIN ) ?>"
664 data-placement=
"left"
665 value=
"<?php _e( 'Delete', WPDK_TEXTDOMAIN ) ?>"