0

I am using Ionic 3/Anuglar 5 am trying to setup a floating horizontal scroll bar for a very tall table in a component . I have come across this solution which should work nicely. Floating horizontal scroll bar for html table

http://jsfiddle.net/cowboy/45rEs/show/

The issue I have is I don't know how to or exactly where to implement the following code into my component:

$('.your-div').floatingScrollbar();

So far I have imported the js plugin into my index:

<script src="assets/jquery.ba-floatingscrollbar.min.js"></script>

I just don't know what to do in my component?

import { Component } from '@angular/core';
@Component({
  selector: 'tall-table',
  templateUrl: 'tall-table.html'
})
export class TallTableComponent {

  constructor() {
  }

  /*
  ** name: Ng On Init
  ** desc: 
  */
  ngOnInit(): void { 
  }

  /*
  ** name: Destroy
  ** desc: Unsubscribe
  */
  ngOnDestroy(): void {
  }

}

tall-table.html

<div id="horz-scroll-wrapper" style="width: 300px; overflow: scroll">
    <div style="height: 100%;">
        <table class='sample'>
            <tbody>
                <tr *ngFor="let row of [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]">
                    <td >abcdefghijklmnopqrstuvwxyz</td><td >ABCDEFGHIJKLMNOPQRSTUVWXYZ</td><td >1234567890</td><td >0987654321</td><td >abcdefghijklmnopqrstuvwxyz</td><td >ABCDEFGHIJKLMNOPQRSTUVWXYZ</td><td >1234567890</td><td >0987654321</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
1
  • Use ngAfterViewInit() method. Commented Jun 26, 2018 at 7:13

1 Answer 1

1

The first thing you will have to do is to setup the jQuery in your Angular project (if you have not done). To use jQuery with Angular, you will have to install jQuery using npm.

You can refer following links to setup jQuery in Angular project:

https://medium.com/@swarnakishore/how-to-include-and-use-jquery-in-angular-cli-project-592e0fe63176

https://stackoverflow.com/a/44653848/4140916

Once you have completed that, you should put corresponding jQuery in ngAfterViewInit life-cycle hook and it should work then.

ngOnInit(): void { 
   $('.your-div').floatingScrollbar();
}

You can read more about life-cycle hooks here: https://angular.io/guide/lifecycle-hooks

If still it does not work, you should try some npm package for floating scrollbar (built specifically for Angular) like following one: https://www.npmjs.com/package/ngx-scrollbar

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I will give this a go. Looking at the ngx-scrollbar unfortunately it does not have the floating property I am after.
Thanks I was able to install jquery but still have issues actually importing the plugin into the component. I think I need to define the typescript file for this plugin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.