ColumnDef blank entry

ColumnDef blank entry

karliekarlie Posts: 83Questions: 17Answers: 0
edited March 2018 in DataTables 1.10

Hi, I have a column that I want to be empty if there is no value assigned to it. I have achieved this using this:

Field::inst( 'ct' )->setFormatter( Format::ifEmpty( null ) ),

I then wanted to append the text ct to each entry in the column. I achieved this using this:

{"render": function ( data, type, row ) {return data +' '+ "ct"+'';},"targets": 10},

However cells with no values assigned to them now display null ct

Any ideas how I could overcome this? I can't provide a link but here's an image.

I know it's doing this because the cell is no longer empty, but obviously that's not desired behaviour!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,449Questions: 26Answers: 4,975

    One option may be to check if data is null in your render function and return "" if so otherwise return data +' '+ "ct"+''.

    Kevin

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

    Hi Karlie,

    This example will help. There's a null value in the third column - the renderer returns blank for that one, and adds " ct" for the rest.

    Hope that helps,

    Cheers,

    Colin

  • karliekarlie Posts: 83Questions: 17Answers: 0

    Thanks for your help Colin & kthorngren. Colin's example pointed me in the right direction I just changed it slightly:

    { targets: 10, render: function (data, type, row, meta) {return (data == null) ? "" : data + "ct";} },
    

    Very much appreciated, the community here is brilliant!

  • karliekarlie Posts: 83Questions: 17Answers: 0

    Thought I would join this on to an existing thread as it's more or less identical. I am rendering a column with an image, and it is displaying fine. However, when there is no image available I am seeing the broken image icon rather than some text saying 'no image'. Here is the code I am using:

    { targets: 1, data: 'thumbnail', render: function ( data, type, row, meta ) {return (data == null) ? "no image" : '<img style="width:40px;height:40px;display:block;margin:0 auto;" src="https://www.wardgemstones.com/media/catalog/product'+data+'">';}},
    

    You can see the issue here: https://wardgemstones.com/stonefinder/

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    The data isn't actually null:

    thumbnail:""
    

    Check for an empty string rather than null.

    Allan

  • karliekarlie Posts: 83Questions: 17Answers: 0

    Thanks Alan, that solved it! I'll add the code below, it may help somebody.

    { targets: 1, data: 'thumbnail', render: function ( data, type, row, meta ) {return (data == "") ? "<img src='https://www.your-url.com/awaiting-image.png'>" : '<img style="width:40px;height:40px;display:block;margin:0 auto;" src="https://www.your-url.com'+data+'">';}}
    
This discussion has been closed.