Performance issue after get data from AJAX

Performance issue after get data from AJAX

collincollin Posts: 3Questions: 1Answers: 0
edited November 2014 in Free community support

I have a datatable below that get the data from ajax and read by columns data method :
if I have 3 objects return from ajax , the below comment "test run times" should be log "count"
for 3 times for one process , but I got the log from console which show

(3)count

count

(3)count

(3)count

(3)count

That means this datatable will show after do 5 times duplicate same thing(the secound log not run for 3 times?) and cause the performance issue,
how to figure out this problems? I have no idea which function will trigger 5 times for this process?
Any help will be appreciate!

table =
$('#game_info_list').dataTable({

       "ajax": {
                "url": default_url,
                "dataSrc": "data_list",

                "type":'GET',
                "data": function(data){
                        return {game_group_id_list:$.cookie("show_list")};
                    },
                "columns": [

              { "data":
                  function(d){
                       //test run times 
                           console.log("count");
                    var s_t_img = '<img src="/assets/images/'+d.sport_type_key+'.png">'
                    return s_t_img+d.game_group_id_game_group_name;

                }},
              { "data": 
                  function(d){
                  //var dt = new Date(d.startDateTime)
                  var dt = new Date(d.startDateTime.replace(/-/g, "/"));
                  var play_time = $.formatDateTime('mm/dd <br> hh:ii', dt)

                  return '<strong>'+play_time+'</strong>'
                }}
            ]

Answers

  • allanallan Posts: 63,290Questions: 1Answers: 10,428 Site admin

    the below comment "test run times" should be log "count" for 3 times for one process

    No - I don't think it should, although it depends upon your DataTable configuration. That function will be called every time DataTables requests the data for that cell and it will request different types of data - the second parameter being passed in. If you log that parameter as well you will see that it asks for type, filtering, sorting and display data. This is how orthogonal data is supported in DataTables.

    Also not I would suggest using columns.render rather than columns.data if you are just rendering data, otherwise you need to handle the set case as well.

    Allan

  • collincollin Posts: 3Questions: 1Answers: 0

    thanks for your response, I have known why it will be triggered for 4 times,
    but could you please kindly give me a update code for reference since I cannot work with the render function after modify my code for one time process. thanks again!

  • allanallan Posts: 63,290Questions: 1Answers: 10,428 Site admin

    I'm really sorry, but I'm not sure what you are asking for? Why can't you use the columns.render function?

    Allan

  • collincollin Posts: 3Questions: 1Answers: 0

    Hi Allan , from your suggestion, it seems like I can use the render method to avoid to processing 4 times for each column of data, but I think I may have a confuse to use this render method after read the document. Could you please give me a simple example to guide me how to change my code to achieve my goal?

  • allanallan Posts: 63,290Questions: 1Answers: 10,428 Site admin

    Perhaps something as simple as:

    {
      data: 'startDateTime',
      render: function ( data, type, row ) {
       return ...; // construct links here using `data` and other data.
      }
    }
    

    Allan

This discussion has been closed.