Okay
  Public Ticket #1031923
Is it possible to return a wpdatatable by ajax?
Closed

Comments

  •  2
    Patrick started the conversation

    Hi,

    Is it possible to return a wpdatatable by ajax? So far I can't get t to work.

    It seems to bring back the contents as I can inspect it in the html, but the table_id is set to "display:none" and if I remove "display:none" it will display but it doesn't have the formatting or functionality, it's just a plain table.

    See my code below

    // my ajax function
    add_action('wp_ajax_my_wpdatatable_shortcode', 'my_wpdatatable_shortcode'); // add action for logged users
    add_action( 'wp_ajax_nopriv_my_wpdatatable_shortcode', 'my_wpdatatable_shortcode' ); // add action for unlogged users

    function my_wpdatatable_shortcode() {
        $result = do_shortcode( '[wpdatatable id=11]' );
        echo $result;
        wp_die();
    }

    // my wordpress template and javascript
    <?php /** * The template used for displaying page content in page.php * * @package Theme Meme */ ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?>>
        <div style="text-align: center;margin-top: 80px" class="tpn_search_results_gif hide">
            <img style="text-align: center" src="<?php echo home_url( '/' ).'wp-content/plugins/wp-tb-post-add/src/templates/images/loader_x.gif'; ?>" />
            <p>Loading..please wait</p>
        </div>
        <div class="clearfix entry-content" style="">
        </div>
    </article>
    <script>
        var tpn_security = "<?php echo wp_create_nonce("tpn_lists_nonce") ?>";
        jQuery(document).ready(function($){

            function tko_tpn_get_page_contents(search_list,elem,is_first_load){
                var search_list = search_list;
                var is_first_load = is_first_load;
                var gif_result = '#tpn_search_results_gif';
                var data = {
                    action: "my_wpdatatable_shortcode",
                    security: tpn_security
                };
                jQuery.ajax({url: ajaxurl,
                    type: "POST",
                    data: data,
                    beforeSend: function(msg){
                        jQuery(".entry-content").addClass('hide');
                        jQuery(gif_result).removeClass('hide');
                        jQuery('.tpn_search_results_gif').removeClass('hide');
                        jQuery(".entry-content").html("");
                    },
                    success: function(msg){
                        //alert(msg);
                        console.log(msg);
                        jQuery(gif_result).addClass('hide');
                        jQuery('.tpn_search_results_gif').addClass('hide');
                        jQuery(".entry-content").html(msg);
                        jQuery(".entry-content").removeClass('hide');
                    } ,
                    error:  function(msg) {
                        console.log(msg);
                        jQuery('.tpn_search_results_gif').addClass('hide');
                        jQuery(gif_result).addClass('hide');
                        jQuery(".entry-content").html('<br><br><br><br><p style="text-align: center">Nothing found that matched your criteria</p>');
                        jQuery(".entry-content").removeClass('hide');
                        return false;
                    }
                });
            }

            $('#tpn_toolbar_btns .btn-primary').click(function(){
                //var goto = window.location.origin+'/'+$(this).data('url')+'/';
                //location.href = goto;
                tko_tpn_get_page_contents($(this).data('url'),this,true);
            });

        });
    </script>

    Any ideas

    Thanks

  • [deleted] replied

    Hi Patrick,

    Please note that customization questions like this one are not covered in the included support for the plugin. Included support covers help with bugs and general inquiries for the plugin features. As you're developing some custom solution if you need our assistance we can offer you our paid customization service.

    Thank you for understanding.

  •  2
    Patrick replied

    Hi Milos,

    That's a shame becuase the tables are taking a long time to load and the ajax solution at least allows me to pop up a loading gif while the content is being prepared.

    Basically all I want to do is to display a loading gif while the page and the table is loading. Are there any filters that could help me do this without using ajax?

    Finally, could you provide me with a quote to customise this ajax functionality?

    Thanks

  • [deleted] replied

    Hi Patrick,

    Did you try to use MySQL tables with Server-side processing enabled? When you enable server side table will be loaded by Ajax. Will be much faster if you are working with large dataset. Also there is a loading spinning (loading) icon until data is loaded. You can take a look at this documentation how to use MySQL tables with Server-side processing - Link. And here you can read more about Server-side processing itself - Link.

    Also if you are creating tables from Excel, CSV or Google Spreadsheet you can create Manual table using second option from the constructor. All tables that are created with wpDataTables constructor are Manual and by default Server-side processing is enabled for that tables.