• Resolved jorgeantolin7

    (@jorgeantolin7)


    Hello, I have encountered this problem and I have been trying a long time how to solve it but I am not able to. I have a Table that I created with the TablePress plug in and it imports data automatically using the Automatic Periodic Table Import Extension.

    I need to hide (or remove) the rows of this table that satisfy that their column “Price” value is less than 3. How can I accomplish this?

    I know I can remove them manually but I want to make this process automatic.
    I have tried to hide these rows using the Table’s Shortcut
    [table id=1 hide_rows=”1,2,3″ /] but I do not know how to select the rows that satisfy this condition.

    I have tried to add some functions to my functions.php file but I come with the same problem of not being able to select the rows in a loop…

    Thanks in advance!

Viewing 15 replies - 1 through 15 (of 21 total)
  • Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    thanks for your post, and sorry for the trouble.

    The TablePress Extension from http://tablepress.org/extensions/row-filter/ might help here, but unfortunately, it can not do < comparisons.
    (If your prices are limited to a few values, like 0.99, 1.99, 2.99, this wouldn’t be a problem though. If they can take arbitrary values between 0 and 3, you would have to adjust the code.)

    Regards,
    Tobias

    Thread Starter jorgeantolin7

    (@jorgeantolin7)

    Hi Tobias,

    Thank you very much for your answer. I have tried that TablePress Extension already but unfortunately it does not work for my case since this “Price” column can take arbitrary values.

    Is it possible to do it modifying the Shortcode as you explained here: https://tablepress.org/faq/documentation-shortcode-table/ ? My problem is to get the list of rows that don’t satisfy my condition. Do you know if I can get those values with PHP or JS? Maybe adding something to the “Custom Commands” when editing the table in WordPress?

    Best regards,

    Jorge.

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    just modifying the Shortcode will not be enough, as there is no logic yet that does the comparison. Your best chance is indeed using PHP code, and I would recommend to adapt the code of the Row Filter Extension. That shows how you can use filter hooks to get the current set of rows. Then, you could loop through them and remove everything where the condition is not fulfilled.

    Regards,
    Tobias

    Thread Starter jorgeantolin7

    (@jorgeantolin7)

    Hi Tobias,

    Thanks for your answer. I am having a look at the PHP code of the Row Filter Extension but I do not understand how you iterate through each row of a table.

    Let’s say I have a table in a variable $table such that if I do “return $table”, then in my WordPress page I get the table. Is there any PHP method that allows me to get the rows of the $table and the value of a certain column of these rows?

    Best regards,
    Jorge.

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    yes, that would be the tablepress_table_render_data filter hook, also used in that Extension. It get’s passed the table data (as a two-dimensional array) and meta data. You would then check if this is the correct table and loop through that array, i.e. remove undesired rows.

    Regards,
    Tobias

    Thread Starter jorgeantolin7

    (@jorgeantolin7)

    Hi Tobias,

    This is how my code in my “functions.php” file looks like now:

    function filter_rows($table){
    	// Rows that do not match the filter: Column Price < 3
    	$hidden_rows = array();
    	// Here I should get the hidde_rows array...
    	foreach ( $hidden_rows as $row_idx ) {
    			unset( $table['data'][ $row_idx ] );
    			unset( $table['visibility']['rows'][ $row_idx ] );
    		}
    
    	$table['data'] = array_merge( $table['data'] );
    	$table['visibility']['rows'] = array_merge( $table['visibility']['rows'] );
    
    	return $table;
    }
    
    function filtering_rows_function($atts) {
    	extract(shortcode_atts(array('id' => 1,), $atts));
    	$attributes['id'] = preg_replace( '/[^a-zA-Z0-9_-]/', '', $id );
    	$table = tablepress_get_table( $attributes );
    	return filter_rows($table);
    }
    
    function register_shortcodes(){
    	add_shortcode('filtering_rows', 'filtering_rows_function');
    }
    
    add_action('init', 'register_shortcodes');

    So should I add this line too?
    add_filter( 'tablepress_table_raw_render_data', array( __CLASS__, 'filter_rows' ), 10, 2 );

    I am struggling to get the $hidden_rows array in my code…

    Best regards and thank you so much for your answer,

    Jorge.

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    yes, you should do this on that filter hook instead. Filtering the output of tablepress_get_table() will not work, as that’s already HTML code.

    Regards,
    Tobias

    Thread Starter jorgeantolin7

    (@jorgeantolin7)

    Hi,

    Okay thanks, and how should I continue in the part of my code where I should get the hidded_rows array..? Can you help me with the foreach loop? I am really new to PHP.

    Thank you and best regards,
    Jorge.

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    the table data is an array, so that you can use foreach(). Then, inside the loop check for your condition.

    Unfortunately, for time reasons I can not help with specific details here. You might want to take a look at other PHP code in TablePress (and especially other TablePress Extensions!) as well, as such foreach() loops appear in several places.

    Regards,
    Tobias

    Thread Starter jorgeantolin7

    (@jorgeantolin7)

    Hi,

    Okay I understand it now, my problem is that $table is already HTML code for me. I have tried to include this, as you told me:

    add_filter( 'tablepress_table_raw_render_data', array( __CLASS__, 'filter_rows' ), 10, 2 );

    But it is not working. Where should I include it in my code? And what do I need to be able to include it, do I need any specific extension or import?

    I do not understand how can I get my $table as a 2 dimensional array.

    Thanks for your answer and regards,
    Jorge.

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi Jorge,

    using

    add_filter( 'tablepress_table_raw_render_data', array( __CLASS__, 'filter_rows' ), 10, 2 );
    

    is correct. Then, your function filter_rows() will get the $table variable, which has a two-dimensional array with the data in its $table['data'] property.
    That’s what you can use to filter.
    Finally, just return the modified $table variable.

    Regards,
    Tobias

    Thread Starter jorgeantolin7

    (@jorgeantolin7)

    Hi Tobias,

    I am sure I am making some mistake because nothing is showing after I wrote this following code in the ‘functions.php’ file of my WordPress page:

    add_filter('tablepress_table_raw_render_data', array( __CLASS__, 'filter_rows' ), 10, 1 );
    
    function filter_rows($table){
    	foreach ($table['data'] as $row){
    		echo $row['Price'];
    	}
    	
    	return;
    }
    
    function filtering_rows_function($atts) {
    	extract(shortcode_atts(array('id' => 1,), $atts));
    	$attributes['id'] = preg_replace( '/[^a-zA-Z0-9_-]/', '', $id );
    	$table = tablepress_get_table( $attributes );
    	return filter_rows($table);
    }
    
    function register_shortcodes(){
    	add_shortcode('filtering_rows', 'filtering_rows_function');
    }
    
    add_action('init', 'register_shortcodes');
    

    Can you spot my error? And I don’t understand where do the ‘tablepress_table_raw_render_data’ and array( __CLASS__ come from exactly.

    Thanks and sorry for too much messages,

    Jorge.

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    the first is a filter hook defined at https://github.com/TobiasBg/TablePress/blob/183ea0b8319b552aca2e4dc81cfe32025d93dccb/classes/class-render.php#L119

    The array( __CLASS__ is actually wrong here.

    You only need

    add_filter('tablepress_table_raw_render_data', 'jorge_filter_tablepress_table', 10, 2 );
    
    function jorge_filter_tablepress_table( $table, $render_options ) {
    	// Let this code only apply to table 123.
    	if ( '123' !== $table['id'] ) {
    		return $table;
    	}
    	
    	// Do something with the table data.
    	foreach ( $table['data'] as $row ) {
    		// $row[2] has the data of the third cell.
    		// Don't use echo here, because you have to return the modified data.
    	}
    	
    	return $table;
    }

    Regards,
    Tobias

    Thread Starter jorgeantolin7

    (@jorgeantolin7)

    Hi Tobias,

    I understand the filter hook now much better, thanks. Now I wanted to try if I am able to hide rows, I tried to hide all rows from the table but I got an error. This is my code:

    add_filter('tablepress_table_raw_render_data', 'jorge_filter_tablepress_table', 10, 2 );
    
    function jorge_filter_tablepress_table( $table, $render_options ) {
    	// Do something with the table data.
    	$hidden_rows = array();
    	foreach ( $table['data'] as $row ) {
    		//if ($row[3] < 2){
    			$hidden_rows[] = $row;
    		//}
    	}
    	
    	// Remove the rows that shall be hidden from table data and table visibility.
    	foreach ( $hidden_rows as $row_idx ) {
    		unset( $table['data'][ $row_idx ] );
    		unset( $table['visibility']['rows'][ $row_idx ] );
    	}
    	// Reset array keys.
    	$table['data'] = array_merge( $table['data'] );
    	$table['visibility']['rows'] = array_merge( $table['visibility']['rows'] );
    
    	return $table;
    }
    
    function filtering_rows_function($atts) {
    	extract(shortcode_atts(array('id' => 1,), $atts));
    	$attributes['id'] = preg_replace( '/[^a-zA-Z0-9_-]/', '', $id );
    	$table = tablepress_get_table( $attributes );
    	return jorge_filter_tablepress_table($table);
    }

    My problem is in the $table['visibility']['rows'] = array_merge( $table['visibility']['rows'] ) part I think, (if I just comment this last row then the whole table is shown). Do you know how can I fix this?

    Thank you so much and best regards,

    Jorge.

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    the problem is what you are storing in $hidden_rows. Instead of the actual row data (from $row), you need to store the row number ($row_idx), which you then also need in the foreach() loop

    foreach ( $table['data'] as $row_idx => $row ) {
    

    In general, I can only recommend to test and debug this step by step, e.g. using var_dump() so that you see what the variables are doing.

    Regards,
    Tobias

Viewing 15 replies - 1 through 15 (of 21 total)

The topic ‘Hide rows if they satisfy a condition’ is closed to new replies.