Trying to Add IF Statement w/ Render

Trying to Add IF Statement w/ Render

DanOshDanOsh Posts: 37Questions: 11Answers: 1

Here is my code

{ data: "Link_PDF", render: function ( data, type, row ) {
                  if (data == '') {
                    return '';
                    } 
                  else {
                    return '<a href='+ data +'><i class="fa fa-file fa-lg"></i></a>';
                    }
                }
            },          

I have an edit page: http://www.skywateryachts.com/datatables_editor_test/boats.html and a quick search for "Namo" will show you the boat Namoh (id- 301) and in a far column I have the URL to a PDF file titled Link_PDF within the cell.

I tried the If statement above in the view page at: http://www.skywateryachts.com/dataviews_test/boats.html so that this display page will render a font awesome PDF img with a link to the file. But in the same search "Namo" there is no image/link in the Link_PDF column cell it is just blank.

Answers

  • allanallan Posts: 61,746Questions: 1Answers: 10,111 Site admin

    That is because there is nothing with "Namo" in the returned string that DataTables can use to search on.

    Have a read through the orthogonal data manual page - particularly using computed values. Basically you want to use the type parameter that is being passed into the function to know what data to return.

    Allan

  • DanOshDanOsh Posts: 37Questions: 11Answers: 1

    Hi Allan,

    I think I may not have been clear.

    I have a column called "Link_PDF" and I placed a pdf link in that cell for the Namoh boat. (I just thought to clarify if someone wanted to see it they could search for the boat in the search box and see the link.)

    Anyway, with the above code, below is what I want to happen with the if statement:

    1. In column "Link_PDF" I use Render to add the IF statement to that column;
    2. IF each cell is NULL, display the cell as blank;
    3. ELSE display the Font Awesome PDF icon and as a link to the pdf file.

    Hope this clarifies it.

    Thanks

  • DanOshDanOsh Posts: 37Questions: 11Answers: 1

    Also to add:

    The editor page http://www.skywateryachts.com/datatables_editor_test/boats.html has an URL pdf link in the "Link_PDF" column for the boat Namoh. I have the editor page displaying the URL.

    The view page http://www.skywateryachts.com/dataviews_test/boats.html I have the html code in the same "Link_PDF" column using Render as <a href='+ data +'><i class="fa fa-file fa-lg"></i></a> and want to view the Awesome Fonts PDF icon as a link using the URL for the Namoh boat where I have added the URL.

  • allanallan Posts: 61,746Questions: 1Answers: 10,111 Site admin

    Ah I see. Using the browser's inspect tools I can see that the HTML is being inserted into the document:

    <td><a href="null"><i class="fa fa-file fa-lg"></i></a></td>
    

    However - I don't see FontAwesome being loaded anywhere on your page and the i element doesn't have any styling information to show an icon. It looks like you just need to load FontAwesome.

    Btw - the href is null because you have data == '', but Link_PDF is null... :-)

    Allan

This discussion has been closed.