jQuery UI Widgets › Forums › Angular › Error getting data from selected row: "Type 'Number' cannot be used as an index"
Tagged: grid, typescript
This topic contains 2 replies, has 2 voices, and was last updated by Ivo Zhulev 7 years, 5 months ago.
-
Author
-
November 1, 2017 at 11:55 am Error getting data from selected row: "Type 'Number' cannot be used as an index" #97046
Overview: Angular 4 project; Recently realized I was using the plain JavaScript jqxGrid instead of the Angular one, so trying to switch to see if that will let me run tests. My table has row checkboxes to select a row, and upon clicking another button I’m changing the value of a field in that row (kind of like a show/hide toggle). My problem is a Typescript error that seems to say I can’t use a Number variable to access indices within
this.daGrid.attrSource.recordids
even though I can hardcode a number in, as you’ll see in the following function’sconsole.log
statement:setRecordRejection(event: any, status: string) { const rowsToReject = this.daGrid.getselectedrowindexes(); let record: EditedRecord; if (rowsToReject.length !== 0) { rowsToReject.forEach(rowIndex => { console.log(rowIndex, this.daGrid.attrSource.recordids[0]); //works! //I'm using VS Code, so I can see types just by hovering over the property. //rowsToReject is Number[], rowIndex is Number, and recordids is Number[], //so it doesn't make any sense to me why recordids[rowIndex] isn't valid. //No problem with the JS-only jqxGrid, because everything is type:any. const rowdata = this.daGrid.attrSource.recordids[rowIndex]; //TS won't even compile; says "Type 'Number' cannot be used as an index type" if (status === 'reject') { record = { recordId: rowdata.recordId, status: 'Y' }; // TODO: call rejectSelected on the service // if that resolves, update the grid as follows rowdata['rejected'] = 'Y'; rowdata['rejectedMap'] = 'Y'; this.daGrid.updaterow(rowIndex, rowdata); } else { record = { recordId: rowdata.recordId, status: '' }; // TODO: call rejectSelected on the service // if that resolves, update the grid as follows rowdata['rejected'] = ''; rowdata['rejectedMap'] = 'N'; this.daGrid.updaterow(rowIndex, rowdata); } }); } }
November 1, 2017 at 4:33 pm Error getting data from selected row: "Type 'Number' cannot be used as an index" #97056I believe I finally discovered the answer.
The problem is that
getselectedrowindexes()
returns Number[] which, according to Typescript’s Do’s and Dont’s, should never be used because “they are almost never used appropriately in JavaScript” which is the case here, as though you get an array of indices, you can’t use single values in aforEach
to access that array since array accessors need to benumber
and notNumber
.The odd thing is that
getselectedrowindex
just returnsnumber
so this appears to be a bug in the jqxGridComponent code that needs to be fixed.My solution then is to explicitly cast to
number
in my forEach loop:const rowsToReject = this.daGrid.getselectedrowindexes(); rowsToReject.forEach((rowIndex:number) => { ... });
November 6, 2017 at 9:17 am Error getting data from selected row: "Type 'Number' cannot be used as an index" #97114 -
AuthorPosts
You must be logged in to reply to this topic.