Redraw datatables after using ajax to refresh the table content?

前端 未结 9 626
遇见更好的自我
遇见更好的自我 2021-01-30 04:22

I am using Datatables and have a button on the page that refreshes the table using AJAX. To be clear the table isn\'t using an ajax source of data, we are just using ajax to ref

9条回答
  •  太阳男子
    2021-01-30 05:00

    This is how I feed my table with data retrieved by ajax (not sure if this is the best practice tough, but it feels intuitive and works well):

    /* initialise table */
    oTable1 = $( '.tables table' ).dataTable
    ( {
        'sPaginationType': 'full_numbers',
        'bLengthChange': false,
        'aaData': [],
        'aoColumns': [{"sTitle": "Tables"}],
        'bAutoWidth': true
    } );
    
    
     /*retrieve data*/
    function getArr( conf_csv_path )
    {
        $.ajax
        ({
            url  : 'my_url'
            success  : function( obj ) 
            {
                update_table( obj );
            }
        });
    }
    
    
    /* build table data */
    function update_table( arr )
    {        
        oTable1.fnClearTable();
        for ( input in arr )
        {
            oTable1.fnAddData( [ arr[input] );
        }                                
    }
    

提交回复
热议问题