包括 jQuery UI 可排序在 WordPress 管理页面



我正在开发一个插件,在尝试使用jQuery UI Sortable时遇到了问题。我按照法典中的说明进行操作,但问题仍然存在。jQuery UI可排序不起作用,Firebug说TypeError: jQuery(...).sortable is not a function

我在WordPress 3.6上运行,代码是:

<?php
/*
Plugin Name: Name
Description: Description
Version: 0.1
Author: Bloorchi
*/
add_action( 'admin_menu', 'my_plugin_admin_menu' );
function my_plugin_admin_menu() {
    add_action('admin_print_scripts-' . $page_hook_suffix, 'my_plugin_admin_scripts');
    $page_hook_suffix = add_submenu_page( 'edit.php', 'My Plugin', 'My Plugin', 'manage_options', 'my_plugin-options', 'my_plugin_manage_menu' );
}
function my_plugin_admin_scripts() {
    wp_enqueue_script( 'jquery-ui-sortable' );
}
function my_plugin_manage_menu() {
?>
<table id="test">
    <tbody>
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
        <tr>
            <td>3</td>
            <td>4</td>
        </tr>
        <tr>
            <td>5</td>
            <td>6</td>
        </tr>  
    <tbody>    
</table>
<script>
    jQuery('table#test tbody').sortable();
</script>
<?php
}

两件事,你颠倒了:

$suffix = add_submenu_page( 
    'edit.php', 
    'My Plugin', 
    'My Plugin', 
    'manage_options', 
    'my_plugin-options', 
    'my_plugin_manage_menu' 
);
add_action( "admin_print_scripts-$suffix", 'my_plugin_admin_scripts');

你需要始终像这样运行jQuery:

<script type="text/javascript">
jQuery(document).ready( function($) {
    $('table#test tbody').sortable();
});
</script>

她是一个简单的tut,用于在WordPress上创建可排序的表格。

http://kvcodes.com/2013/12/create-sortable-tables-in-wordpress-front-end/

对于后端,请尝试此。

http://kvcodes.com/2014/02/sortable-data-table-wordpress-frontback-end/

尝试禁用所有插件,然后重试。我遇到了类似的问题,我发现问题出在其中一个插件中

最新更新