wordpress中具有Tablesorter的列中的数字范围


Range of numbers in a column with Tablesorter in wordpress

我打算使用Tablesorter插件对wordpress主题中的表进行排序。在我的一个表列中,我有一个值范围(即8-10、5-12等)。在这一列上,排序无效。我在stackoverflow中看到了解决方案示例。我试着实现这个,但没有成功。一般来说,排序在我的表上有效,但它在该列上给出了不正确的结果。

这是我的头文件,其中包括脚本:

 <script type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/js/jquery-latest.js"></script>
<script type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/js/jquery.tablesorter.js"></script>
<script type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/js/jquery.tablesorter.widgets.js"></script>

这是我的桌子

 <table cellspacing='0' id="posts-table"> <!-- cellspacing='0' is important, must stay -->
            <!-- Table Header -->
            <thead>
            <tr>
                <th>Col1</th>
                <th>Col2</th>
                <th>Col3</th>
                <th>Col4</th>
                <th>Col5</th>
                <th>Col6</th>
            </tr>
            </thead>
            <!-- Table Header -->
            <!-- Table Body -->
            <?php /* Start the Loop */
            $i = 1;?>
            <?php while ( have_posts() ) : the_post(); ?>
                <?php
                $val1 = get_post_meta($post->ID, 'x-value', true);
                $post_categories = get_the_category();
                $categories_by_id = array();
                foreach ( $post_categories as $category ) {
                    $categories_by_id[$category->cat_ID] = $category;
                }
                foreach ( $post_categories as $category ) {
                    unset( $categories_by_id[$category->category_parent] );
                }
                    echo '<tr>';
                        echo '<td class="align-center">'; echo $i++; echo'</td>';
                        echo '<td>'; echo end($categories_by_id)->cat_name; echo'</td>';
                        echo '<td>'; echo the_title(); echo'</td>';
                        echo '<td class="align-center">'; echo $val1; echo'</td>';
                        echo '<td class="align-center">'; echo $val1; echo'</td>';
                        echo '<td class="align-center">'; echo $val1; echo'</td>';
                    echo'</tr>';
                ?>
            <?php endwhile; ?>
            <!-- Table Body -->
        </table>

这是表分类器的初始化

$(document).ready(function()
{
    $("#posts-table").tablesorter({
        theme: 'blue',
        sortList: [[1,0]],
        widthFixed: true,
        widgets: ['zebra', 'filter'],
        widgetOptions: {
            filter_functions: {
                1 : function(e, n, f, i) {
                    var parts = e.split('-'),
                        val = parseFloat(f),
                        min = parseFloat(parts[0]),
                        max = parseFloat(parts[1] || 999); // default max = 999
                    return val >= min && val <= max;
                }
            },
            filter_formatter     : {
                1 : function($cell, indx){
                    return $.tablesorter.filterFormatter.uiSlider( $cell, indx, {
                        values : 0,
                        min : 0,
                        max : 60,
                        delayed : false,
                        exactMatch: false,
                        valueToHeader : false
                    });
                }
            }
        }
    });
}

);

有什么想法为什么不起作用吗?如果这听起来很愚蠢,我很抱歉。干杯:)

filter_formatter代码与单独的筛选器格式化程序文件一起使用。我认为这没有必要,或者这可能是代码不适合您的原因。

删除该代码块,看看它是否解决了您的排序问题(演示):

$(function () {
    $('table').tablesorter({
        theme: 'blue',
        sortList: [[1, 0]],
        widthFixed: true,
        widgets: ['zebra', 'filter'],
        widgetOptions: {
            filter_functions: {
                1: function (e, n, f, i) {
                    var parts = e.split('-'),
                        val = parseFloat(f),
                        min = parseFloat(parts[0]),
                        max = parseFloat(parts[1] || 999); // default max = 999
                    return val >= min && val <= max;
                }
            }
        }
    });
});