asp.net mvc - jQuery DataTable refreshing data - MVC View building table first, then trying to display data retrieved via ajax -
i'm working on mvc5 , load datatable iterating through model data, manually building table elements on document ready initialize table .datatable(). part works fine. now, allow user change data want display (via dropdown selection, change event fires , calls ajax controller method, returns json objects), new initialization of datatable doesn't seem want cooperate. nothing visible @ point.
here's table within view:
<table class="table table-bordered" id="accountrequeststatus" name="accountrequeststatus"> <thead> <tr> <th>col1</th> <th>col2</th> <th>col3</th> <th>col4</th> <th>col5</th> <th></th> </tr> </thead> <tbody> @foreach (var request in model.payload) { <tr class="registerrow" id="register@(request.col1)"> <td class="value registerrow">@request.col2</td> <td class="value registerrow">@request.col3</td> <td class="value registerrow">@request.col4</td> <td class="value registerrow">@request.emailaddress</td> <td>@html.actionlink("click me, bro", controllermethod, new { param1 = request.myparam1 })</td> </tr> } </tbody> </table>
document ready:
$(document).ready(function () { // setting filter button status selected value $('#accountrequeststatus').datatable();});
bam. works slick, , thank that!
now, when try reload grid single column, 'cannot reinitialize datatable'. when i'm working single column, modify view's table have 1.
my original example had 4-5 column, in test table i've removed single column. here's i've done (i think) wire column data (and add name too). click event calls loaddatatable function, shown below.
function loaddatatable(data) { $('#accountrequeststatus').datatable( { paging: false, "data": data, "columns": [ { "data": "companyname", "name": "blah blah" } ] } );}
from above, looks i'm reinitializing table. want this, or should else used?
here's of sample data single column, copied developer tools:
anything can see i'm doing wrong? @ point i'd finish way i've begun can call 'good'. appreciated!
use destroy
:
initialise new datatable usual, if there existing datatable matches selector, destroyed , replaced new table.
$('#accountrequeststatus').datatable( { destroy : true, //<-- here paging: false, "data": data, "columns": [ { "data": "companyname", "name": "blah blah" } ] } );}
Comments
Post a Comment