Skip to content

Commit f2904ef

Browse files
authored
Merge pull request eclipse-ditto#1864 from eclipse-ditto/feature/add-search-count-to-ui
Enhance things search slot by displaying the amount of matching things
2 parents 0ad1528 + 9395cc6 commit f2904ef

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

Diff for: ui/modules/things/things.html

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<input autocomplete="false" name="hidden" type="text" style="display:none;">
2525
<input type="search" class="form-control form-control-sm" id="searchFilterEdit" name="search"
2626
spellcheck=false autocomplete="off" />
27+
<span class="input-group-text" id="searchFilterCount" data-bs-toggle="tooltip"
28+
title="Amount of things matching the filter">
29+
</span>
2730
<button type="button" id="searchThings" class="btn btn-outline-primary btn-sm" data-bs-toggle="tooltip"
2831
title="Search things by thingId or valid ditto search filter">
2932
search

Diff for: ui/modules/things/thingsSearch.ts

+26-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ let lastSearch = '';
3030
let theSearchCursor;
3131

3232
const dom = {
33+
searchFilterCount: null,
3334
thingsTableHead: null,
3435
thingsTableBody: null,
3536
searchFilterEdit: null,
@@ -76,7 +77,7 @@ function onThingsTableClicked(event) {
7677
* Tests if the search filter is an RQL. If yes, things search is called otherwise just things get
7778
* @param {String} filter search filter string containing an RQL or a thingId
7879
*/
79-
export function searchTriggered(filter) {
80+
export function searchTriggered(filter: string) {
8081
lastSearch = filter;
8182
const regex = /^(eq\(|ne\(|gt\(|ge\(|lt\(|le\(|in\(|like\(|ilike\(|exists\(|and\(|or\(|not\().*/;
8283
if (filter === '' || regex.test(filter)) {
@@ -113,13 +114,15 @@ export function performLastSearch() {
113114
* @param {Array} thingIds Array of thingIds
114115
*/
115116
export function getThings(thingIds) {
117+
dom.searchFilterCount.innerHTML = '';
116118
dom.thingsTableBody.innerHTML = '';
117119
const fieldsQueryParameter = Fields.getQueryParameter();
118120
if (thingIds.length > 0) {
119121
API.callDittoREST('GET',
120122
`/things?${fieldsQueryParameter}&ids=${thingIds}&option=sort(%2BthingId)`)
121123
.then((thingJsonArray) => {
122124
fillThingsTable(thingJsonArray);
125+
dom.searchFilterCount.innerHTML = '#: ' + thingJsonArray.length;
123126
notifyAll(thingIds, fieldsQueryParameter);
124127
})
125128
.catch((error) => {
@@ -134,19 +137,38 @@ export function getThings(thingIds) {
134137

135138
function resetAndClearViews(retainThing = false) {
136139
theSearchCursor = null;
140+
dom.searchFilterCount.innerHTML = '';
137141
dom.thingsTableHead.innerHTML = '';
138142
dom.thingsTableBody.innerHTML = '';
139143
if (!retainThing) {
140144
Things.setTheThing(null);
141145
}
142146
}
143147

148+
/**
149+
* Calls Ditto search API to perform a count and adds the count to the UI.
150+
* @param {String} filter Ditto search filter (rql)
151+
*/
152+
function countThings(filter: string) {
153+
dom.searchFilterCount.innerHTML = '';
154+
const namespaces = Environments.current().searchNamespaces
155+
API.callDittoREST('GET',
156+
'/search/things/count' +
157+
((filter && filter !== '') ? '?filter=' + encodeURIComponent(filter) : '') +
158+
((namespaces && namespaces !== '') ? '&namespaces=' + namespaces : ''), null, null
159+
).then((countResult) => {
160+
dom.searchFilterCount.innerHTML = '#: ' + countResult;
161+
}).catch((error) => {
162+
notifyAll();
163+
});
164+
}
165+
144166
/**
145167
* Calls Ditto search api and fills UI with the result
146168
* @param {String} filter Ditto search filter (rql)
147169
* @param {boolean} isMore (optional) use cursor from previous search for additional pages
148170
*/
149-
function searchThings(filter, isMore = false) {
171+
function searchThings(filter: string, isMore = false) {
150172
document.body.style.cursor = 'progress';
151173

152174
const namespaces = Environments.current().searchNamespaces;
@@ -162,6 +184,7 @@ function searchThings(filter, isMore = false) {
162184
if (isMore) {
163185
removeMoreFromThingList();
164186
} else {
187+
countThings(filter);
165188
resetAndClearViews(true);
166189
}
167190
fillThingsTable(searchResult.items);
@@ -212,7 +235,7 @@ export function removeMoreFromThingList() {
212235
* Fills the things table UI with the given things
213236
* @param {Array} thingsList Array of thing json objects
214237
*/
215-
function fillThingsTable(thingsList) {
238+
function fillThingsTable(thingsList: any[]) {
216239
const activeFields = Environments.current().fieldList.filter((f) => f.active);
217240
fillHeaderRow();
218241
let thingSelected = false;

0 commit comments

Comments
 (0)