Move and delete rows (dynamic table)

This commit is contained in:
Denys Vuika
2016-10-20 12:07:28 +01:00
committed by Vito Albano
parent 8542999f0e
commit 4f566a7ba2
3 changed files with 97 additions and 1 deletions

View File

@@ -43,8 +43,9 @@ export class DynamicTableModel extends FormWidgetModel {
this.rows.forEach(row => row.selected = false); this.rows.forEach(row => row.selected = false);
if (value) {
this._selectedRow = value; this._selectedRow = value;
if (value) {
this._selectedRow.selected = true; this._selectedRow.selected = true;
} }
} }
@@ -65,4 +66,42 @@ export class DynamicTableModel extends FormWidgetModel {
} }
} }
} }
flushValue() {
this.field.value = this.rows.map(r => r.value);
this.field.updateForm();
}
moveRow(row: DynamicTableRow, offset: number) {
let oldIndex = this.rows.indexOf(row);
if (oldIndex > -1) {
let newIndex = (oldIndex + offset);
if (newIndex < 0) {
newIndex = 0;
} else if (newIndex >= this.rows.length) {
newIndex = this.rows.length;
}
let arr = this.rows.slice();
arr.splice(oldIndex, 1);
arr.splice(newIndex, 0, row);
this.rows = arr;
this.flushValue();
}
}
deleteRow(row: DynamicTableRow) {
if (row) {
if (this.selectedRow === row) {
this.selectedRow = null;
}
let idx = this.rows.indexOf(row);
if (idx > -1) {
this.rows.splice(idx, 1);
this.flushValue();
}
}
}
} }

View File

@@ -23,5 +23,32 @@
</tbody> </tbody>
</table> </table>
<div>
<button class="mdl-button mdl-js-button mdl-button--icon"
[disabled]="!hasSelection()"
(click)="moveSelectionUp()">
<i class="material-icons">arrow_upward</i>
</button>
<button class="mdl-button mdl-js-button mdl-button--icon"
[disabled]="!hasSelection()"
(click)="moveSelectionDown()">
<i class="material-icons">arrow_downward</i>
</button>
<button class="mdl-button mdl-js-button mdl-button--icon"
(click)="addNewRow()">
<i class="material-icons">add_circle_outline</i>
</button>
<button class="mdl-button mdl-js-button mdl-button--icon"
[disabled]="!hasSelection()"
(click)="deleteSelection()">
<i class="material-icons">remove_circle_outline</i>
</button>
<button class="mdl-button mdl-js-button mdl-button--icon"
[disabled]="!hasSelection()"
(click)="editSelection()">
<i class="material-icons">edit</i>
</button>
</div>
<span *ngIf="content.validationSummary" class="mdl-textfield__error">{{content.validationSummary}}</span> <span *ngIf="content.validationSummary" class="mdl-textfield__error">{{content.validationSummary}}</span>
</div> </div>

View File

@@ -38,4 +38,34 @@ export class DynamicTableWidget extends WidgetComponent implements OnInit {
this.content.selectedRow = row; this.content.selectedRow = row;
} }
} }
hasSelection(): boolean {
return !!(this.content && this.content.selectedRow);
}
moveSelectionUp() {
if (this.content) {
this.content.moveRow(this.content.selectedRow, -1);
}
}
moveSelectionDown() {
if (this.content) {
this.content.moveRow(this.content.selectedRow, 1);
}
}
addNewRow() {
console.log('add new row clicked');
}
deleteSelection() {
if (this.content) {
this.content.deleteRow(this.content.selectedRow);
}
}
editSelection() {
console.log('edit selection clicked');
}
} }