append only if there is data

append only if there is data

LaRichyLaRichy Posts: 17Questions: 4Answers: 0

Hi,

I have the following column:

"columns": [
  { "data": "ssName2",
  fnCreatedCell: function (nTd, sData, oData, iRow, iCol) {
  $(nTd).append(' ('+oData['ss1Name2']+')'); }}
]

How can I make it that only when ss1Name2 has data and not null or empty the data is appended?
Because now, when it's empty or null I get ().

This question has an accepted answers - jump to answer

Answers

  • LaRichyLaRichy Posts: 17Questions: 4Answers: 0

    And please do not point me to the api :) I am not a programmer :)

  • LaRichyLaRichy Posts: 17Questions: 4Answers: 0

    I now know I need to use: https://www.datatables.net/examples/advanced_init/column_render.html

    But still, I do not know how to check if the data is empty so that I do not combine it.

  • LaRichyLaRichy Posts: 17Questions: 4Answers: 0

    I would expect something like this, but it does not work :(

    "columnDefs": [
      { "render": function ( data, type, row ) { 
        if(row['ss1Name2'] >= null){
          return data +' ('+ row['ss1Name2']+')'; }, "targets": 0 
        }else{
          return data;
        }
      }
    ]
    

    Anybody?

  • kthorngrenkthorngren Posts: 21,673Questions: 26Answers: 5,018
    Answer ✓

    I don't think >= null will do what you expect. Also you need to move the }, "targets": 0 outside the render function. Maybe something like this:

    "columnDefs": [
      { "targets": 0,
        "render": function ( data, type, row ) {
          if(row['ss1Name2'] != null){
            return data +' ('+ row['ss1Name2']+')'; 
          }else{
            return data;
          }
        }
      }
    ],
    

    Kevin

  • LaRichyLaRichy Posts: 17Questions: 4Answers: 0

    Yessssssssssss tnx!
    I only changed this, because null didn't work in my case: if(row['ss1Name2'] <= "")

    Thanks!

This discussion has been closed.