• Resolved datverse

    (@datverse)


    Hi,
    Love your plugin.
    I want create Responsive table like this:
    https://codepen.io/AllThingsSmitty/pen/MyqmdM

    But by this way I need add custom html data-attribute to <td> / <th> then create custom CSS.

    How can I add <td data-label=”{header-(number)-value}”>;.

    Any hook, php code to do this?

    Thanks

    • This topic was modified 2 years, 4 months ago by datverse.
    • This topic was modified 2 years, 4 months ago by datverse.
Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter datverse

    (@datverse)

    Hi, I found the hook to apply attribute to th/td

    add_filter( 'tablepress_cell_tag_attributes', function ($tag_attributes, $table_id, $cell_content, $row_idx, $col_idx, $colspan_row, $rowspan_col) {
    
    	$attribute_value = 'foo-value';
    
    	return [ 'data-foo' => $attribute_value ];
    
    }, 10, 7 );

    But how can I get the value of the header cell(s) in header row?

    Thanks

    • This reply was modified 2 years, 4 months ago by datverse.
    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    thanks for your post, and sorry for the trouble.

    Unfortunately, that information is not available directly inside that filter hook, so that your best chance probably is to save it to some temporary variable from inside the tablepress_table_render_data filter hook, which does get full table data. Then, in your current filter hook handler, use the $col_idx variable to get the header value for that column.

    Regards,
    Tobias

    Thread Starter datverse

    (@datverse)

    Hi,
    Thanks for your reply.

    Can you help me complete the code?

    Love.

    Thread Starter datverse

    (@datverse)

    Hi, how can I get some variable array value in $table[‘data’] in ‘tablepress_cell_tag_attributes’ filter?

    Thanks

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    sorry, developing the full code is beyond what I can help with at the moment, due to time and resource constraints.

    To get that array into the filter, you would have to assign it to an outside (global or maybe a class property) variable from within that tablepress_table_render_data filter hook.

    Regards,
    Tobias

    Thread Starter datverse

    (@datverse)

    Hi,
    Got it!

    Thanks so much, love!

    Thread Starter datverse

    (@datverse)

    The full code:

    add_filter( 'tablepress_table_render_data', function ( $table, $orig_table, $render_options ) {
    
    	global $table_header;
    
    	foreach ( $table['data'][0] as $col_idx => $cell_value ) {
    		$table_header[ $col_idx ] = $cell_value;
    	}
    
    	return $table;
    
    }, 10, 3 );
    
    add_filter( 'tablepress_cell_tag_attributes', function ( $tag_attributes, $table_id, $cell_content, $row_idx, $col_idx, $colspan_row, $rowspan_col ) {
    
    	global $table_header;
    
    	if ( $row_idx === 1 ) {
    		return $tag_attributes;
    	}
    
    	$attribute_value = $table_header[ $col_idx - 1 ];
    
    	return [ 'data-label' => $attribute_value ];
    
    }, 10, 7 );
    • This reply was modified 2 years, 4 months ago by datverse.
    Thread Starter datverse

    (@datverse)

    Hi,
    The full code should be:

    add_filter( 'tablepress_table_render_data', function ( $table, $orig_table, $render_options ) {
    
    	global $table_header;
    
    	foreach ( $table['data'][0] as $col_idx => $cell_value ) {
    		$table_header[ $col_idx ] = $cell_value;
    	}
    
    	return $table;
    
    }, 10, 3 );
    
    add_filter( 'tablepress_cell_tag_attributes', function ( $tag_attributes, $table_id, $cell_content, $row_idx, $col_idx, $colspan_row, $rowspan_col ) {
    
    	global $table_header;
    
    	if ( $row_idx === 1 ) {
    		return $tag_attributes;
    	}
    
    	$attribute_value = $table_header[ $col_idx - 1 ];
    
    	$tag_attributes += [ 'data-label' => $attribute_value ];
    
    	return $tag_attributes;
    
    }, 10, 7 );

    Thanks ??

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    good to see that you found a solution! You could maybe add two optimizations, to make the array handling faster and more robust:

    add_filter( 'tablepress_table_render_data', function ( $table, $orig_table, $render_options ) {
    	global $table_header;
    	$table_header = $table['data'][0];
    	return $table;
    }, 10, 3 );
    
    add_filter( 'tablepress_cell_tag_attributes', function ( $tag_attributes, $table_id, $cell_content, $row_idx, $col_idx, $colspan_row, $rowspan_col ) {
    	global $table_header;
    
    	if ( $row_idx === 1 ) {
    		return $tag_attributes;
    	}
    
    	$attribute_value = $table_header[ $col_idx - 1 ];
    	$tag_attributes['data-label'] = $attribute_value;
    
    	return $tag_attributes;
    }, 10, 7 );

    Best wishes,
    Tobias

    Thread Starter datverse

    (@datverse)

    Awesome!

    Thanks ??????

    • This reply was modified 2 years, 4 months ago by datverse.
    Thread Starter datverse

    (@datverse)

    Awesome!

    Thanks ??

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    no problem, you are very welcome! ?? Good to hear that this helped!

    Best wishes,
    Tobias
    ?
    P.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Add custom data-attribute to th, td’ is closed to new replies.