3

I've tried to embed a small script within (or just outside) my template tags, without success. The script is never executed.

In my case I'm looking to execute this script:

<script type="text/javascript">
    function DoubleScroll(element) {
        var scrollbar= document.createElement('div');
        scrollbar.appendChild(document.createElement('div'));
        scrollbar.style.overflow= 'auto';
        scrollbar.style.overflowY= 'hidden';
        scrollbar.firstChild.style.width= element.scrollWidth+'px';
        scrollbar.firstChild.style.paddingTop= '1px';
        scrollbar.firstChild.appendChild(document.createTextNode('\xA0'));
        scrollbar.onscroll= function() {
            element.scrollLeft= scrollbar.scrollLeft;
        };
        element.onscroll= function() {
            scrollbar.scrollLeft= element.scrollLeft;
        };
        element.parentNode.insertBefore(scrollbar, element);
    }

    DoubleScroll(document.getElementById('doublescroll'));
</script>

from How can I get horizontal scrollbars at top and bottom of a div?, to add a horizontal scrollbar at the top of my table.

<div class="table-responsive">
    <table class="table table-bordered table-striped">
        // ... a ton of rows
    </table>
</div>

Or what might a better (aurelia approach be)? Any ideas?

Here's how I imagine, but I can't seem to figure the last parts, and I don't even know if this is the right way to think about it.

  1. add the dummy element with a ref attribute
  2. create an attach function inside the view controller in Aurelia
  3. somehow add the styles to the element fetched by the ref as this.[yourref]

Or could you avoid having to place a dummy div inside the template and actually to what the script above does, which is dynamically adding it. Also multiple table exist in the same view, so having to use ref is perhaps not the best way :/

Any further ideas would be greatly appreciated. It should be fairly easy to make a horizontal top scrollbar right? :-)

http://plnkr.co/edit/N9dRxG?p=info

Community
  • 1
  • 1
Dac0d3r
  • 2,176
  • 6
  • 40
  • 76
  • Possible duplicate of [What is the Aurelia way of adding a horizontal top scrollbar to a table?](http://stackoverflow.com/questions/33317262/what-is-the-aurelia-way-of-adding-a-horizontal-top-scrollbar-to-a-table) – nemesv Oct 24 '15 at 11:03
  • opps just deleted it. Didn't know I had created 2. :) – Dac0d3r Oct 24 '15 at 11:10

1 Answers1

7

create a custom attribute for this- here's an example:

double-scroll.js

import {inject} from 'aurelia-framework';

@inject(Element)
export class DoubleScrollCustomAttribute {
  constructor(element) {
    this.element = element; // this is the element the double-scroll attribute appears on.
  }

  attached() {
    let element = this.element;
    // contents of your DoubleScroll function from your original post
  }

  detached() {
    let element = this.element;
    // optional:  tear-down double scroll (unsubscribe events, etc)
  }
}

Then change your markup to:

app.html

<require from="./double-scroll"></require>

<div class="table-responsive">
    <table class="table table-bordered table-striped" double-scroll>
        // ...
    </table>
</div>

(the custom attribute class was exported as DoubleScrollCustomAttribute... aurelia strips the CustomAttribute and hyphenates the name resulting in the double-scroll attribute you see on your table element above.

Working plunker: http://plnkr.co/edit/SYHXBO?p=preview

For more info on custom attributes check out the docs at http://aurelia.io

Jeremy Danyow
  • 26,470
  • 12
  • 87
  • 133
  • Thanks Jeremy for pointing me in the right direction, however I still cannot get it to work: https://jsfiddle.net/mdmhgcqm/ - even the alert is never printed out. What am I doing wrong? :) – Dac0d3r Oct 24 '15 at 15:55
  • Sure I'm on it. Should be up in like 15 minutes :) – Dac0d3r Oct 24 '15 at 16:06
  • 1
    you were close- edited my answer. All that was missing was to pull the custom attribute resource into your view using `` – Jeremy Danyow Oct 24 '15 at 16:39
  • Haha Yes now it works. Thank you so much! Thought I only had to require custom elements, since I thought of it as including a view :/ adding did indeed work! Awesome! Ps. also had to add double-scroll to the div containing the element. – Dac0d3r Oct 24 '15 at 16:42