Giter Site home page Giter Site logo

Comments (3)

lincolnthree avatar lincolnthree commented on May 25, 2024

You can do this using DOM listeners and the "scrollContainer.scrollToPosition" method. Just keep track of which element is selected, then calculate the position of the next/previous one, and call the method

Personally I don't think this is something that should be "built in". But all the methods/data to make it possible are here already.

from ngx-virtual-scroller.

weilinzung avatar weilinzung commented on May 25, 2024

@lincolnthree that sounds workable, do you have an example? we are trying to write our custom method with CDK focusKeyManager, but the logic isn't that simple.

from ngx-virtual-scroller.

lincolnthree avatar lincolnthree commented on May 25, 2024

Unfortunately not a good one. It's pretty embedded in my application, but here's the general idea:

This would track the index of the selected item (and presumably highlight it)

      {
        key: ["up"],
        label: "Editor",
        description: "Previous card in list",
        preventDefault: true,
        command: async (e) => {
          if (!InputUtil.isInputFocused() && this.controller.list.isActive()) {
            if (this.controller.list.canShiftIndex(-1)) {
              UIEvents.stopPropagation(e.event);
              this.controller.list.shiftIndex(-1); 
            }
          }
        }
      },
  {
        key: ["down"],
        label: "Editor",
        description: "Next card in list",
        preventDefault: true,
        command: async (e) => {
          if (!InputUtil.isInputFocused() && this.controller.list.isActive()) {
            if (this.controller.list.canShiftIndex(1)) {
              UIEvents.stopPropagation(e.event);
              this.controller.list.shiftIndex(1); 
            }
          }
        }
      }

Some code for figuring out the item height based on our component / wrapper for the virtual list:


    this.list = this.lists.createListManager(CARDS_OVERVIEW_LIST, {
      dimensions: () => {
        return this.grid?.scroll
          ? { childHeight: this.grid.scroll.itemHeight, itemsPerWrapGroup: this.grid.scroll.itemsPerRow }
          : { childHeight: CARD_ITEM_HEIGHT, itemsPerWrapGroup: 1 };
      },
      scrollOnIndexChanged: false,
      scrollTo: async (_x: number, y: number) => {
        this.scrollTo(y + Math.floor(this.grid.scroll._virtualWindow?.nativeElement?.offsetTop ?? 0))
      }
    });

HostListeners for keyboard binding events in Angular:

  @HostListener('document:keydown.ArrowUp', ['$event']) onPrevHandler(_event: KeyboardEvent) {
    if (this.isPrevButtonVisible && !this.isFirstStep()) {
      this.prev();
    }
  }

from ngx-virtual-scroller.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.