简体   繁体   English

多次调用jquery数据表数据渲染函数

[英]Jquery datatable data render function called multiple times

I only have 4 records in my data table我的数据表中只有 4 条记录

在此处输入图片说明

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.如果您不想看到多个日志输出,请针对您感兴趣的类型使用if语句。

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.不同type值可用于存储与display值不同的sort值或filter值。

So, for example, you may want to show your display value as a link by wrapping it in some HTML.因此,例如,您可能希望通过将display值包装在一些 HTML 中来将其display为链接。 But when sorting and filtering, you want DataTables to just use the raw unchanged data value, without the HTML.但是在排序和过滤时,您希望 DataTables 只使用未更改的原始数据值,而不使用 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.如果您希望日志记录更清晰,请添加if语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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