Highlight a certain member on the grid

Customizing the grid

by Flexmonster Pivot Table

HTML

<script src="https://cdn.flexmonster.com/flexmonster.js"></script>

<div>
  <h2>How to highlight a certain member on the grid</h2>
  <p>
    This example demonstrates how to highlight certain members on the grid
    regardless of their position.
  </p>
  <p>
    To highlight members, we use the
    <a href="https://www.flexmonster.com/api/customizecell/" target="_blank"
      >customizeCell()</a
    >
    method. For each cell, we check if the cell contains the needed member. If
    it does, we add the <code>highlight</code> CSS class to that cell (lines
    38-56 in the JavaScript box).
  </p>
  <p>
    In this example, we've highlighted the <code>Canada</code>,
    <code>Germany</code>, and <code>Warehouse</code> members.
  </p>
  <p>
    <a href="https://www.flexmonster.com/doc/customizing-grid/" target="_blank"
      >Visit our docs to learn more about customizing the grid</a
    >.
  </p>
</div>

<button onclick="clearCustomizing()">Clear Customizing</button>
<button onclick="startCustomizing()">Start Customizing</button>

<div id="pivot-container"></div>

CSS

@import url(https://cdn.flexmonster.com/assets/jsfiddle-styles.css);

.highlight {
  background-color: #FFB74D !important;
}

JavaScript

let pivot = new Flexmonster({
  container: "pivot-container",
  componentFolder: "https://cdn.flexmonster.com/",
  customizeCell: customizeCellFunction,
  report: {
    dataSource: {
      filename: "data/data.json"
    },
    slice: {
      rows: [
        {
          uniqueName: "Country"
        }, 
        {
          uniqueName: "Color"
        }
      ],
      columns: [
        {
          uniqueName: "[Measures]"
        }, 
        {
          uniqueName: "Business Type"
        }
      ],
      measures: [{
        uniqueName: "Price"
      }]
    }
  }
});

const highlightedMembers = {
  "Country": ["Canada", "Germany"],
  "Business Type": ["Warehouse"]
}

function customizeCellFunction(cell, data) {
  if (data.rows) {
    data.rows.forEach(row => {
      if (highlightedMembers[row.hierarchyName] &&
        highlightedMembers[row.hierarchyName].includes(row.caption)) {
        cell.addClass("highlight");
      }
    });
  }
  
  if (data.columns) {
    data.columns.forEach(column => {
      if (highlightedMembers[column.hierarchyName] &&
        highlightedMembers[column.hierarchyName].includes(column.caption)) {
        cell.addClass("highlight");
      }
    });
  }
}

function clearCustomizing() {
  pivot.customizeCell(null);
}

function startCustomizing() {
  pivot.customizeCell(customizeCellFunction);
}