官术网_书友最值得收藏!

Handling missing image issue

In the previous recipe, we saw how to add images to the report. You will be using that technique in many cases, some involving hundreds of images (For example, Product Catalogue).

There will often be a case in which database has a URL or image name, whereas the corresponding image is either missing or inaccessible. In such a case, the web browser shows an error symbol. This looks quite ugly and needs to be handled properly.

Handling missing image issue

In this recipe, we will see how to handle this problem gracefully.

Getting ready

We will use the report prepared in previous recipe. We need to delete the Green.jpg file (or rename it to something else) from the server, in order to create the missing image scenario.

How to do it...

  1. In the previous recipe, we added an image object and defined its conditional URLs. We need to replace that image with an HTML Item. For that, unlock the report objects How to do it... and delete the image component. Add an HTML Item in the same column.
    How to do it...
  2. Select this HTML item and from the Properties pane, set its HTML Source Variable to 'Traffic'. (Please note that we already have this conditional variable in the last recipe).
  3. Now define the HTML for different conditions. Start with 'red'. Choose 'red' from conditional explorer and define the HTML as: <img src="../samples/images/red.jpg" alt="downsell" onError="img2txt(this)"/>
  4. For 'yellow', define the HTML as: <img src="../samples/images/yellow.jpg" alt="No Change" onError="img2txt(this)"/>
  5. For 'green', define HTML as: <img src="../samples/images/green.jpg" alt="Upsell" onError="img2txt(this)"/>
  6. Now go back to the No Variable state by double clicking on the green bar, and add another HTML item on the report. Put it just before the list.
    How to do it...
  7. Define this HTML as:
    <script>
    function img2txt(img) {
    txt = img.alt;
    img.parentNode.innerHTML=txt;}
    </script>
  8. Now run the report to test it.
    How to do it...

    As you can see, if the image is missing, the report will now handle it gracefully and show some text instead of an error image.

How it works...

Here we are using our custom code to display the image, instead of using CRS's in-built Image component.

We have pulled an HTML item onto the report and defined it to display different images depending on the condition using the <img> tag. This tag allows us to define an alternative text and onError event as well. We are using the onError event to call our custom made JavaScript function called img2txt.

This function replaces the HTML item with a text which was originally defined as 'alternative text'. Hence, if green.jpg is missing, this function will replace it with a text item Upsell.

There's more...

As we are using HTML code and JavaScript in this technique, it works in HTML format only. This technique will be useful for a lot of graphical reports (dashboards, scorecards, online product catalogues, and so on).

主站蜘蛛池模板: 卢龙县| 东丰县| 岳阳市| 西昌市| 宜宾县| 遂昌县| 张掖市| 林周县| 宁晋县| 麟游县| 茂名市| 武宁县| 吉木萨尔县| 扎囊县| 安远县| 兴城市| 井冈山市| 中山市| 长岭县| 昌黎县| 塘沽区| 乌什县| 门源| 惠东县| 原平市| 白玉县| 石棉县| 隆德县| 昌宁县| 托里县| 道孚县| 万载县| 怀安县| 余干县| 阿鲁科尔沁旗| 噶尔县| 延边| 昭觉县| 江阴市| 葫芦岛市| 将乐县|