Skip to content

Commit

Permalink
Fix up broken search when using a client datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
bennothommo committed Jan 14, 2025
1 parent 5885522 commit ac1337f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/backend/widgets/table/assets/js/build-min.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ this.searchInput=$('.table-search-input',this.searchForm).get(0)}}
Search.prototype.getQuery=function(){return $.trim(this.activeQuery)}
Search.prototype.hasQuery=function(){return this.searchEnabled()&&$.trim(this.activeQuery).length>0}
Search.prototype.searchEnabled=function(){return this.tableObj.options.searching}
Search.prototype.getSearchableColumns=function(){const columns=[];this.tableObj.options.columns.forEach(function(column){if(column.type==='checkbox'){return;}if(!column.searchable){return;}columns.push(column.key);});return columns;}
Search.prototype.performSearch=function(query,onSuccess){var isDirty=this.activeQuery!=query
this.activeQuery=query
if(isDirty){this.tableObj.updateDataTable(onSuccess)}}
Expand Down Expand Up @@ -439,6 +440,7 @@ Client.prototype.getRecords=function(offset,count,onSuccess){if(!count){onSucces
Client.prototype.createRecord=function(recordData,placement,relativeToKey,offset,count,onSuccess){if(placement==='bottom'){this.data.push(recordData)}else if(placement=='above'||placement=='below'){var recordIndex=this.getIndexOfKey(relativeToKey)
if(placement=='below')recordIndex++
this.data.splice(recordIndex,0,recordData)}this.getRecords(offset,count,onSuccess)}
Client.prototype.searchRecords=function(query,offset,count,onSuccess){const searchFields=this.tableObj.search.getSearchableColumns();console.log(this.data);console.log(query);console.log(searchFields);const matched=this.data.filter(function(record){for(let i=0;i<searchFields.length;i++){const value=record[searchFields[i]];if(value===undefined){continue;}if(value.toString().toLowerCase().includes(query.toLowerCase())){return true;}}return false;});if(matched.length===0){onSuccess([]);return;}if(!count){onSuccess(matched,matched.length);}else{onSuccess(matched.slice(offset,offset+count),matched.length);}}
Client.prototype.updateRecord=function(key,recordData){var recordIndex=this.getIndexOfKey(key)
if(recordIndex!==-1){recordData[this.tableObj.options.keyColumn]=key
this.data[recordIndex]=recordData}else{throw new Error('Record with they key '+key+' is not found in the data set')}}
Expand Down
40 changes: 40 additions & 0 deletions modules/backend/widgets/table/assets/js/table.datasource.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,46 @@
this.getRecords(offset, count, onSuccess)
}

/*
* Identical to getRecords except using a search query.
*/
Client.prototype.searchRecords = function(query, offset, count, onSuccess) {
const searchFields = this.tableObj.search.getSearchableColumns();

console.log(this.data);
console.log(query);
console.log(searchFields);

const matched = this.data.filter(function(record) {
for (let i = 0; i < searchFields.length; i++) {
const value = record[searchFields[i]];

if (value === undefined) {
continue;
}

if (value.toString().toLowerCase().includes(query.toLowerCase())) {
return true;
}
}

return false;
});

if (matched.length === 0) {
onSuccess([]);
return;
}

if (!count) {
// Return all records
onSuccess(matched, matched.length);
} else {
// Return a subset of records
onSuccess(matched.slice(offset, offset + count), matched.length);
}
}

/*
* Updates a record with the specified key with the passed data
*
Expand Down
18 changes: 18 additions & 0 deletions modules/backend/widgets/table/assets/js/table.helper.search.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@
return this.tableObj.options.searching
}

Search.prototype.getSearchableColumns = function() {
const columns = [];

this.tableObj.options.columns.forEach(function(column) {
if (column.type === 'checkbox') {
return;
}

if (!column.searchable) {
return;
}

columns.push(column.key);
});

return columns;
}

Search.prototype.performSearch = function(query, onSuccess) {
var isDirty = this.activeQuery != query

Expand Down

0 comments on commit ac1337f

Please sign in to comment.