nativescript-angular icon indicating copy to clipboard operation
nativescript-angular copied to clipboard

Width of GridLayout

Open jsparks19 opened this issue 10 years ago • 5 comments

I have an 8x8 GridLayout with each cell having a fixed width of 30. I thought this would mean the width of the layout would be 240, but the touch event listener attached to it is responding to touch events that far exceed that, e.g. x = 295. Does the GridLayout have a fixed width? How do I get the width of it programmatically? Thanks!

Here is the template code. {{itemSpec}} evaluates to "30,30,30,30,30,30,30,30".

<StackLayout>
   <GridLayout columns="{{itemSpec}}" rows="{{itemSpec}}"
            horizontalAlignment="center" verticalAlignment="center"
            (touch)="onTouch($event)">
      <template ngFor #row [ngForOf]="puzzle" #y="index">
         <template ngFor #col [ngForOf]="row" #x="index">
             <Label id="letter" [col]="x" [row]="y" [text]="col">
            </Label>
         </template>
     </template>
  </GridLayout>
</StackLayout>

jsparks19 avatar May 25 '16 01:05 jsparks19

My Presumption as its a stack layout as the parent ... would be that the Grid layout takes the entire width available, but the columns take a subset of those widths as defined in the column spec?

As for getting at the grid that is quite easy:

<GridLayout #PuzzleGrid columns="{{itemSpec}}" rows="{{itemSpec}}"
            horizontalAlignment="center" verticalAlignment="center"
            (touch)="onTouch($event)">
      <template ngFor #row [ngForOf]="puzzle" #y="index">
         <template ngFor #col [ngForOf]="row" #x="index">
             <Label id="letter" [col]="x" [row]="y" [text]="col">
            </Label>
         </template>
     </template>
  </GridLayout>
@ViewChild('PuzzleGrid ') private puzzleGrid : ElementRef

function CheckWidth() {
   let grid : GridLayout = this.gridRef.nativeElement;
   // do whatever with the grid. 
   // check width etc 
   // cols etc 
}

matt4446 avatar May 25 '16 13:05 matt4446

@matt4446 I added the code from your example and I'm getting NaN for the grid width and height. I wanted to see if any of the grid's other properties had values, so I called getRows.length and it returned zero, which I found odd but maybe I'm not accessing that correctly. I also tried putting the code that accesses the grid in an ngAfterViewInit function, but that didn't work. Any ideas what I'm doing wrong? Thanks!

<StackLayout>
    <GridLayout #PuzzleGrid columns="{{itemSpec}}" rows="{{itemSpec}}"
            (touch)="onTouch($event)">
        <template ngFor #row [ngForOf]="puzzle" #y="index">
            <template ngFor #col [ngForOf]="row" #x="index">
                <Label id="letter" [col]="x" [row]="y" [text]="col">
                </Label>
            </template>
        </template>
    </GridLayout>
</StackLayout>
export class PuzzleComponent {

    @ViewChild('PuzzleGrid') private puzzleGrid : ElementRef

    constructor(private puzzleService: PuzzleService) { }

    puzzle = this.puzzleService.getPuzzle();

    itemSpec = this.createItemSpec(this.puzzle.length);

    private createItemSpec(length) {
        let itemSpec = [];
        for (var i=0; i<length; i++) {
            itemSpec.push("30");
        }
        return itemSpec.join(",");
    }

    onTouch(args: TouchGestureEventData) {
        let grid : GridLayout = this.puzzleGrid.nativeElement;
        console.log("Width: " + grid.width);
        console.log("Number of rows: " + grid.getRows.length);
    }

}

jsparks19 avatar May 26 '16 01:05 jsparks19

Indeed width and height properties define the size that is used to measure control (GridLayout). In order to get the actual size there is a dedicated method for that getActualSize it returns size (width and height). Be aware that result of this method differs if control is just initialized or completely rendered.

nsndeck avatar May 26 '16 05:05 nsndeck

@nsndeck @matt4446 getActualSize returned the width and height. It was 240 for both, which is what I expected. I'm still puzzled about the behavior I'm seeing when attaching a touch event handler to the GridLayout. Even though the GridLayout is 240 pixels, if I press outside of the grid it is still triggering a touch. I tried removing the StackLayout parent and have the same result. Is this expected behavior? Is the GridLayout not intended to have a touch sensor attached to it? Thanks

jsparks19 avatar May 27 '16 01:05 jsparks19

New observation: when the GridLayout is contained within the StackLayout, the only time I'm able to touch points beyond the boundaries of my grid is when the touch action is "up". I think that makes sense now. So maybe I could make this work by just checking if the X or Y is greater than the values from getActualSize and if so adjust it accordingly.

When the GridLayout doesn't have a parent layout I can touch anywhere outside the grid and trigger the touch event handler. That doesn't seem correct to me, but maybe I'm missing something.

jsparks19 avatar May 27 '16 01:05 jsparks19