• Resolved Buschtrommel

    (@buschtrommel)


    Hi,

    I love this plugin – but now I found me limit.

    I have a table full of users. In the front-end the user can search after the postcode.
    I reduce the search the postcode column through this code:
    “aoColumnDefs”: [ { “bSearchable”: false, “aTargets”: [ 0,1,2,4,5,6,7 ] } ]

    Works fine. BUT: When I search the 2 first digits like 72 – it shows me all fields with 72.

    Example: Search 72
    724628 (its ok – starts with 72)
    72938 (its ok – starts with 72)
    327292 (Wrong is only in the middle)
    92ß72 (Wrong is only in the end)

    Any idea?

    Thx.

    http://ww.wp.xz.cn/plugins/tablepress/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    thanks for your question, and sorry for the trouble.

    I can see what you are trying to do, but unfortunately, I’m not aware of a possibility to change this search behavior 🙁 As I’m not the developer of the external DataTables JS library that TablePress uses for this, and as I therefore don’t know all the details about it, I can only suggest that you check the documentation at http://www.datatables.net for more information or ask in the DataTables forums there.

    Regards,
    Tobias

    Thread Starter Buschtrommel

    (@buschtrommel)

    Hi Tobias,

    thank you.
    After one day in the other forum here is my idea:
    Enable regex filter and search with ^
    ^ <- Starts with the first letter.

    My question? How 😀 ?

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    ah, indeed, nice idea.

    I haven’t tested it, but this code should achieve this:

    add_filter( 'tablepress_datatables_command', 'tablepress_filter_on_enter', 10, 5 );
    function tablepress_filter_on_enter( $command, $html_id, $parameters, $table_id, $js_options ) {
    	if ( ! in_array( $table_id, array( '1', '3' ) ) )
    		return $command;
    
    	$name = 'DT_' + str_replace( '-', '_', $html_id );
    	$command = <<<JS
    var {$name} = $('#{$html_id}').dataTable({$parameters});
    $('#{$html_id}_filter input').unbind().bind('keyup', function(e) {
    	{$name}.fnFilter( '^' + this.value, null, true );
    });
    JS;
    	return $command;
    }

    Just paste that into a new small plugin or into your theme’s “functions.php” file. Also, the line

    if ( ! in_array( $table_id, array( '1', '3' ) ) )

    needs to be adjusted, so that the array contains all the table IDs for which you want this search behavior.

    Regards,
    Tobias

    Thread Starter Buschtrommel

    (@buschtrommel)

    Thank you for your hard work & help.
    Hmmm – but the code failed.

    I insert both in my theme function but now the search field is not there.
    http://www.bundesverband-brennholz.de/?page_id=3591

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    ah, my bad. There’s a small typo in the code above…

    Please change

    $name = 'DT_' + str_replace( '-', '_', $html_id );

    to

    $name = 'DT_' . str_replace( '-', '_', $html_id );

    Regards,
    Tobias

    Thread Starter Buschtrommel

    (@buschtrommel)

    Just this 4 u : <3

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    🙂
    Good to hear that this helps!

    We might need one more modification though. It looks like the search field now gets the ^ in it as well. Please try this:
    Replace

    {$name}.fnFilter( '^' + this.value, null, true );

    with

    var search_term = this.value;
    {$name}.fnFilter( '^' + search_term, null, true );
    $(this).val( search_term );

    Regards,
    Tobias

    Thread Starter Buschtrommel

    (@buschtrommel)

    Works !!! Thank you so much!!!!
    A good idea and your knowledge.

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    great. Looks like that worked 🙂

    Best wishes,
    Tobias

    P.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Search the first char’ is closed to new replies.