Skip to content

Commit 0ba6fdd

Browse files
authoredFeb 7, 2020
feat(ui5-table): provide rowClick event (#1186)
Fixes: #1018
1 parent ddd2c8c commit 0ba6fdd

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed
 

‎packages/main/src/Table.js

+21
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ const metadata = {
106106
},
107107
},
108108
events: /** @lends sap.ui.webcomponents.main.Table.prototype */ {
109+
/**
110+
* Fired when a row is clicked.
111+
*
112+
* @event
113+
* @param {HTMLElement} row the clicked row.
114+
* @public
115+
*/
116+
rowClick: {
117+
detail: {
118+
row: { type: HTMLElement },
119+
},
120+
},
121+
109122
/**
110123
* Fired when the <code>ui5-table-column</code> is shown as a pop-in instead of hiding it.
111124
*
@@ -185,6 +198,7 @@ class Table extends UI5Element {
185198
}.bind(this);
186199

187200
this.fnOnRowFocused = this.onRowFocused.bind(this);
201+
this.fnOnRowClick = this.onRowClick.bind(this);
188202

189203
this._handleResize = this.popinContent.bind(this);
190204
}
@@ -196,6 +210,9 @@ class Table extends UI5Element {
196210
row._columnsInfo = columnSettings;
197211
row.removeEventListener("ui5-_focused", this.fnOnRowFocused);
198212
row.addEventListener("ui5-_focused", this.fnOnRowFocused);
213+
214+
row.removeEventListener("ui5-_click", this.fnOnRowClick);
215+
row.addEventListener("ui5-_click", this.fnOnRowClick);
199216
});
200217

201218
this.visibleColumns = this.columns.filter((column, index) => {
@@ -219,6 +236,10 @@ class Table extends UI5Element {
219236
this._itemNavigation.update(event.target);
220237
}
221238

239+
onRowClick(event) {
240+
this.fireEvent("rowClick", { row: event.target });
241+
}
242+
222243
_onColumnHeaderClick(event) {
223244
this.getColumnHeader().focus();
224245
this._itemNavigation.update(event.target);

‎packages/main/src/TableRow.js

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const metadata = {
3737
},
3838
events: /** @lends sap.ui.webcomponents.main.TableRow.prototype */ {
3939
_focused: {},
40+
41+
_click: {},
4042
},
4143
};
4244

@@ -92,6 +94,8 @@ class TableRow extends UI5Element {
9294
// the focus remains on that element.
9395
this._onfocusin(event, true /* force row focus */);
9496
}
97+
98+
this.fireEvent("_click");
9599
}
96100

97101
_getActiveElementTagName() {

‎packages/main/test/pages/Table.html

+38
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,41 @@
208208

209209
</ui5-table>
210210

211+
<!-- Test row click -->
212+
<ui5-title>Test row click</ui5-title>
213+
<ui5-table class="demo-table" id="testRowClick">
214+
<ui5-table-column id="column-1" slot="columns">
215+
<ui5-label>City</ui5-label>
216+
</ui5-table-column>
217+
218+
<ui5-table-column id="column-2" slot="columns" min-width="500" popin-text="Supplier">
219+
<ui5-label>Supplier</ui5-label>
220+
</ui5-table-column>
221+
222+
<ui5-table-column id="column-3" slot="columns" min-width="500" popin-text="Country" demand-popin>
223+
<div class="column-content">
224+
<ui5-label>Country</ui5-label>
225+
</div>
226+
</ui5-table-column>
227+
228+
<ui5-table-row data-city="Dublin">
229+
<ui5-table-cell><span id="testRowClickCell1">Dublin</span></ui5-table-cell>
230+
<ui5-table-cell><span>J.M. Brothers</span></ui5-table-cell>
231+
<ui5-table-cell><span>USA</span></ui5-table-cell>
232+
</ui5-table-row>
233+
<ui5-table-row data-city="Sofia">
234+
<ui5-table-cell><ui5-label for="myInput">Sofia</ui5-label></ui5-table-cell>
235+
<ui5-table-cell><span>J.M. Brothers</span></ui5-table-cell>
236+
<ui5-table-cell><span>USA</span></ui5-table-cell>
237+
</ui5-table-row>
238+
<ui5-table-row data-city="London">
239+
<ui5-table-cell><div id="testRowClickCell2">London</div></ui5-table-cell>
240+
<ui5-table-cell><span>J.M. Brothers</span></ui5-table-cell>
241+
<ui5-table-cell><span>USA</span></ui5-table-cell>
242+
</ui5-table-row>
243+
</ui5-table>
211244

245+
<label id="testRowClickResult" />
212246
<script>
213247
"use strict";
214248

@@ -2851,6 +2885,10 @@
28512885
table.addEventListener("popinChange", function (e) {
28522886
tableLable.textContent = `Number of poppedColumns: ${e.detail.poppedColumns.length}`;
28532887
});
2888+
2889+
testRowClick.addEventListener("rowClick", function (e) {
2890+
testRowClickResult.textContent = e.detail.row.getAttribute("data-city");
2891+
});
28542892
</script>
28552893
</body>
28562894

‎packages/main/test/specs/Table.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,18 @@ describe("Table general interaction", () => {
4444

4545
assert.strictEqual(tableLabel.getHTML(false), "Number of poppedColumns: 2", "popinChange should be fired and columns should be 4");
4646
});
47+
48+
it("tests rowClick is fired", () => {
49+
const lbl = browser.$("#testRowClickResult");
50+
const cellInRow1 = browser.$("#testRowClickCell1");
51+
const cellInRow2 = browser.$("#testRowClickCell2");
52+
const row1Data = "Dublin";
53+
const row2Data = "London";
54+
55+
cellInRow1.click();
56+
assert.ok(lbl.getHTML().indexOf(row1Data), "Event rowClick fired and intercepted.");
57+
58+
cellInRow2.click();
59+
assert.ok(lbl.getHTML().indexOf(row2Data), "Event rowClick fired and intercepted.");
60+
});
4761
});

0 commit comments

Comments
 (0)
Please sign in to comment.