2013-05-15 118 views
0

我想在我的WordPress插件中使用tablesorter。Tablesorter不能在Wordpress插件中工作

这是我的表看起来像

<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目录。但没有任何反应。任何想法出了什么问题?

回答

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')); 

也许这将帮助你的问题出更好一点排序。

+0

也把内容回答 – SSP