Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active September 26, 2024 22:25
Show Gist options
  • Save xnau/64d3175aeb190572c7d31ba46555658c to your computer and use it in GitHub Desktop.
Save xnau/64d3175aeb190572c7d31ba46555658c to your computer and use it in GitHub Desktop.
Participants Database list display template to demonstrate how to use a placeholder field to show an enumeration column
<?php
/**
* template that demonstrates how to use a placeholder field to show an enumeration column
*
*/
$enum_field = 'rank'; // name of the placeholder field
$records_per_page = $this->pagination->size;
$numeration = 1;
?>
<div class="wrap <?php esc_attr_e( $this->wrap_class ) ?>" id="<?php esc_attr_e( $this->list_anchor ) ?>">
<?php $this->show_search_sort_form() ?>
<table class="wp-list-table widefat fixed pages list-container" >
<?php if ( has_action( 'pdb-prepend_to_list_container_content' ) ) : ?>
<caption>
<?php do_action( 'pdb-prepend_to_list_container_content' ) ?>
<?php $this->print_list_count( '<div class="%s"><span class="list-display-count">' ) ?>
</caption>
<?php else : ?>
<?php
$this->print_list_count( '<caption class="%s" ><span class="list-display-count">' );
?>
<?php endif ?>
<?php if ( $record_count > 0 ) : // print only if there are records to show ?>
<thead>
<tr>
<?php
/*
* this function prints headers for all the fields
* replacement codes:
* %2$s is the form element type identifier
* %1$s is the title of the field
*/
$this->print_header_row( '<th class="%2$s" scope="col">%1$s</th>' );
?>
</tr>
</thead>
<tbody>
<?php while ( $this->have_records() ) : $this->the_record(); // each record is one row ?>
<tr>
<?php while ( $this->have_fields() ) : $this->the_field(); // each field is one cell ?>
<?php
/*
* if we're showing the enumeration display field, set the value to display
*/
if ( $this->field->name() === $enum_field )
{
//this is where we calculate the number to display
$line_number = $numeration + ( $records_per_page * ( $this->pagination->page - 1 ) );
// assign the line number to the placeholder field so it will be displayed
$this->field->default_value = strval( $line_number ) . '.';
}
?>
<td class="<?php esc_attr_e( $this->field->name() ) ?>-field">
<?php $this->field->print_value() ?>
</td>
<?php endwhile; // each field ?>
</tr>
<?php
$numeration = $numeration + 1;
?>
<?php endwhile; // each record ?>
</tbody>
<?php else : // if there are no records ?>
<tbody>
<tr>
<td><?php if ( $this->is_search_result ) echo wp_kses_post( Participants_Db::plugin_setting('no_records_message') ) ?></td>
</tr>
</tbody>
<?php endif; // $record_count > 0 ?>
</table>
<?php $this->show_pagination_control() ?>
</div>
@xnau
Copy link
Author

xnau commented Sep 26, 2024

To configure this, first create a "Placeholder" field to hold the list number value.

Make sure the placeholder field is visible in the list by configuring it on the Manage List Columns page.

To use this template place the template in your Participant Database template location (usually /wp-content/participants-database-templates). Enable the template in the shortcode like this: [pdb_list template=enumerated]

Once the template is copied into place, edit line 7 to match the name of your placeholder field.

Read this article if you need help understanding how to use the template: Using Participants Database Custom Templates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment