Ways of passing data to datatable

Ways of passing data to datatable

imda123imda123 Posts: 2Questions: 1Answers: 0
edited December 2016 in Free community support

Hi everyone,

I have a jquery post like following, and which updates my existing datatable directly by injecting the data into the DOM like following:

  $.post("/SearchCompetitor/Index", { username: _username }, StartLoading())
                               .done(function (data) {
                                   if (data !== "UsernameError") {
                              
                              
                                       var dbtb = $('<table />').append(data).find('#datatable-responsive').html();
                                       $('#datatable-responsive').html(dbtb);
                                      
                                       $('#datatable-responsive').dataTable({
                        
                                        "bDestroy": true
                                       });
                                    // This is where I re-create the table to display the data
                                   }
                                   else {
                                       StopLoading();
                                       ShowMessage("No user was found under: " + $('.txtSearch').val());
                                   }
                               })

The "data" object holds the HTML of the table and what I'm trying to figure out here is how to pass the data directly into the datatable, instead of directly injecting it into the DOM like I'm doing right now:

                                   var dbtb = $('<table />').append(data).find('#datatable-responsive').html();
                                   $('#datatable-responsive').html(dbtb);

So my questions are:

  1. How to convert a C# List into a DBTable JSon acceptable format
  2. How to fill the data directly into the dbtable,without first injecting it into the DOM of the browser

Can someone help me out with this please?

Answers

  • imda123imda123 Posts: 2Questions: 1Answers: 0

    P.S. The reason why I'd like to pass the data directly into the datatable first is because sometimes I could be injecting up to 5000 items into the DOM and my browser literally freezes and crashes, causing big performance issues for me.

    This way if I passed the data directly into the datatable, DT would only inject xx (10-25-50-100) items into DOM at a time, if I'm correct?

  • allanallan Posts: 63,226Questions: 1Answers: 10,416 Site admin

    Use rows.add() (or row.add() for a single row) to dynamically add rows to the DataTable. Using the API will also allow it to render the table more efficiently if you are using paging and deferRender, since not all nodes would need to be immediately created.

    Allan

This discussion has been closed.