Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
255
Rendering a cell value depending on other attributes of the object
posted

Hi,

Suppose you have object of the type

var item = [ { "ID": "4711", "A": "0", "B": "yes" } ]

These objects shall be shown in an igGrid. If A==0 the text "yes" shall be shown in red otherwise in green.

For the column showing B I have specified a formatter (a function). But the formatter only gets the value as the parameter not the entire object so there seems to be no chance to evaluate the value of property A of the current rendered object.

Is there any possibility to access the current rendered object in a formatter. A workaround could be to get the key of the object currently rendered (formatted).

Thanks for your help

Michael

Parents
  • 17590
    Offline posted

    Hello Michael,

    Setting styling for column B based on some condition could be accomplished by using Conditional Template. Such a template could be created and set for a column via the template column option of igGrid. In this template you have a reference to all the values for particular row and the cell appearance could be changed based on any of these values. In your scenario, the template could check what is the value of column A and set the font color in column B based on this value. For example:

    <!-- creating template -->
       <script id="colTmpl" type="text/template">
      {{if parseInt(${A}) > 0 }}
      <span style='color:green'>${B}</span></font>
      {{else}}
       <span style='color:red'>${B}</span></font>
      {{/if}}
     </script>
        <script>
     $(function () {
       var items = [  { "ID": "4711", "A": "0", "B": "yes" },
          { "ID": "4712", "A": "1", "B": "yes" },
          { "ID": "4713", "A": "0", "B": "yes" }
          ]
       $("#grid").igGrid({
                    columns: [
                        { headerText: "ID", key: "ID", dataType: "string" },
                        { headerText: "A", key: "A", dataType: "string"  },
                        { headerText: "B", key: "B", dataType: "string", template: $("#colTmpl" ).html()}
                    ],
                    width: "500px",
                    dataSource: items
                });
            });
        </script>

    I am also attaching a small sample illustrating my suggestion for your reference.

    Please do not hesitate to contact me if you have any additional questions regarding this matter.

    igGridWithTemplateColumn.zip
Reply Children