javascriptangulartypescriptangular-gridster2

How to change the gridster2 options value when clicking the button in angular


here's the code & output: https://stackblitz.com/edit/d3-angular-gridster2-working-axhc7u?file=src%2Fapp%2Fgrid%2Fgrid.component.html

GRID-HTML

<gridster [options]="options">
  <gridster-item [item]="item" *ngFor="let item of dashboard">
   
  </gridster-item>
</gridster>

GRID-TS

ngOnInit() {
  @Input() editing: any;
    this.options = {
      gridType: GridType.Fit,
      displayGrid: DisplayGrid.Always,
      enableEmptyCellClick: false,
      enableEmptyCellContextMenu: false,
      enableEmptyCellDrop: false,
      enableEmptyCellDrag: false,
      enableOccupiedCellDrop: false,
      emptyCellClickCallback: this.emptyCellClick.bind(this),
      emptyCellContextMenuCallback: this.emptyCellClick.bind(this),
      emptyCellDropCallback: this.emptyCellClick.bind(this),
      emptyCellDragCallback: this.emptyCellClick.bind(this),
      emptyCellDragMaxCols: 50,
      emptyCellDragMaxRows: 50
    };

    this.dashboard = [
      { cols: 2, rows: 1, y: 0, x: 0 },
      { cols: 2, rows: 2, y: 0, x: 2 },
      { cols: 1, rows: 1, y: 0, x: 4 },
      { cols: 3, rows: 2, y: 1, x: 4 },
      { cols: 1, rows: 1, y: 4, x: 5 },
      { cols: 1, rows: 1, y: 2, x: 1 },
      { cols: 2, rows: 2, y: 5, x: 5 },
      { cols: 2, rows: 2, y: 3, x: 2 },
      { cols: 2, rows: 1, y: 2, x: 2 },
      { cols: 1, rows: 1, y: 3, x: 4 },
      { cols: 1, rows: 1, y: 0, x: 6 }
    ];
  }

What I'm trying to do here is to enabled the enableEmptyCellDrag. for example I clicked the button edit then the value of edit is true and then the value of enableEmptyCellDrag is true.

I already tried this:

ngOnChanges() {
 ///this.options['enableEmptyCellDrag'] = true // the enableEmptyCellDrag is undefined
 ///if (this.editing) {
 /// this.options['enableEmptyCellDrag'] = true // the value of enableEmptyCellDrag is change to true, but when I try to drag from the empty cell it doesn't work
 ///}

}

Solution

  • If I understand correctly, you want to set this.options['enableEmptyCellDrag'] to the value of @Input() editing.

    And you want to gridster2 (I must admit I don't know what this is) to recognise the change.

    So you have 2 problems:

    1. When you're in ngOnChanges, accessing your @Input() directly will give you the "old" value.

    2. Usually, for Angular to detect changes in objects, you need to change the reference of the object.

    So this is what your ngOnChanges should look like.

      ngOnChanges(changes: SimpleChanges) {
        if (changes.editing && changes.editing.currentValue) {
          // Solve problem 1)
          const newValueOfEditingInput = changes.editing.currentValue;
    
          // Solve Problem 2) - Create a new reference for this.options, so that angular (grister2) can detect the change
          this.options = {
            ...this.options,
            enableEmptyCellDrag: newValueOfEditingInput
          };
        }
      }
    

    Of Course I haven't tested this but I hope it can help you