if/else Statement

if/else Statement

2008jackson2008jackson Posts: 38Questions: 9Answers: 0
edited May 2021 in Free community support

Hi. I would like to show a 'View' hyperlink when a Sharepoint online attachment link is available from the JSON object returned. Using code below, im able to generate the list of attachments. However, it only displays one View hyperlink for cases with multiple attachments. Please advice.

I need to have a validation first to look for an attachment hyperlink and then create the view hyperlink for number of attachment links from JSON object.

$(document).ready(function() {    
    loadItems();
});    
    
function loadItems() {   
    var oDataUrl = "SPO_Link/_api/web/lists/GetByTitle('List_Name')/items?$expand=AttachmentFiles";    
    $.ajax({    
        url: oDataUrl,    
        type: "GET",    
        dataType: "json",    
        headers: {    
            "accept": "application/json;odata=verbose"    
        },    
        success: mySuccHandler,    
        error: myErrHandler    
    });    
}    
  
function mySuccHandler(data) {   
    try {    
        $('#table_id').DataTable({    
        "dom": 'Bfrtip',
    "aaData": data.d.results,
         "aoColumns": [  
            { "mData": "Title"},             
            {    
                "mData": "AttachmentFiles.results[].ServerRelativeUrl",
                "render": function(data) { 
                if(data) {return data}
              }     
            }           
            ]    
        });    
    } catch (e) {alert(e.message);}    
}    
    
function myErrHandler(data, errMessage) {    
    alert("Error: " + errMessage);    
}   

This question has an accepted answers - jump to answer

Answers

  • 2008jackson2008jackson Posts: 38Questions: 9Answers: 0
    edited May 2021

    Doing the following merges all values returned into one icon instead of one icon per attachment link. Reading further, i see that the attachment names are seperated with a , since the values returned from the JSON object are concatenated together.

                {    
                    "mData": "AttachmentFiles.results[].ServerRelativeUrl", render: function ( data, type, row ) {
                      if (data == '') {
                        return '';
                        }
                      else {
                        return '<a href="'+ data +'"><i class="fa fa-file fa-lg"></i></a>';
                        }
                    }       
                }
    
  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    Answer ✓

    Depending on whether its a an array of names or a string will determine the specific steps but you will need to use Javascript to iterate the names to generate a string that contains the output of each link. Maybe this pseudo code will help:

    var html = '';
    
    for (i=0; i<data.length; i++) {
      html += '<a href="'+ data[i[ +'"><i class="fa fa-file fa-lg"></i></a>';
    }
    
    return html;
    

    Kevin

  • 2008jackson2008jackson Posts: 38Questions: 9Answers: 0

    worked perfectly thankyou. Pls note correction on line 4.

    var html = '';
     
    for (i=0; i<data.length; i++) {
      html += '<a href="'+ data[i] +'"><i class="fa fa-file fa-lg"></i></a>';
    }
     
    return html;
    
This discussion has been closed.