1

Here is my HTML which is a partial view,

<table id="table_id" class="table table-inverse">
            <thead class="thead-inverse">
                <tr>
                    <th>Select</th>
                    <th>StructureName</th>
                    <th>FieldType</th>
                    <th>DataType</th>
                    <th>IsHeirarchy</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td><input type="checkbox" /></td>
                        <td>@item.One</td>
                        <td>@item.Two</td>
                        <td>@item.Three</td>
                        <td>@item.Four</td>
                        <td><a href="#"> Edit</a></td>
                    </tr>
                }
            </tbody>
        </table>

I'm initializing DataTable as shown below,

<script type="text/javascript" language="javascript">
        $(document).ready(function () {
            $('#table_id').DataTable();
        });
</script>   

Above code is working fine and data table is initialized on page load by calling controller method to populate data.

Now, On click event I'm calling ajax method to pass input parameters to load data as shown below,

$(function () {
    $('#searchbtn').on('click', function () {          
        var url = '@Url.Action("LoadGridData", "Home")';
        var clientID = $('#Clientdata').val();
        var serviceId = $('#SelectService').val();
        $.ajax({               
            type: "GET",
            dataType: "html",                
            success: function (services) {
                $('#searchResults').load(url, { intclientId: clientID, intserviceID: serviceId });
                $('#table_id').DataTable();
            }
        });
    });

});

Here, while debugging i can see that $('#table_id').DataTable(); DataTable is initialized but its not staying.

Not sure whats happening. Any help appreciated :)

7
  • 1
    what do you mean by not staying ? Commented Feb 26, 2016 at 7:58
  • Don't initialize it in success function initialize it before jquery click event Commented Feb 26, 2016 at 8:00
  • 1
    can you tell me what is searchresults Commented Feb 26, 2016 at 8:01
  • is the #searchResults the container of your table ? Commented Feb 26, 2016 at 8:02
  • yes, #searchResult is just a <div> container Commented Feb 26, 2016 at 9:04

2 Answers 2

2

The problem is you are initializing the plugin before the table is loaded. You have to wait for the HTML to be loaded. Try this

    success: function (services) {
                    $('#searchResults').load(url, { intclientId: clientID, intserviceID: serviceId },function(){
                       $('#table_id').DataTable(); // now we apply plugin when the elment is available.
                    });                        
                }
Sign up to request clarification or add additional context in comments.

2 Comments

spot on! The jquery.load() makes the request and you imediately try to initialise the table before the data from server comes to your view... +1
Thanks a lot! :) Perfect spot!
0

I'm not sure if you can initialize the DataTable twice.

But you can pass the new data from ajax like:

$('#table_id').DataTable( {
   data: dataSet, //from ajax result
   columns: [
       { title: "Name" },
       { title: "Position" },
       ......
   ]
});

As stated in Documentation. Javascript sourced data

1 Comment

problem was with the way I initialized the DataTable function. Not with the data. Thanks for the help anyways! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.