扩展6: 只显示表格中符合筛选条件的行

一些使用WordPress表格插件WP-Table Reloaded的用户可能会想要实现下面的想法:您有一个相当大的表格,维护着某些东西的列表(比如产品)。但是在某些页面上,您不希望将表格全部都显示出来,而只是想将某一列里那些符合某个关键词的行显示出来。

在这种情况下,您就可以使用下面的筛选扩展了。该扩展在简码中加入另一个参数 filter ,可以使用一个或多个(使用逻辑运算符连接的)关键词对表格内容进行筛选:

[table id=1 filter="word1&&word2" /]

然后,只有那些符合条件的行才会显示出来,这些行拥有可以精确匹配设定的关键词的列。在上面的例子中,只有一列精确匹配值“word1”并且还有一列精确匹配“word2”的行才会显示出来。

要使用 WP-Table Reloaded 扩展,您应该先阅读扩展页面,了解相关方法和说明,并按照相关要求进行操作。您还能从该页面找到实现其它功能的扩展的链接。

下面就是该扩展的代码:

/**
 * 仅显示匹配“filter”参数值/包含的逻辑表达式的行
 * Show only rows that match the "filter" parameter value/contained logical expression
 */
function wp_table_reloaded_filter_rows( $output_options, $table_id, $table ) {
    // early exit if no "filter" parameter given
    if ( empty( $output_options['filter'] ) )
        return $output_options;

    $filter = $output_options['filter']; // from the Shortcode parameter "filter"

    // && is the passed value for &&
    if ( false !== strpos( $filter, '&&' ) ) {
        $compare = 'and';
        $filter = explode( '&&', $filter );
    } elseif ( false !== strpos( $filter, '||' ) ) {
        $compare = 'or';
        $filter = explode( '||', $filter );
    } else {
        $compare = 'none'; // single filter word
        $filter = array( $filter );
    }

    foreach ( $filter as $key => $string ) {
        // remove HTML entities and turn them into characters, escape/slash other characters
        $filter[ $key ] = addslashes( wp_specialchars_decode( $string, ENT_QUOTES, false, true ) );
    }

    $row_match = false;
    foreach ( $table['data'] as $row_idx => $row ) {
        if ( 0 == $row_idx && $output_options['first_row_th'] )
            continue;

        $found = array();
        foreach ( $filter as $key => $string ) {
            $found[ $key ] = in_array( $string, $row );
        }

        switch ( $compare ) {
            case 'none':
            case 'or':
                if ( in_array( true, $found ) ) // at least one word was found / only filter word was found
                    $row_match = true;
                else
                    $output_options['hide_rows'][] = (string)$row_idx;
                break;
            case 'and':
                if ( ! in_array( false, $found ) ) // if not (at least one word was *not* found) == all words were found
                    $row_match = true;
                else
                    $output_options['hide_rows'][] = (string)$row_idx;
                break;
        }
    }

    // if search term(s) was/were not found in any of the rows, all rows need to be hidden
    // but only if first row is used as table head
    if ( ! $row_match && $output_options['first_row_th'] ) {
        $row_idx = 0;
        $output_options['hide_rows'][] = (string)$row_idx;
    }

    return $output_options;
}

/**
 * Add "filter" as a valid parameter to the [table] Shortcode
 */
function wp_table_reloaded_shortcode_parameter_filter( $default_atts ) {
    $default_atts['filter'] = '';
    return $default_atts;
}

/**
 * Register necessary Plugin Filters
 */
add_filter( 'wp_table_reloaded_frontend_output_options', 'wp_table_reloaded_filter_rows', 10, 3 );
add_filter( 'wp_table_reloaded_shortcode_table_default_atts', 'wp_table_reloaded_shortcode_parameter_filter' );

只需要将这段代码复制到 “wp-table-reloaded-extensions.php”文件里,该文件可以根据扩展页面的使用说明来创建,在插件头声明之后而在PHP结束符号 ?>之前。如果您还没有激活这个新插件“WP- Table Reloaded Extensions”,激活即可。

作者在上面的代码中加入了一些注释,应该会比较容易理解。下面是他对其工作方式的解释:该扩展通过执行挂钩注入到 WP-Table Reloaded  代码中,注册一个新的简码参数 filter。这是必须的,因为实际的参数值需要随参数一起传递给筛选算法。该算法首先判断需要进行什么类型的筛选(“and”(与),“or”(或),还是只有一个词)。然后从搜索表达式中解析出关键词并搜索匹配的行。再执行条件检查,如果该行不满足条件,就在输出中将它标记为隐藏。其中会有少量的健壮性判断(比如,第一行需要区别对待,或者如果结果中所有行都是隐藏的就需要进行说明),不过该扩展基本上就是这样工作的了。

下面是几个简单的示例,向您展示3种可能的使用情况(请注意,在任何情况下都需要单元格内容精确匹配关键词,例如,如果您搜索 “test”,但是单元格内容是“testing”,这就不匹配!):

在行中搜索一个关键词:

[table id=1 filter="word1" /]

(如果没有任何一个单元格包含该内容,所有行都会被隐藏。)

搜索至少一个关键词:

[table id=1 filter="word1||word2" /]

(没有单元格包含“word1”或者 “word2”的行都会被隐藏。)

您可以使用 || 将所有需要的关键词组合起来。

搜索所有关键词:

[table id=1 filter="word1&&word2&&word3" /]

(如果行中没有单元格包含“word1”,同时也没有单元格包含“word2”,还没有单元格包含“word3”,则满足条件的所有行均被隐藏。)

你也可以使用 && 将所有需要的关键词组合起来。

重要提示:无法使用组合的逻辑运算,如

[table id=1 filter="word1||word2&&word3" /]

该简码将无法工作!

这样一来,您就可以只显示一个大表格中需要的那些部分了:-)

使用中如果有什么问题,请详细阅读相关文档,或者到讨论区相应板块寻求解答。©

本文发表于水景一页。永久链接:<https://cnzhx.net/blog/extension-6-showing-only-rows-that-match-a-filter/>。转载请保留此信息及相应链接。

2 条关于 “扩展6: 只显示表格中符合筛选条件的行” 的评论

时间过去太久,评论已关闭。
如果您有话要说,请到讨论区留言并给出此文章链接。
谢谢您的理解 :-)