class DataTableApi {
eventDispatcher = new EventDispatcher();
static EVENTS = {
ON_SEARCH_INPUT_CHANGED: 'searchInputChanged'
};
constructor(settings) {
this.api = new $.fn.dataTable.Api(settings);
}
getSearchInput() {
return $(this.api.table().container()).find('.dataTables_filter input');
}
replaceSearchInputElement() {
const searchInput = this.getSearchInput();
if (searchInput.length === 0) {
return null;
}
const newInput = searchInput.clone();
newInput.val(searchInput.val());
searchInput.replaceWith(newInput);
return newInput;
}
overrideSearchInputHandler() {
const searchInput = this.replaceSearchInputElement();
if (!searchInput) {
throw new Error('Search input element not found in the DataTable.');
}
searchInput
.off('.customDataTableSearch')
.on('keyup.customDataTableSearch search.customDataTableSearch input.customDataTableSearch', (event) => {
this.eventDispatcher.emit(DataTableApi.EVENTS.ON_SEARCH_INPUT_CHANGED, {
searchString: event.currentTarget.value,
event: event
});
});
}
search(searchString) {
this.api.search(searchString, true, false).draw();
}
}