Skip to content

Instantly share code, notes, and snippets.

@dennisseah
Last active September 5, 2022 16:49
Show Gist options
  • Save dennisseah/173eed844580dbcaac0f to your computer and use it in GitHub Desktop.
Save dennisseah/173eed844580dbcaac0f to your computer and use it in GitHub Desktop.
SAPUI5: Fire cell press event in sap.m.Table
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<script src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js"
id="sap-ui-bootstrap"
data-sap-ui-libs="sap.m"
data-sap-ui-theme="sap_bluecrystal"
type="text/javascript">
</script>
<script>
$(function() {
var oModel = new sap.ui.model.json.JSONModel([
{ fname: "Joe", lname: "Doe" },
{ fname: "Mary", lname: "Ann" }
]);
var oTable = new sap.m.Table({
columns: [
new sap.m.Column({ header: new sap.m.Label({text: "First Name"})}),
new sap.m.Column({ header: new sap.m.Label({text: "Last Name"})})
],
items: {
path: "/",
template: new sap.m.ColumnListItem({
cells: [
new sap.m.Text({ text: "{fname}" }),
new sap.m.Text({ text: "{lname}" })
]
})
}
});
oTable.onAfterRendering = function() {
if (sap.m.Table.prototype.onAfterRendering) {
sap.m.Table.prototype.onAfterRendering.apply(this);
}
var tbl = this;
tbl.selectedCell = null;
var items = this.getItems();
for (var i = 0; i < items.length; i++) {
var item = items[i];
var path = item.getBindingContext().getPath();
var cells = item.getAggregation('cells');
for (var j = 0; j < cells.length; j++) {
var cell = cells[j];
var $cell = cell.$();
$cell.attr('path', path);
$cell.attr('bindName', cell.mBindingInfos.text.parts[0].path);
var $parent = $cell.parent();
$parent.css('cursor', 'pointer');
$parent.click(function() {
if (tbl.selectedCell) {
tbl.selectedCell.css('color', '');
}
var $o = $(this).find('span');
var obj = tbl.getModel().getProperty($o.attr('path'));
sap.m.MessageToast.show(obj[$o.attr('bindName')]);
$o.css('color', 'orange');
tbl.selectedCell = $o;
});
}
}
}
oTable.setModel(oModel);
oTable.placeAt('content');
});
</script>
</head>
<body class="sapUiBody">
<div id="content"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment