Treat null, undefined, and empty string as different values

Options

by Flexmonster Pivot Table

HTML

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

<div>
  <h2>Example with the distinguishNullUndefinedEmpty option</h2>
  <p>
    By default, the component treats <code>null</code>, <code>undefined</code>,
    and empty string (<code>""</code>) values as the same. Set the
    <code>distinguishNullUndefinedEmpty</code> option to <code>true</code>, so
    that these values are treated distinctly. Learn more about
    <a href="https://www.flexmonster.com/doc/options/" target="_blank"
    >Flexmonster's options</a>.
  </p>
  <p>
    Using the switch below you can set the
    <a
      target="_blank"
      href="https://www.flexmonster.com/api/options-object/#distinguishnullundefinedempty"
    >distinguishNullUndefinedEmpty</a> to <code>true</code> or <code>false</code>.
  </p>
</div>

<div class="switch-container compact">
  <label class="switch">
    <input
      type="checkbox"
      id="distinguishCheckbox"
      onclick="toggleDistinguishNullUndefinedEmpty()"
    />
    <span class="slider round"></span>
  </label>
  <span class="text" id="currentSetting">
    <code>distinguishNullUndefinedEmpty</code> is set to <code>false</code>.
  </span>
</div>

<div id="pivot-container">The component will appear here</div>

CSS

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

JavaScript

let pivot = new Flexmonster({
  container: "pivot-container",
  componentFolder: "https://cdn.flexmonster.com/",
  toolbar: true,
  report: {
    dataSource: {
      data: getData(),
    },
    options: {
      distinguishNullUndefinedEmpty: false,
    },
    slice: {
      rows: [
        {
          uniqueName: "Category",
        },
      ],
      columns: [
        {
          uniqueName: "Color",
        },
        {
          uniqueName: "[Measures]",
        },
      ],
      measures: [
        {
          uniqueName: "Price",
        },
      ],
    },
  },
  global: {
    localization: {
      grid: {
        blankMember: "",
      },
    },
  },
})

function toggleDistinguishNullUndefinedEmpty() {
  const checkbox = document.getElementById('distinguishCheckbox');
  const currentSetting = document.getElementById('currentSetting');
  const currentReport = pivot.getReport({ withDefaults: true, withGlobals: true });
  currentReport.options.distinguishNullUndefinedEmpty = checkbox.checked;
  pivot.setReport(currentReport);
  currentSetting.innerHTML = `<code>distinguishNullUndefinedEmpty</code> is set to <code>${checkbox.checked}</code>.`;
}

function getData() {
  return [
    {
      Color: "green",
      Price: 166,
      Quantity: 19,
      Category: null,
    },
    {
      Color: "blue",
      Price: 345,
      Quantity: 76,
      Category: undefined,
    },
    {
      Color: "blue",
      Price: 345,
      Quantity: 76,
      Category: "Accessories",
    },
    {
      Color: "red",
      Price: 345,
      Quantity: 76,
      Category: "",
    },
  ]
}