Skip to content

Instantly share code, notes, and snippets.

@dennisseah
Last active August 28, 2018 02:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dennisseah/63289b77d6d7d902b3de to your computer and use it in GitHub Desktop.
Save dennisseah/63289b77d6d7d902b3de to your computer and use it in GitHub Desktop.
SAPUI5: Set background color for column in sap.ui.table.Table
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script id="sap-ui-bootstrap"
type="text/javascript"
data-sap-ui-libs="sap.ui.table,sap.ui.commons"
data-sap-ui-theme="sap_bluecrystal"
src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js">
</script>
<style>
.coloredTable {
background-color: rgba(99,99,99, 0.1);
}
</style>
<script>
jQuery(function() {
sap.ui.table.Column.extend('ColoredColumnTable', {
metadata: {
properties: {
coloredStyleClass: 'string'
}
}
});
var aData = [
{name: "Dente", company: "http://www.sap.com", status: 1},
{name: "Friese", company: "Google", status: 1},
{name: "Mann", company: "http://www.sap.com", status: 2},
{name: "Schutt", company: "SAP", status: 3}
];
var oTable = new sap.ui.table.Table({
width: '350px',
selectionMode: sap.ui.table.SelectionMode.None,
});
oTable.addColumn(new ColoredColumnTable({
label: new sap.ui.commons.Label({text: "Name"}),
template: new sap.ui.commons.TextView({text:"{name}"}),
coloredStyleClass: 'coloredTable'
}));
oTable.addColumn(new sap.ui.table.Column({
label: new sap.ui.commons.Label({text: "Company"}),
template: new sap.ui.commons.TextView({text:"{company}"})
}));
oTable.onAfterRendering = function() {
if (sap.ui.table.Table.prototype.onAfterRendering) {
sap.ui.table.Table.prototype.onAfterRendering.apply(this, arguments);
}
var cols = this.getColumns();
for (var i = 0; i < cols.length; i++) {
if (cols[i].getColoredStyleClass) {
var coloredCol = cols[i].getColoredStyleClass();
if (coloredCol) {
var th = this.$().find('.sapUiTableColHdr').find('.sapUiTableCol');
$(th[i]).addClass(coloredCol);
var rows = this.$().find('.sapUiTableTr');
for(var j = 0; j < rows.length; j++) {
var r = rows[j];
var tds = $(r).find('td');
$(tds[i]).addClass(coloredCol);
}
}
}
}
}
var oModel = new sap.ui.model.json.JSONModel();
oModel.setData(aData);
oTable.setModel(oModel);
oTable.bindRows("/");
oTable.placeAt("content");
})
</script>
</head>
<body class="sapUiBody">
<div id="content"></div>
</body>
</html>
@dash042
Copy link

dash042 commented Mar 25, 2015

is there a way to avoid having the jquery function encompass the entire table generation?

@njames
Copy link

njames commented Aug 28, 2018

You can skip the overloading of the table by using the addCustomData method on the column to add a key value pair to add your classname. Pretty much the same impact.
Oh and you can change var r = rows[j]; to var r = rows[j].getDomRef();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment