dev: More improvements for fuzzySearch script

pull/527/head
SandyDatatables 3 years ago
parent e8609c7464
commit d2a3b53026

@ -42,7 +42,7 @@
that_j = that[j-1]; that_j = that[j-1];
cost = (this_i === that_j) ? 0 : 1; // Step 5 cost = (this_i === that_j) ? 0 : 1; // Step 5
// Calculate the minimum (much faster than Math.min(...)). // Calculate the minimum (much faster than Math.min(...)).
min = matrix[i - 1][j ] + 1; // Deletion. min = matrix[i - 1][j ] + 1; // Devarion.
if ((t = matrix[i ][j - 1] + 1 ) < min) min = t; // Insertion. if ((t = matrix[i ][j - 1] + 1 ) < min) min = t; // Insertion.
if ((t = matrix[i - 1][j - 1] + cost) < min) min = t; // Substitution. if ((t = matrix[i - 1][j - 1] + cost) < min) min = t; // Substitution.
@ -127,16 +127,17 @@
for (var y = 0; y < splitData.length; y++){ for (var y = 0; y < splitData.length; y++){
// If this search Term word is the beginning of the word in the cell we want to pass this word // If this search Term word is the beginning of the word in the cell we want to pass this word
if(splitData[y].indexOf(splitSearch[x]) === 0){ if(splitData[y].indexOf(splitSearch[x]) === 0){
var newScore = splitSearch[x].length / splitData[y].length;
highest = { highest = {
pass: true, pass: true,
score: splitSearch[x].length / splitData[y].length score: highest.score < newScore ? newScore : highest.score
}; };
} }
// Get the levenshtein similarity score for the two words // Get the levenshtein similarity score for the two words
var steps = levenshtein(splitSearch[x], splitData[y]).similarity; var steps = levenshtein(splitSearch[x], splitData[y]).similarity;
// If the levenshtein similarity score is better than a previous one for the search word then let's store it // If the levenshtein similarity score is better than a previous one for the search word then var's store it
if(steps > highest.score) { if(steps > highest.score) {
highest.score = steps; highest.score = steps;
} }
@ -152,7 +153,7 @@
} }
} }
// Check that all of the rows have a score greater than 0.5 // Check that all of the search words have passed
for(var i = 0; i < highestCollated.length; i++) { for(var i = 0; i < highestCollated.length; i++) {
if(!highestCollated[i].pass) { if(!highestCollated[i].pass) {
return { return {
@ -176,24 +177,28 @@
if (settings.aoData[dataIndex]._fuzzySearch !== undefined) { if (settings.aoData[dataIndex]._fuzzySearch !== undefined) {
// Read score to set the cell content and sort data // Read score to set the cell content and sort data
var score = settings.aoData[dataIndex]._fuzzySearch.score; var score = settings.aoData[dataIndex]._fuzzySearch.score;
settings.aoData[dataIndex].anCells[initial.rankColumn].innerHTML = score;
if (initial.rankColumn !== undefined) {
// Remove '%' from the end of the score so can sort on a number settings.aoData[dataIndex].anCells[initial.rankColumn].innerHTML = score;
settings.aoData[dataIndex]._aSortData[initial.rankColumn] = +score.substring(0, score.length - 1);
// Remove '%' from the end of the score so can sort on a number
settings.aoData[dataIndex]._aSortData[initial.rankColumn] = +score.substring(0, score.length - 1);
}
// Return the value for the pass as decided by the fuzzySearch function // Return the value for the pass as decided by the fuzzySearch function
return settings.aoData[dataIndex]._fuzzySearch.pass; return settings.aoData[dataIndex]._fuzzySearch.pass;
} }
else { else if (initial.rankColumn !== undefined) {
settings.aoData[dataIndex].anCells[initial.rankColumn].innerHTML = ''; settings.aoData[dataIndex].anCells[initial.rankColumn].innerHTML = '';
settings.aoData[dataIndex]._aSortData[initial.rankColumn] = ''; settings.aoData[dataIndex]._aSortData[initial.rankColumn] = '';
} }
return true; return true;
} }
); );
$(document).on('init.dt', function(e, settings) { $(document).on('init.dt', function(e, settings) {
let api = new $.fn.dataTable.Api(settings); var api = new $.fn.dataTable.Api(settings);
var initial = api.init().fuzzySearch; var initial = api.init().fuzzySearch;
// If this is not set then fuzzy searching is not enabled on the table so return. // If this is not set then fuzzy searching is not enabled on the table so return.
@ -202,17 +207,17 @@
} }
// Find the input element // Find the input element
let input = $('div.dataTables_filter input', api.table().container()) var input = $('div.dataTables_filter input', api.table().container())
let fontBold = { var fontBold = {
'font-weight': '600', 'font-weight': '600',
'background-color': 'rgba(255,255,255,0.1)' 'background-color': 'rgba(255,255,255,0.1)'
}; };
let fontNormal = { var fontNormal = {
'font-weight': '500', 'font-weight': '500',
'background-color': 'transparent' 'background-color': 'transparent'
}; };
let toggleCSS = { var toggleCSS = {
'border': 'none', 'border': 'none',
'background': 'none', 'background': 'none',
'font-size': '100%', 'font-size': '100%',
@ -224,7 +229,7 @@
} }
// Only going to set the toggle if it is enabled // Only going to set the toggle if it is enabled
let toggle, tooltip, exact, fuzzy, label; var toggle, tooltip, exact, fuzzy, label;
if(initial.toggleSmart) { if(initial.toggleSmart) {
toggle =$('<button class="toggleSearch">Abc</button>') toggle =$('<button class="toggleSearch">Abc</button>')
.insertAfter(input) .insertAfter(input)
@ -269,7 +274,7 @@
.append(label).append(exact).append(fuzzy); .append(label).append(exact).append(fuzzy);
} }
function toggleFuzzy() { function toggleFuzzy(event) {
if(toggle.attr('blurred')) { if(toggle.attr('blurred')) {
toggle.css({'filter': 'blur(0px)'}).removeAttr('blurred'); toggle.css({'filter': 'blur(0px)'}).removeAttr('blurred');
fuzzy.removeAttr('highlighted').css(fontNormal); fuzzy.removeAttr('highlighted').css(fontNormal);
@ -280,7 +285,9 @@
exact.removeAttr('highlighted').css(fontNormal); exact.removeAttr('highlighted').css(fontNormal);
fuzzy.attr('highlighted', true).css(fontBold); fuzzy.attr('highlighted', true).css(fontBold);
} }
triggerSearchFunction();
// Whenever the search mode is changed we need to re-search
triggerSearchFunction(event);
} }
// Turn off the default datatables searching events // Turn off the default datatables searching events
@ -297,7 +304,6 @@
settings.aoData[rowIdx]._fuzzySearch = undefined; settings.aoData[rowIdx]._fuzzySearch = undefined;
}) })
api.search(input.val()) api.search(input.val())
api.draw();
} }
// Otherwise perform a fuzzy search // Otherwise perform a fuzzy search
else { else {
@ -316,8 +322,9 @@
// Empty the datatables search and replace it with our own // Empty the datatables search and replace it with our own
api.search(""); api.search("");
input.val(searchVal); input.val(searchVal);
api.draw();
} }
api.draw();
} }
} }
@ -325,13 +332,9 @@
// Set listeners to occur on toggle and typing // Set listeners to occur on toggle and typing
if(toggle) { if(toggle) {
// Highlights one of the buttons in the tooltip and un-highlights the other // Highlights one of the buttons in the tooltip and un-highlights the other
function highlightButton(toHighlight, noHighlight) { function highlightButton(toHighlight, event) {
if(!toHighlight.attr('highlighted')){ if(!toHighlight.attr('highlighted')){
toHighlight.attr('highlighted', true); toggleFuzzy(event)
toHighlight.css(fontBold);
noHighlight.css(fontNormal);
noHighlight.removeAttr('highlighted');
toggleFuzzy()
} }
} }
@ -347,8 +350,8 @@
tooltip tooltip
.insertAfter(toggle) .insertAfter(toggle)
.on('mouseleave', removeToolTip); .on('mouseleave', removeToolTip);
exact.on('click', () => highlightButton(exact, fuzzy)); exact.on('click', (event) => highlightButton(exact, event));
fuzzy.on('click', () => highlightButton(fuzzy, exact)); fuzzy.on('click', (event) => highlightButton(fuzzy, event));
}) })
.on('mouseleave', removeToolTip); .on('mouseleave', removeToolTip);
@ -358,8 +361,8 @@
tooltip tooltip
.insertAfter(toggle) .insertAfter(toggle)
.on('mouseleave', removeToolTip); .on('mouseleave', removeToolTip);
exact.on('click', () => highlightButton(exact, fuzzy)) exact.on('click', (event) => highlightButton(exact, event))
fuzzy.on('click', () => highlightButton(fuzzy, exact)) fuzzy.on('click', (event) => highlightButton(fuzzy, event))
}) })
.on('mouseleave', function() { .on('mouseleave', function() {
var inToolTip = false; var inToolTip = false;
@ -374,16 +377,17 @@
var state = api.state.loaded(); var state = api.state.loaded();
if(state !== null && state._fuzzySearch === 'true') {
toggle.click();
}
api.on('stateSaveParams', function(e, settings, data) { api.on('stateSaveParams', function(e, settings, data) {
data._fuzzySearch = toggle.attr('blurred'); data._fuzzySearch = {
active: toggle.attr('blurred'),
val: input.val()
}
}) })
api.state.save(); if(state !== null && state._fuzzySearch !== undefined && state._fuzzySearch.active === 'true') {
input.val(state._fuzzySearch.val);
toggle.click();
}
} }
// Always add this event no matter if toggling is enabled // Always add this event no matter if toggling is enabled

Loading…
Cancel
Save