WordPress.org

Ready to get started?Download WordPress

Forums

wp-table-reloaded: Search function (13 posts)

  1. joesmalley
    Member
    Posted 4 years ago #

    Hi Tobias,

    My website which uses your plugin requires to be able to search/filter the table rows. For instance a user could type in "red" and it shows only the rows which contain the word "red". Do you intend to add such functionality in the future? It would be very useful to me and I'm sure others.

    I have tried to add it for myself (should be a simple case of modifying the SQL query which produces the table data no?) but I can't find where your plugin puts the table data in my WordPress SQL database.

    Any help you could give would be much appreciated.

    Regards

    Joe

  2. joesmalley
    Member
    Posted 4 years ago #

    I should add, that if you can develop the search functionality for me - I will give you a suitable donation to cover your time.

  3. TobiasBg
    Member
    Posted 4 years ago #

    Hi,

    have you tried the search/filter function that comes with the DataTables JavaScript (the library that is also responsible for sorting)?
    It includes a great filtering mechanism, in real time.

    (You can also see it in action on the Demo Table on the WP-Table Reloaded website: http://tobias.baethge.com/wordpress-plugins/wp-table-reloaded-english/ )

    Regards,
    Tobias

  4. joesmalley
    Member
    Posted 4 years ago #

    Wow that is great, just what I am after! I hadn't seen those options. I will certainly be making a donation now :)

    I wonder if you can help me with one more thing. I am using the plugin to display a list of products in table form. I need the table rows to be clickable and a 'product detail' page appear containing the data from that row.

    This may be a step outside your plugin's feature set so I am hoping I can develop this myself.

    To achieve this my plan is to add a 'link' column and modify the PHP to add an HTML link here to another WordPress Page. The link can include the row ID using a GET variable in the URL, and my WordPress page template can read this ID and query the appropriate fields from the database, and show them.

    I'm sure I can achieve this, do you think this is the best way to do what I need? Also I still can't find where your plugin puts the table data into the SQL database? I will needs this for my 'product detail' page.

    Thanks

  5. TobiasBg
    Member
    Posted 4 years ago #

    Hi Joe!

    cool that you like the integrated Search feature, and thanks for your good will to donate!

    Regarding your idea:

    You can of course insert links into the table that link to your product detail pages. You just need to know the URL. Having them generated automatically is non-trivial though. I recommend creating a Shortcode that you just pass the ID of the product into and that will create the link for you.
    (You say that you want "the rows clickable". This could then be added with e.g. jQuery, so that a click on the row will trigger the click on the link. Should be only a few lines of code.)

    Now the probably problematic part: My plugin does not really store the tables in a format that makes them SQL readable.
    The table data, which is a two-dimensional array internally, is "serialized" to a string. And that string is stored in a "WordPress option" in the "wp_options" database table, using the WordPress API functions get_option() and update_option().
    Thus, there is no possibility to run SQL queries on the tables. You could only use SQL to get the complete table from that wp_options table. (I would not recommend that though, instead the mentioned API functions should be used.)

    Hope this helps!
    Tobias

  6. joesmalley
    Member
    Posted 3 years ago #

    I thought I'd follow up on this post. The website this function was for has been on hold, and I've just started work on it.

    To achieve what I was after has been very easy. Firsly the DataTables JavaScript does everything I want with regards to searching and sorting.

    Then to link to a particular row's data on it's own page. In render/render.class.php I modified line 247 like this:

    // JOE MOD
                // Old Code
                // $row_cells[] = "<{$tag}{$span_attr}{$class_attr}{$style_attr}>{$cell_content}</${tag}>";
                // New code
                if($col_idx == 0 && $row_idx != 0){
                    $row_cells[] = "<{$tag}{$span_attr}{$class_attr}{$style_attr}><a href='".get_bloginfo('url')."/our-wines/wine-detail/?id=".$row_idx."'>{$cell_content}</a></${tag}>";
                } else {
                    $row_cells[] = "<{$tag}{$span_attr}{$class_attr}{$style_attr}>{$cell_content}</${tag}>";
                }
    // END JOE MOD

    Then in my functions.php file I addded:

    function get_tablefield($tableid, $rowid, $fieldname){
    
        $tabledata = get_option('wp_table_reloaded_data_'.$tableid);
    
        $columncount = count($tabledata['data'][0]);
    
        for($i=0; $i < $columncount; $i++){
            if($tabledata['data'][0][$i] == $fieldname){
                return $tabledata['data'][$rowid][$i];
            }
        }
    
    }

    Then I created a new WordPress page with its own page template to display the data (this page URL must match the URL in the code I added to render/render.class.php). In the page template, I put this:

    <?php echo get_tablefield(1, $_GET['id'], "Name"); ?>

    replacing 1 with the table ID, and "Name" with the column name I wish to display.

    Hope this helps anyone who wants to achieve the same result.

  7. TobiasBg
    Member
    Posted 3 years ago #

    Hi,

    thanks for sharing this!

    (However, I do not recommend to edit the plugin PHP files, as you will lose those modifications when the plugin is updated.
    Instead, you should use the "wp_table_reloaded_cell_content" filter to edit the cell content.)

    Best wishes,
    Tobias

  8. joesmalley
    Member
    Posted 3 years ago #

    Hi Tobias,

    Can you explain how the filter would work?

    I tried creating this in my functions.php file but it doesn't run?

    function wp_table_reloaded_cell_content($cell_content, $table_id, $row_idx, $col_idx){
    
        if($table_id == 1 && $col_idx == 1 && $row_idx != 1){
            return "<a href='".get_bloginfo('url')."/our-wines/wine-detail/?id=".$row_idx."'>".$cell_content."</a>";
        } else {
            return $cell_content;
        }
    
    }
  9. TobiasBg
    Member
    Posted 3 years ago #

    Hi,

    I suggest that you take the Automatic URL converter as an example (http://tobias.baethge.com/2009/12/extension-1-url-to-link-conversion/).

    From that code, just change the function name (two occurances, but don't use "wp_table_reloaded_cell_content"), the parameter list (to what you have already) and the actual content of the function (to what you have already).
    In short: All you need to still do is rename your function (for clarity on what the function does and to not get duplicate function names) and hook the function to the actual filter (using add_filter).

    Best wishes,
    Tobias

  10. joesmalley
    Member
    Posted 3 years ago #

    Thanks, I've now done this, and added it all to a wp-table-reloaded-extensions.php file, with a hook to the filter. It now runs, but I don't have access to the whole parameter list ($cell_content, $table_id, $row_idx, $col_idx) - it seems only the '$cell_content' variable is passed. I need the other variables so I can add my link only to the first column. Is this possible?

    Here is my code:

    function wp_table_reloaded_customfunction($cell_content, $table_id, $row_idx, $col_idx){
    
        if($table_id == 1 && $col_idx == 1 && $row_idx != 1){
            return "<a href='".get_bloginfo('url')."/our-wines/wine-detail/?id=".$row_idx."'>".$cell_content."</a>";
        } else {
            return $cell_content;
        }
    
    }
    
    add_filter( 'wp_table_reloaded_cell_content', 'wp_table_reloaded_customfunction' );
  11. TobiasBg
    Member
    Posted 3 years ago #

    Hi,

    oh, sorry, forgot to mention one thing:
    You need to tell the add_filter function how many parameters you have. Just change your last line to

    add_filter( 'wp_table_reloaded_cell_content', 'wp_table_reloaded_customfunction', 10, 4 );

    The 10 is a default value for the priority of the filter, the 4 is the number of parameters.

    Best wishes,
    Tobias

  12. joesmalley
    Member
    Posted 3 years ago #

    Excellent, that's fixed it! And I can now update the plugin with my modification too. Great! Thanks

  13. joesmalley
    Member
    Posted 3 years ago #

    Here is some code I added to controller-frontend.php to allow pre-searching by a GET variable in the URL. Someone may find it useful:

    Line 564:

    if(isset($_GET['ts'])){
    $parameters['oSearch'] = '"oSearch": {"sSearch": "'.$_GET['ts'].'"}';
    }

    And access the pre-searched table like:
    http://www.website.com/wordpress/table-page/?ts=Search term

Topic Closed

This topic has been closed to new replies.

About this Topic