Wordpress wp_ajax使用UI-jquery-dialog返回0面向对象的插件

时间:2014-09-11 15:05:23

标签: jquery ajax wordpress-plugin jquery-ui-dialog

我正在尝试使用带有ui-jquery-dialog的对话框中的ajax触发一个动作,但是wp_ajax它总是返回cero,我的插件是所有面向对象的,我找到了信息但仍然没有工作。

我的代码

 public function wpsgq_plugin_edit(){

    echo "1";
    die();
}


public function wpsgq_admin_options_page() {

    wp_enqueue_script('jquery');
    wp_enqueue_script('jquery-ui-core');
    wp_enqueue_script('jquery-ui-dialog');
    wp_enqueue_style('wp-jquery-ui-dialog');

    $wpdb = DB::getInstance();
    $table_name = $this->prefix . 'plugin';
    $rows = $wpdb->get_results( "SELECT * FROM $table_name" );
    ?>
   <div class="wrap">
        <div id="users-contain" class="ui-widget">
            <h2>My Information in DB</h2>
            <table id="users" class="ui-widget ui-widget-content" >
                <thead>
                    <tr class="ui-widget-header ">
                        <th>ID</th>
                        <th>Time</th>
                        <th>Name</th>
                        <th>Text</th>
                        <th>URL</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr class="ui-widget-header ">
                        <th>ID</th>
                        <th>Time</th>
                        <th>Name</th>
                        <th>Text</th>
                        <th>URL</th>
                        <th>--</th>
                    </tr>
                </tfoot>
                <tbody>
                <?php 
                    $i=1;
                    foreach($rows as $row){

                        echo "<tr>";
                        echo    "<td class='check-column' scope='row'>".$row->id."</td>";
                        echo    "<td class='column-columnname'>".$row->time."</td>";
                        echo    "<td class='column-columnname'>".$row->name."</td>";
                        echo    "<td class='column-columnname'>".$row->text."</td>";
                        echo    "<td class='column-columnname'>".$row->url."</td>";
                        echo    "<td class='column-columnname'>
                                    <a id='edit_$row->id' name='edit_$row->id' onclick='show_dialog($row->id)'>Edit</a>
                                    <div id='edit-div_$row->id' name='edit-div_$row->id' style='display: none;'>";
                                        $item = $row->id;
                                        ${"view_".$item} = new WPSGQViews;
                                        echo ${"view_".$item}->wpsgq_plugin_view_edit($item);       
                        echo        "</div>
                                </td>";
                        echo "</tr>";
                        $i++;
                    }
                ?>
                </tbody>
            </table>
            <?php
            add_action( 'wp_ajax_nopriv_wpsgq_plugin_edit', array( $this, 'wpsgq_plugin_edit' ) ); 
            add_action( 'wp_ajax_wpsgq_plugin_edit', array( $this, 'wpsgq_plugin_edit' ) );     
            ?>
            <script type="text/javascript">

                function show_dialog(id){
                    jQuery(function($) {
                        var info = $("#edit-div_"+id);

                        info.dialog({                   
                            'dialogClass'   : 'wp-dialog',           
                            'modal'         : true,
                            'autoOpen'      : true, 
                            'closeOnEscape' : true,
                            'height'        : 420,
                            'width'         : 500,      
                            'buttons'       : {
                                "Edit": function() {

                                    $.ajax({
                                        type: "POST",
                                        async: true,
                                        url: ajaxurl,
                                        data: {
                                            action  : 'wpsgq_plugin_edit',
                                            param   : $("#form-edit_"+id).serialize()  
                                        },

                                        success: function(data){

                                            var $newdiv = $('<div/>');
                                            $("#message").append($newdiv);

                                            $newdiv.html(data);

                                            if(data==1){

                                            }else{
                                                $newdiv.html(data);

                                                $newdiv.dialog({ modal: true, 
                                                    buttons: { 
                                                        Ok: function(){                                                       
                                                            $( this ).dialog("destroy"); 
                                                            $( this ).remove();                                             
                                                        }
                                                    },
                                                    close: function() {
                                                        $( this ).dialog("destroy"); 
                                                        $( this ).remove(); 
                                                    }
                                                });
                                            }
                                        },
                                        error: function(data){
                                            $("#message").html("Error: "+data);
                                        }
                                    });
                                },
                                "Cancel": function() {
                                    $(this).dialog('close');
                                }
                            }
                        });
                    });
                } 
            </script>
        </div>         
    </div>
    <?php
}

您可以看到的任何错误都会受到赞赏

2 个答案:

答案 0 :(得分:0)

将这两行移出wpsgq_admin_options_page()函数调用并进入构造函数...

        add_action( 'wp_ajax_nopriv_wpsgq_plugin_edit', array( $this, 'wpsgq_plugin_edit' ) ); 
        add_action( 'wp_ajax_wpsgq_plugin_edit', array( $this, 'wpsgq_plugin_edit' ) );     

答案 1 :(得分:0)

您必须使用Jquery ui完整版 或者不需要更多的库 &#34; ./插件&#34 ;, &#34; ./按钮&#34 ;, &#34; ./拖动&#34 ;, &#34; ./小鼠&#34 ;, &#34; ./位置&#34 ;, &#34; ./可调整大小&#34; 对话框不能仅与核心一起使用。

您可以找到图书馆需要的原因 通过以下链接 https://github.com/jquery/jquery-ui/blob/master/ui/dialog.js