1

I only have 4 records in my data table

enter image description here

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 enter image description here

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

10
  • I suspect "what is wrong" is that you're misinterpreting the console.log and what the render function does and when it is called. Can you provide a snippet with some sample data (not loaded via ajax). See the datatables SO wiki for a sample. Commented Jul 5, 2021 at 10:58
  • what do you mean sir? okay let me try to provide some snippet Commented Jul 5, 2021 at 11:10
  • is there an alternative using render function @freedomn-m? Commented Jul 5, 2021 at 11:31
  • Yes i already updated the question. I discovered that render called console.log multiple times. how can i avoid it or what other methods can i use? Commented Jul 5, 2021 at 11:38
  • Why do i need to remove console log? i didnt find an way to call console log once in column function Commented Jul 5, 2021 at 11:41

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.