1

我想在我的 wordpress 插件中使用 tablesorter。

这就是我的桌子的样子

    <table class="table table-condensed table-striped" style="font-size:80%;" id="table_id">
       <thead align="left">
         <tr>
        <th>col1</th>
        <th>col2</th>
            <th style="min-width:6em;">col3</th>
            <th style="min-width:4em";>col4</th>
         </tr>
      </thead>
   <tbody> 
   </tbody> 
</table>

表格行稍后会动态添加。

在我使用的 PHP 文件中

  wp_enqueue_script('tablesorter', plugin_dir_url( __FILE__ ) . 'js/jquery.tablesorter.js', array('jquery172'));

在我使用的 javascript 文件中

  $("#table_id").tablesorter(); 

我将文件 jquery.tablesorter.js 复制到我的 js 目录中。但什么也没有发生。知道出了什么问题吗?

4

1 回答 1

0

您是否为 wordpress 尝试过这个特定的帮助函数以使其工作?

http://webjawns.com/2009/11/jquery-tablesorter-helper-function-for-wordpress/

/**
 * Make all tables with the specified identifier sortable using the jQuery Tablesorter
plugin.
 *
 * @uses wp_enqueue_script() To enqueue the jQuery Tablesorter script.
 *
 * @param string $identifier jQuery selector (.sortable, #element_id, table, etc.)
 * @param array $args Default is empty string. Array of arguments to pass to
 $.tablesorter()
 * @return void
 */
function make_table_sortable($identifier, $args = '') {
global $sortable_tables;

if ( in_array($identifier, (array)$sortable_tables) )
    return;

wp_enqueue_script('jquery-tablesorter');

$sortable_tables[] = array(
    'identifier' => $identifier,
    'args'       => $args
);

add_action('admin_print_footer_scripts', '_make_table_sortable');
}

function _make_table_sortable() {
global $sortable_tables;

if ( count( (array)$sortable_tables ) <= 0 )
    return;
?>
<script type="text/javascript">
<?php
foreach ($sortable_tables as $sortable_table) {
    if ( !is_array($sortable_table['args']) ) {
        $arguments = '';
    } else {
        $arguments = '{';

        $args_count = sizeof($sortable_table['args']);
        $i = 0;
        foreach ($sortable_table['args'] as $k => $v) {
            $arguments .= $k . ': ' . $v;
            if (++$i != $args_count) $arguments .= ', ';
        }

        $arguments .= '}';
    }
?>
$('<?php echo esc_js($sortable_table['identifier']); ?>').tablesorter(<?php 
echo esc_js($arguments); ?>);
<?php } ?>
</script>
<?php
}

下面举例说明 make_table_sortable() 函数的用法:

// Sort tables with class 'sortable', first and second columns in ascending 
// order with first taking precedence.
make_table_sortable('.sortable', array('sortList' => '[[0,0], [1,0]]'));

// Sort table with ID 'sortable', first column in ascending order. Turn debugging on.
make_table_sortable('#sortable', array('sortList' => '[[1,0]]', 'debug' => 'true'));

也许这将帮助您更好地解决问题。

于 2013-10-10T11:25:48.747 回答