WordPress.org

Ready to get started?Download WordPress

Forums

WP-Table Reloaded
Main Search to pass keyword to datatables filtering search. (5 posts)

  1. Jaya
    Member
    Posted 3 years ago #

    Hello Tobias,

    I was wondering if there was a way to have the plugin's tables detect the search keyword(s) in the search page and filter the results in the tables accordingly?

    for example:
    If I search for "hat" , the result page in my case would be http://bit.ly/bIde4N

    Instead of the user having to manually retype the search keyword "hat" in each of the tables' filtering search (i have many), I'd like the search term to be typed/detected automatically in all of them.

    Is there a way around that? Thank you in advance for your response and great support!

    Regards,

  2. TobiasBg
    Member
    Plugin Author

    Posted 3 years ago #

    Hi,

    yes, that should be possible with some PHP code, as a WP-Table Reloaded Extension (see my website for details on that topic).

    The code is

    /**
     * DataTables Automatic Filtering
     * @author Tobias Baethge
     */
    
    /**
     * Register necessary Plugin Filters
     */
    add_filter( 'wp_table_reloaded_table_js_options', 'wp_table_reloaded_add_table_js_options', 10, 3 );
    add_filter( 'wp_table_reloaded_js_frontend_command', 'wp_table_reloaded_extend_js_frontend_command', 10, 7 );
    
    /**
     * Add "autofilter" from the WordPress search to the $js_options
     */
    function wp_table_reloaded_add_table_js_options( $js_options, $table_id, $output_options ) {
        $js_options['datatables_autofilter'] = esc_js( get_query_var( 's' ) ); // contains the search term from WordPress
        return $js_options;
    }
    
    /**
     * Add the necessary DataTables JavaScript commands for the autofiltering
     */
    function wp_table_reloaded_extend_js_frontend_command( $command, $table_id, $html_id, $tablesorter_script, $js_command, $parameters, $js_options ) {
        // early exit if no "autofilter" parameter given
        if ( empty( $js_options['datatables_autofilter'] ) )
            return $command;
    
        $autofilter_word = $js_options['autofilter']; // from the Shortcode parameter "autofilter"
    
        // append necessary JavaScript to the original command
        $command  = "var oTable_{$table_id} = {$command}\n";
        $command .= "\toTable_{$table_id}.fnFilter( '{$js_options['datatables_autofilter']}' );";
    
        return $command;
    }

    Note however, that I have not really tested this code. If it works, the search term from the WordPress search should automatically be used to perform a filtering in the table using the DataTables library.

    Best wishes,
    Tobias

  3. Jaya
    Member
    Posted 3 years ago #

    Hi Tobias,

    The extension worked partially. It does what is needed but the filter resets in seconds for some reason.

    Here is an example: http://bit.ly/cbdDoW

    Any ideas on what could be causing this? Thanks a lot!! :)

    Regards,

  4. TobiasBg
    Member
    Plugin Author

    Posted 3 years ago #

    Hi,

    unfortunately I don't :-(

    As far as I can see, the code is pretty much equal to the example at http://datatables.net/api#fnFilter

    As this seems to be a DataTables/JavaScript problem, I would recommend, that you ask in the DataTables forums at http://www.datatables.net/forums, if they have any ideas.

    Regards,
    Tobias

  5. Jaya
    Member
    Posted 3 years ago #

    Hi, Thank you for the appreciated help!

    I'm thinking if the execution of the filtering code happens after the page is fully loaded, it'd work. I'm going to look into it and see what result I can find. I will also leave this open and update it with the solution if i can find any, to help others who might need this.

    Have a great day!
    Regards,

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic