filtering - Page 2

filtering

2»

Answers

  • kthorngrenkthorngren Posts: 10,561Questions: 25Answers: 2,405

    I saw that error I think Wasn't sure yet where it was coming from. But it makes sense that it is from the plugin. Thanks!

    Kevin

  • kthorngrenkthorngren Posts: 10,561Questions: 25Answers: 2,405
    edited December 2019

    That was an easy fix. I just added a check for rowPosition < 0 to handle the case where the row index is not found using indexOf(). It stays on the current page.
    http://live.datatables.net/sijixiho/6/edit

    Kevin

  • colincolin Posts: 9,777Questions: 0Answers: 1,617

    Thanks, Kevin - all tested and good. I've updated the code, so all good to use.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 10,561Questions: 25Answers: 2,405
    Answer ✓

    DEV kevin's tweak

    I like that :smile:

    Kevin

  • LapointeLapointe Posts: 103Questions: 22Answers: 1

    Hello @kthorngren @colin @Allan
    Thanks all for your work
    I'm just coming back to office now.
    You are right for these 2 points.
    My first question (or second perhaps) was... How to know if a selected row (table.row( {selected: true} )) was visible and if yes how to display it at filter change time...
    the corrected row().show() can take care of filter cative and this is a good point, but if the row is hidden by filter expression, row().show() try to go to page -1... and a cannot read property error is uncaught.
    I'll modify api to set page to 0 if -1...

    $.fn.dataTable.Api.register('row().show()', function() {
        var page_info = this.table().page.info();
        // Get row index
        var new_row_index = this.index();
        // Row position
        var row_position = this.table().rows({search: 'applied' })[0].indexOf( new_row_index );
        // Already on right page ?
        if( row_position >= page_info.start && row_position < page_info.end ) {
            // Return row object
            return this;
        }
        // Find page number
        var page_to_display = Math.floor( row_position / this.table().page.len() );     // page_info.length
        // Go to that page
        this.table().page( Math.max(0,page_to_display) );
        // Return row object
        return this;
    });
    

    Because for me the utility is to unselect row if selected and not visible, I do a test at return to verify if selected is in current displayed page...

                                        table.one( 'draw', function () {
                                            if ( table.row( { selected: true } ).count() > 0 ) {
                                                table.row( {selected: true} ).show().draw(false);
                                                if (table.row( {selected: true, page:'current'} ).count() != 1 ) {  // filtered data not visible
                                                    table.row( {selected: true} ).deselect();
                                                }
                                            }
                                        } );
    

    Thanks all
    PS perhaps an api extension to give param as GoToPageIfNotVisible and boolparam DeselectIfNotVisible should be a way to do that...

                                                table.row( {selected: true} ).show(0, true).draw(false);
    
    
  • LapointeLapointe Posts: 103Questions: 22Answers: 1

    final (tested but... I'll do more test later)

                                        table.one( 'draw', function () {
                                            var curSelection = table.rows( { selected: true } );
                                            var bIsDisplayable = false;
                                            if ( curSelection.count() > 0 ) {
                                                if ( curSelection.count() === 1  ) {
                                                    table.row( { selected: true } ).show().draw(false);
                                                    bIsDisplayable = (table.row( {selected: true, page:'current'} ).count() == 1 );
                                                }
                                                if ( !bIsDisplayable ) {// filtered data not visible or multi select
                                                    curSelection.deselect();
                                                    localStorage.setItem( '<?php echo $TblName.'_'.GetUserID()?>', null );
                                                }
                                           }
                                        } );
    
    
  • LapointeLapointe Posts: 103Questions: 22Answers: 1

    tested and tested...
    At start I'd like to show the selected record, if visible.
    The status visible or not may change at filter set (global or column)
    Concerning global filtering table.one(... is the correct way, but column filter change event not need to use table.one... If using at startup .one( event is not fired and results are nor each time ok.
    So use

                                .on( 'change', function () {
                                    var val = $.fn.dataTable.util.escapeRegex($(this).val());    
                                    column
                                        .search( val ? '^'+val+'$' : '', true, false )
                                        .draw();                                        
                                    var curSelection = table.rows( { selected: true } );
                                    if ( curSelection.count() > 0 ) {
                                        var bIsDisplayable = false;
                                        if ( curSelection.count() === 1  ) {
                                            table.row( { selected: true } ).show().draw(false);
                                            bIsDisplayable = (table.row( {selected: true, page:'current'} ).count() == 1 );
                                        }
                                        if ( !bIsDisplayable ) {// filtered data not visible or multi select
                                            curSelection.deselect();
                                            localStorage.setItem( '<?php echo $TblName.'_'.GetUserID()?>', null );
                                        }
                                   }
                                } );
    
    

    and everything seem to be OK

    Tks a lot @kthorngren , @colin, @allan

  • LapointeLapointe Posts: 103Questions: 22Answers: 1

    about row().show() I did not see you did solve the problem when no row to display using

    if( (row_position >= page_info.start && row_position < page_info.end) || row_position < 0 ) 
    

    Sorry for that. It's all ok now

Sign In or Register to comment.