HTML Presentation Object: Get cell value from dataset

From Mea Wiki
Revision as of 15:21, 28 December 2017 by Ollvihe (talk | contribs)
Jump to navigation Jump to search

The following example shows how to get a single cell value from a dataset and present it in an HTML presentation object. The functionality is similar as in the expression language Cell function.

configurable JavaScript variables are:

  • datasetIdentifier<#uniqueId>
  • columnNameOrIndex<#uniqueId>
  • rowNumber<#uniqueId>
  • defaultValue<#uniqueId>
<div id="container<#uniqueId>"></div>

<style>
#container<#uniqueId> {
  margin: 10px 0px 0px 15px;
  font-size: 15px;
}
</style>

<script>
var datasetIdentifier<#uniqueId> = "myDataset1";
var columnNameOrIndex<#uniqueId> = 1;  //can be defined as column name (string)
var rowNumber<#uniqueId> = 1;
var defaultValue<#uniqueId> = "default value";
var poData<#uniqueId>;

function <#datasetChangeFunction>(datasetIdentifier, datasetChangeCallbackFunction) {
  if (datasetIdentifier == datasetIdentifier<#uniqueId>) {
    datasetChangeCallbackFunction(datasetAvailable<#uniqueId>);
  }
}

function datasetAvailable<#uniqueId>(datasetData) {
  if (datasetData == null || datasetData.sheets == null || datasetData.sheets.length == 0 || datasetData.sheets[0] == null || datasetData.sheets[0].values == null || datasetData.sheets[0].values.length == 0) return;
  
  if (columnNameOrIndex<#uniqueId> == null) columnNameOrIndex<#uniqueId> = 1;
  if (rowNumber<#uniqueId> == null) rowNumber<#uniqueId> = 1;

  var columnIndex = null;
  if (typeof columnNameOrIndex<#uniqueId> == "string") {
    for (var index = 0; index < datasetData.sheets[0].values.length; index++) {
      if (datasetData.sheets[0].values[index][0].value == columnNameOrIndex<#uniqueId>) {
        columnIndex = index + 1;
        break;
      }
    }
  } else {
    columnIndex = columnNameOrIndex<#uniqueId>;
  }

  var textboxValue = null;
  if (columnIndex == null) {
    textboxValue = "Column name \"" + columnNameOrIndex<#uniqueId> + "\" wasn't found.";
  } else if (columnIndex < 1) {
    textboxValue = "Column index cannot be lower than 1 (" + columnIndex + ")";
  } else if (rowNumber<#uniqueId> < 1) { 
    textboxValue = "Row number cannot be lower than 1 (" + rowNumber<#uniqueId> + ")";
  } else if (datasetData.sheets[0].values.length < columnIndex) {
    if (defaultValue<#uniqueId> == null) {
      textboxValue = "Provided column index wasn't found in the dataset.";
    } else {
      textboxValue = defaultValue<#uniqueId>;
    }
  } else if (datasetData.sheets[0].values[0].length <= rowNumber<#uniqueId>) {
    if (defaultValue<#uniqueId> == null) {
      textboxValue = "Provided row number wasn't found in the dataset.";
    } else {
      textboxValue = defaultValue<#uniqueId>;
    }
  } else {
    textboxValue = datasetData.sheets[0].values[columnIndex - 1][rowNumber<#uniqueId>].value;
  }

  $("#container<#uniqueId>").text(textboxValue);
}

</script>