1

I have a JavaScript page which is querying a SharePoint list. I'm getting the result of column called DevicePriority which is either Red, Amber or Green. The results are returned to a HTML page. I need to change these to the relevant jpgs eg ://xxx.com/IT/SiteAssets/amber.png

To do this I think I'll need a onload function with an if statement, so

<body onload="imageswap()">

<script>
if DevicePriority = "Red" 
 {
Don't know what condition to put here?
  } 
</script>

Do I place this in the JavaScript or the HTML? Both codes below:

JavaScript

function getDeviceDetails() {
var txtTitle = "";
var txtOverview = "";
var txtAccessories = "";
var txtDevicetype = "";
var txtTypicalDeviceUsage ="";
var txtKnownSystemIssues ="";
var txtLifeCycles = "";
var txtTrafficlight = "";

var tempLCS2 = "";

var query = "http://collaboration-xxxx/sites/it/SystemInventory/_vti_bin/listdata.svc/Devices?$expand=LifeCycleStatus&Priority&$filter=Id eq " + window.DeviceId + "";

var call = $.ajax({
        url: query,
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }       
    });
call.done(function (data,textStatus, jqXHR){
$.each(data.d.results, function(index, item) {
        var tempID = item.Id;
        var tempTitle = item.Title;




         var LifeCycleStart = item.DeviceAvailableFrom;
        var LifeCycleStatus = item.LifeCycleStatusValue;
        var DeviceOverView = item.Description;
        var AccessDetails = item.Accessories;
        var DeviceKind = item.Devicetype;
        var Usage = item.TypicalUsage;
        var DevicePriority = item.PriorityValue;

        txtTitle = "<p>" + LifeCycleStart + "</p><p>" + LifeCycleStatus  
+ "</p>";
        txtOverview = "<p>" + DeviceOverView + "</p>";
        txtAccessories = "<p>" + AccessDetails + "</p>";  
        txtDevicetype = "<p>" + DeviceKind  + "</p>";
        txtTypicalDeviceUsage = "<p>" + Usage + "</p>";
        txtTrafficlight = "<p>" + DevicePriority + "</p>";

    });
    $('#devicedetails').append($(txtTitle));  
    $('#deviceoverview').append($(txtOverview));
    $('#devicekind').append(txtDevicetype);
    $('#deviceacc').append(txtAccessories);
    $('#deviceuse').append(txtTypicalDeviceUsage);
    $('#devicestatus').append(txtTrafficlight);


});

  call.fail(function (jqXHR,textStatus,errorThrown){
    alert("Error retrieving data: " + jqXHR.responseText);
});

}

HTML

 <html xmlns:mso="urn:schemas-microsoft-com:office:office"  
  xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"><head>
  <script src="/jquery-1.10.1.min.js"></script>
 <script 
 src="/it/SystemInventory/SiteAssets/scripts/moment.min.js"></script>
<script src="/sites/it/SystemInventory/SiteAssets/getDevices.js"></script>
 <script  
   src="/sites/it/SystemInventory/SiteAssets/getDeviceDetails.js"></script>
 <script type="text/javascript">

     function parseQueryString(queryString)
    {
    var params = {}, queries, temp, i, l;
    queries = queryString.split("&");
    for ( i = 0, l = queries.length; i < l; i++ ) {
        temp = queries[i].split('=');
        params[temp[0]] = temp[1];
   }
return params;
 }

  $(document).ready(function(){
function getDeviceID() {
    if(typeof parseQueryString === "function") {
        if(window.location.href.indexOf("?") >= 0) {
            var qs = parseQueryString(window.location.href.split("?")[1])
            window.DeviceId = qs.did;
            getDeviceDetails();
            getDeviceKnownIssues();
        }
       }
}
getDeviceID();
    getDevices();

});

 <!--[if gte mso 9]><xml>
 <mso:CustomDocumentProperties>
 <mso:_dlc_DocId msdt:dt="string">ITDOCUMENTS-135-47</mso:_dlc_DocId>
 <mso:_dlc_DocIdItemGuid msdt:dt="string">1d6007ea-9e58-4298-b2d6-
   9196e2678ea3</mso:_dlc_DocIdItemGuid>

 <mso:_dlc_DocIdUrl msdt:dt="string">http://collaboration-
  dev.norgine.com/sites/it/SystemInventory/_layouts/DocIdRedir.aspx?  
 ID=ITDOCUMENTS-135-47, ITDOCUMENTS-135-47</mso:_dlc_DocIdUrl>
 </mso:CustomDocumentProperties>
 </xml><![endif]-->
 </head>
 <style type="text/css">
 .tg  {border-collapse:collapse;border-spacing:0;}
 .tg td{font-family:Arial, sans-serif;font-size:16px;padding:10px     
   5px;border-style:solid;border-width:1px;overflow:hidden;word-  
   break:normal;}

