Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including jQuery UI Sortable in WordPress admin page

I'm working on a plugin and encountered a problem where trying to make use of the jQuery UI Sortable. I followed the instruction as in Codex but issue remained. The jQuery UI sortable does not function and Firebug says TypeError: jQuery(...).sortable is not a function.

I'm running on WordPress 3.6 and the code is:

<?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
}
like image 668
Bloorchi Avatar asked Oct 22 '13 04:10

Bloorchi


2 Answers

Two things, you have this inverted:

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

And you need to always run jQuery like this:

<script type="text/javascript">
jQuery(document).ready( function($) {
    $('table#test tbody').sortable();
});
</script>
like image 189
brasofilo Avatar answered Nov 10 '22 11:11

brasofilo


her is a simple tut for creating sortable table on wordpress.

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

and for back end, try this one.

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

like image 23
varadha Avatar answered Nov 10 '22 12:11

varadha