Search in Datatable so that I can search Column starts from specific alphabet by excluding prefixes?

Search in Datatable so that I can search Column starts from specific alphabet by excluding prefixes?

pankajomsahupankajomsahu Posts: 5Questions: 2Answers: 0

If I Click on T, I want only those records which which have suffixes 'The', 'The Uttar Pradesh' and just after the word which is starting from T or the word starting from T itself and it is not 'The', 'The Uttar Pradesh'.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    How are you performing the search at the moment? Our alphabet search plug-in does what you are looking for.

    Allan

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    edited July 2022

    I think the op is talking about skipping leading articles in text.
    I have a lot of titles in my data, and my solution is to store leading articles in a separate column and concatenate them back for displaying. Initially more complex, and not for everyone, but makes search and sort more logical.

  • pankajomsahupankajomsahu Posts: 5Questions: 2Answers: 0

    Thank You @tangerine for showing interest here.
    But Simple Alphabet Sorting is working. I'm looking for my specific case if you understand.Please go through the image you will understand what I'm actually looking for. The three results are underlined by ignoring prefixes like 'THE', 'THE UTTAR PRADESH' & 'UTTAR PRADESH'. There will be a In Image I'm performing operation on 'T' alphabet. There will be one another result which will display All records starting with 'T' AND do not have prefixes mentioned above.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I think we need to go back to my question then. How are you performing that search since the plug-in I linked to is performing the way you describe. So I guess you've got some other implementation.

    Allan

  • pankajomsahupankajomsahu Posts: 5Questions: 2Answers: 0

    @allan I just want to ignore some words in the start of string while performing search.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I don't really like repeating myself three times, but you haven't answered my question:

    How are you performing that search

    Allan

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    edited July 2022 Answer ✓

    If I understand correctly you want to ignore leading words like The. Assuming you are using the alphabet search plugin Allan linked to you can change the search pluglin to remove these leading words before doing the compare. The he search plugin looks like this:

    $.fn.dataTable.ext.search.push( function ( context, searchData ) {
        // Ensure that there is a search applied to this table before running it
        if ( ! context.alphabetSearch ) {
            return true;
        }
     
        if ( searchData[0].charAt(0) === context.alphabetSearch ) {
            return true;
        }
     
        return false;
    } );
    

    Lines 7-9 perform the comparison. You can use a regex expression to remove the leading words like The then use that result for the if statement. Maybe something like this:

    $.fn.dataTable.ext.search.push( function ( context, searchData ) {
        // Ensure that there is a search applied to this table before running it
        if ( ! context.alphabetSearch ) {
            return true;
        }
        
        var re =  = /^The |^The Uttar Pradesh /;  // Note the trailing space after each word
        var shortTitle = searchData[0].replace(re, "");  // Replace the leading words with empty string
    
        // Compare with the leading articles removed
        if ( shortTitle.charAt(0) === context.alphabetSearch ) {
            return true;
        }
     
        return false;
    } );
    

    Haven't tested this but it should be close :smile: I will leave it to you to test this solution.

    Kevin

  • pankajomsahupankajomsahu Posts: 5Questions: 2Answers: 0
    edited July 2022

    thank you @kthorngren ...

Sign In or Register to comment.