.tg .tg-yw4l{vertical-align:top}


 </style>
 <table>
 <table class="tg">
  <tr>

<td rowspan="3" class="tg-yw4l"><h2>Devices</h2>
 <div id="devices"></div>



 </td>


<td class="tg-yw4l"><h2>Devise Details</h2> 

<div id="devicedetails">
    </div></td>


    <td class="tg-yw4l"><h2>Specification</h2>

 <div id="deviceoverview">
     <div id="devicekind">   
    </div></td>




   </tr>
   <tr>
<td class="tg-yw4l"><h2>Known Issues</h2> 
  <div id="knowntitle">
      </div> 
      <input type=button onClick="location.href='http://collaboration-
     XXX.com/sites/it/SystemInventory/Lists/Knownissues/NewForm.aspx?
     Source=http%3A%2F%2Fcollaboration%2Ddev%2Enorgine%2Ecom%2Fsites%2Fit%
     2FSystemInventory%2FLists%2FKnownissues%2FAllItems%
     2Easpx&RootFolder='" value='Submit a known issue '> 


       </td>



   <td class="tg-yw4l"><h2>Accessories </h2> 
 <div id="deviceacc"></div></td>

<td colspan="2" td class="tg-yw4l"><h2>Typical Usage</h2> 

<td colspan="3" td class="tg-yw4l" width="100"><h2>Current Status</h2> 

</table>
  • I am not getting what you are trying to do? Are you displaying multiple devices in one and based on device click you are showing device details with image or what? – ThinkB4Code Jun 15 '17 at 13:06
  • No, basically it's a RAG status column. So if a user selects Red, Amber, or Green in the column choice of SharePoint (Priority) is shows the relevant coloured jpg on the HTML page. – Dazza Jun 15 '17 at 13:23
2

For this, you can make following change in your code.

HTML
Add a container for your image holder, e.g. <div id='imageContainer'></div>, where you want to display the image.

JS
After getting the data of DevicePriority just call the following statement:

$('#imageContainer').append("<img src='url_to_web/SiteAssets/" + DevicePriority + ".png' />");

Just make sure, the value DevicePriority gives is the name of you image without extension and the extension is hard-coded as .png.

  • Thanks, that looks like it should work, I've put $('#imageContainer').append("<img src='/IT/SiteAssets/" + DevicePriority + ".png' />"); but nothings coming up. I've added the div tag to the HTML... – Dazza Jun 15 '17 at 13:58
  • I changed the + DevicePriority to + txtTrafficlight and an image is showing up but as the x place holder and when I look at the properties it's: collaboration-xxx.com/IT/SiteAssets/%3Cp%3EAmber%3C/p%3E.png – Dazza Jun 15 '17 at 14:02
  • Are you using rich text formatting in column? Because it is adding <p> tag around the data. If so use RegEx to remove the extra tags. – ThinkB4Code Jun 15 '17 at 14:09
  • Use following js code. txtTrafficlight.replace(/<[^>]*>/g, '');. And if it does solve your problem then please mark the answer. :) – ThinkB4Code Jun 15 '17 at 14:13
  • also add a .trim() function to remove white spaces around the data. – ThinkB4Code Jun 15 '17 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.