问题
I have app on Angular 4 + TypeScript + Kendo UI. Also I have a page with table of users. After editing user I want to highlight user by ID that I edited.
users: Observable<Array<User>>;
selectedId: number;
gridView: GridDataResult;
ngOnInit() {
this.users = this.route.params
.switchMap((params: Params) => {
this.selectedId = +params['id'];
return this.adminService.getUsers();
});
});
}
Also I found in Kendo documentation event with "index" and "selected" fields:
selectionChange(event: SelectionEvent): void {
//?
}
I tried to call selectionChange
programmatically in ngOnInit
method:
this.selectionChange({ index: 1, selected: true });
But I don't know what to do to set selected row in gridView
in body of selectionChange
method.
So, what should I do with gridView
to select row? Or may be there is a simpler method to select row by ID.
回答1:
You can select the row manually as below, I have tried in my Kendo grid it works but it may required some polishing.
Define some required variables: (please define missing variables from below code)
rowIndex : number =0;
isSelectedRowChanged: boolean = false;
selectedItem : any;
Define Function to set selected row as below:
public SetSelectedRow(index: number, isManualSelection, isSelected, isCellClick) {
var grid = this.el.nativeElement.getElementsByClassName('k-grid-content')[0];
var rows = grid.getElementsByTagName('tr');
let dataRowIndex = -1;
let selectedRow = grid.getElementsByClassName('k-state-selected')[0];
for (let i = 0; i < rows.length; i++) {
if (rows[i].className.indexOf("k-grouping-row") < 0 && rows[i].className.indexOf("k-group-footer") < 0) {
if (isManualSelection) {
this.rowIndex = 1;
if (selectedRow != null) {
selectedRow.className = String(selectedRow.className).replace(" k-state-selected", '').replace("k-state-selected", '');
}
if (rows[i].className.indexOf("k-state-selected") < 0) {
rows[i].click();
}
//Set selectedItem
for (let k = 0; k < rows[i].children.length; k++) {
if (rows[i].children[k].className.indexOf("k-group-cell") < 0 && rows[i].children[k].children[0] != null) {
rows[i].children[k].children[0].click();
break;
}
}
break;
} else {
if (this.isSelectedRowChanged) {
this.rowIndex = index + 1;
return;
}
if (selectedRow == null) {
//Set selectedItem
rows[i].className = rows[i].className + " k-state-selected";
for (let k = 0; k < rows[i].children.length; k++) {
if (rows[i].children[k].className.indexOf("k-group-cell") < 0 && rows[i].children[k].children[0] != null) {
rows[i].children[k].children[0].click();
break;
}
}
break;
}
else {
dataRowIndex++;
if (!this.isSelectedRowChanged) {
if (isSelected && !isCellClick) {
selectedRow.click();
}
break;
} else {
this.rowIndex = dataRowIndex + 1;
}
}
}
}
}
}
Call below function to set first row selected after assign Data source to grid:
setTimeout(() => {
this.rowIndex = 0;
this.SetSelectedRow(0, true, false, false);
}, 200);
Write below function on Row Selection Change Event:
public OnSelection_Changed(item: any): void {
if (!item.selected) {
this.SetSelectedRow(item.index, false, true, false);
}
else {
this.SetSelectedRow(item.index, false, false, false);
}
}
Also define Cell Click event:
OnCellClick(dataItem, rowIndex, columnIndex) {
if (this.selectedItem != dataItem) {
this.isSelectedRowChanged = true;
this.selectedItem = dataItem;
}
else {
this.isSelectedRowChanged = false;
}
}
回答2:
My temp solution is:
Insert attribute data-id in html:
<kendo-grid-column title="Actions">
<ng-template kendoGridCellTemplate let-user>
<div class="btn-group">
<button class="btn btn-xs btn-default" type="button" data-toggle="tooltip" title="Edit User" [attr.data-id]="user.id"><i class="fa fa-pencil"></i></button>
</div>
</ng-template>
</kendo-grid-column>
And in ts:
gridView: GridDataResult;
selectedId: number;
constructor(private route: ActivatedRoute,
private adminService: AdminService) { }
ngOnInit() {
this.route.params
.switchMap((params: Params) => {
this.selectedId = +params['id'];
return this.adminService.getUsers();
})
.subscribe((response: GridDataResult) => {
this.gridView = response;
this.selectRow();
});
}
selectRow() {
setTimeout(() => {
if (this.selectedId) {
let button = document.querySelector('[data-id="' + this.selectedId + '"]');
if (button) {
let tr = button.parentElement.parentElement.parentElement;
tr.className += " k-state-selected";
}
}
}, 200);
}
来源:https://stackoverflow.com/questions/43581367/how-to-set-selected-row-programmatically-in-kendo-grid-angular-4