Column Search with Numeric Values

Column Search with Numeric Values

BTW8892BTW8892 Posts: 3Questions: 1Answers: 0
edited February 2020 in Free community support

I currently have a DataTable that has a column containing a string of numeric values such in the format of '14,11,10'. They can only be a maximum of two digits long, but the number of values can vary. It could be a single value such as '11' or as many as four values within the string '16,14,10,21'.

In addition, I have a match handler and a select2 box that contains all of the potential number values. A user can either select to match 'Any', which will match every result if the number string contains at least one of the selected numbers. Selecting 'Match All' will only search for the specific combination, no matter the order of the numbers.

<select class="form-control form-control-alternative" id="syngery_match" name="syngery_match">
    <option value="any">Match Any</option>
    <option value="all">Match All</option>
</select>

<select class="form-control form-control-alternative synergies-multi select2-hidden-accessible" name="synergies[]" multiple="" id="synergies" data-select2-id="synergies" tabindex="-1" aria-hidden="true">
    <option value="1">NP</option>
    <option value="2">BL</option>
    <option value="3">X</option>
    <option value="4">BM</option>
    <option value="5">TN</option>
    <option value="6">DK</option>
    <option value="7">AD</option>
    <option value="8">WK</option>
    <option value="9">WM</option>
    <option value="10">RS</option>
    <option value="11">M</option>
    <option value="12">1T</option>
    <option value="13">TK</option>
    <option value="14">WC</option>
    <option value="15">SP</option>
    <option value="16">FB</option>
    <option value="18">HH</option>
    <option value="19">DZ</option>
    <option value="20">CP</option>
    <option value="21">HT</option>
    <option value="22">CS</option>
</select>

Match Any

I have this part working aside from one minor issue with the single digit numbers.

Example Input: [1,22,16]

if ($("#syngery_match").val() == "any")
{
    var any_string = $(this).val().toString();
    var select_snys = any_string.replace(',', '|');
    players_table.columns("#synergies_column").search(select_snys, true, false).draw();
}

Since 1 was included in the string of numbers, it returns rows that contain values such as 11,12,13....19, 21, etc. How would I go about altering my code so the rows returned have to contain the exact number in the string and not just a part of the number?

Match All

This part is also partially working. The only issue here is that the string of numbers entered only returns rows with the numbers in that exact order.

Target Row: [13,15,6]

Example Input: [6,13,15]
Example Input 2: [15,13,6]

The two example inputs return no results as the numbers were entered out of order compared to the column on the table.

else if ($("#syngery_match").val() == "all")
{
    var any_string = $(this).val().toString();
    players_table.column("#synergies_column").search(any_string, true, false).draw();
}

Hopefully I was able to explain my situation clearly enough. Any help would be appreciated!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921
    Answer ✓

    Thanks for the good/detailed description. You can use the site https://regex101.com/ to workout your regex expressions.

    For the first you can probably use the word boundary token (\b). The regex expression for [1,22,16] would look something like this \b1\b|\b22\b|\b16\b.

    The Match All case is more complex. Not sure you can use a simple search for this. Creating a regex expression might be complex. I would consider using a Search Plugin for this. Within the plugin I would create two arrays; one of the search input and the other of the column data. Then compare the two arrays for the All match.

    If you want help with either of these please put together is simple example with your data so we can offer suggestions with the code.

    Kevin

  • BTW8892BTW8892 Posts: 3Questions: 1Answers: 0
    edited February 2020

    Your answer for the Match Any part of my issue worked perfectly.

    I was able to figure out the Match All part by adding an additional hidden column to the table and converted the array of numbers to a string, separated by commas.

    I then went ahead and used the array of the search input to generate the regular expression (which I tested on https://regex101.com/).

    Here is my final code.

    else if ($("#syngery_match").val() == "all")
    {
        $.each(synergy_array,function(i){
            all_array[i] = '(?=.*\\b' + synergy_array[i] + '\\b)';
        });
    
        var allstring_final = all_array.join('#').replace(/\#/g, '');
        players_table.column("#synergy_ids_column").search('^' + allstring_final + '.*$', true, false).draw();
    }
    
This discussion has been closed.