display data double on loading the page.

display data double on loading the page.

mehabri88mehabri88 Posts: 3Questions: 0Answers: 0
edited August 2018 in Free community support

I am getting the data on console twice inside the rendered function. (When the data is loaded and when it is drawn). Is there a way to prevent rendering the data on loading ? and allow accessing the data only on draw? Is there any hit on the performance if I kept my code like this?

this.dtOptions = {
            columns: [ {
                title: 'Actions',
                searchable : false,
                orderable: false,
                visible: true,
                data: null,
                render: function (data) {
                    **console.log(data);**
                    return
                }
            }],
            ajax: {
                url: myURL
            },
        };

this.table = $('#datatables').on('processing.dt', function (e, settings, processing) {
            that.processing = processing;
        }).dataTable(this.dtOptions);

Thank you
Mohammed

Replies

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @mehabri88 ,

    If you wanted to improve performance, you'll notice the second arg for columns.render is the type of data being requested. You could check for that and only go deeper into render function when you expect the data to be changed. That's the reason why you see it being called multiple times, it'll be called for each of those types in case there's a difference between the search/order/display formatting.

    Cheers,

    Colin

  • mehabri88mehabri88 Posts: 3Questions: 0Answers: 0
    edited August 2018

    Hi Colin,

    Thanks for your reply.

    I see your point, but still I am getting type "display" in every one when I consoled the type.

    render: function (data, type, row, meta ) {
    console.log(type);
    if(type === 'display') {
    console.log(data);
    }
    }

    BR
    Mohammed

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @mehabri88 ,

    Yep, it will do that for all of the rows in that column - it will render all the data it is aware of (regardless of whether it is on the current page) and will add it to the cache. You would only see it once though -see the example here.

    Hope that makes sense,

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 21,302Questions: 26Answers: 4,947

    This thread might help you understand more about how columns.render behaves.
    https://datatables.net/forums/discussion/comment/136292/#Comment_136292

    Kevin

  • mehabri88mehabri88 Posts: 3Questions: 0Answers: 0

    Thank you guys.

    autoWidth: false

    solves the issue.

This discussion has been closed.