简体   繁体   中英

Jquery datatable data render function called multiple times

I only have 4 records in my data table

在此处输入图片说明

But when i do console log

                columns:[
                        {
                            data: {},  name: 'mws_name', className: 'text-center', orderable: true, searchable: true, class:"mws_name", render: function (data,type,row) {
                                console.log(row.mws_name)
                                if(data.capabilities.length == 0){
                                   return data.mws_name
                               }else{
                                return data.mws_name +'&nbsp; &nbsp;' + `<span id="tool">&#x1F6C8;</span>`;
                               }
                            }
                        },

I see multiple names and duplicates在此处输入图片说明

I need to remove duplicates since i have to concat some data to display. Can someone tell me what is wrong?

The column render function is supposed to be called multiple times - once per "type". See the orthogonal data documentation for details.

If you don't want to see multiple log outputs, then use an if statement, for the type you are interested in.

For example:

render: function ( data, type, row ) {
  if ( type === 'display' ) {
    console.log(row.mws_name);
  }
  ... // the rest of your render function logic here
}

The different type values can be used to store a sort value or filter value which is different from the display value.

So, for example, you may want to show your display value as a link by wrapping it in some HTML. But when sorting and filtering, you want DataTables to just use the raw unchanged data value, without the HTML.

Your render function may not need to do anything at all with these orthogonal values, in which case, you can ignore them, as you do in your code in the question. But you will see their effect in the background when you use a logging statement, as you noticed.

Therefore, the bottom line is: If your code as written is not causing any problems, you don't need to worry about this issue. If you want your logging to be cleaner, then add the if statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM