primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Error thrown when using non-standard [column] objects in p-table with global filter

Open nickiannone opened this issue 8 years ago • 3 comments

I'm submitting a ... (check one with "x")

[ x ] bug report => Search github for a similar issue or PR before submitting
[ ] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35

Plunkr Case (Bug Reports)

http://plnkr.co/edit/Q5RFGs?p=preview

Current behavior When objects that don't match the standard { header: string, value: string } format are passed into [columns] on a p-table component, and a global filter is used, using the global filter field returns no results and an error is generated on the console (from the above plunkr on Firefox):

TypeError: field.indexOf is not a function
Stack trace:
ObjectUtils.prototype.resolveFieldData@https://unpkg.com/[email protected]/components/utils/objectutils.js:68:17
Table.prototype._filter@https://unpkg.com/[email protected]/components/table/table.js:755:100
Table.prototype.filter/this.filterTimeout<@https://unpkg.com/[email protected]/components/table/table.js:691:13
ZoneDelegate.prototype.invokeTask@https://unpkg.com/[email protected]?main=browser:398:17
onInvokeTask@https://unpkg.com/@angular/[email protected]/bundles/core.umd.js:4656:24
ZoneDelegate.prototype.invokeTask@https://unpkg.com/[email protected]?main=browser:397:17
Zone.prototype.runTask@https://unpkg.com/[email protected]?main=browser:165:28
ZoneTask/this.invoke@https://unpkg.com/[email protected]?main=browser:460:28
timer@https://unpkg.com/[email protected]?main=browser:1732:17

Expected behavior Any of the following:

  • The search should fail, but an error message should be generated which indicates that no "field" value was specified in the column data, and this is required for global filtering
  • The search should succeed, using the first field of the objects passed into [columns] (in the above plunkr, name) or the field which matches that passed into the p-sortableColumn for the column (if possible; I know that's a tall order to fill given how p-table's templates are implemented)
  • The table itself should fail fast, with a message that the data passed into [columns] is in the wrong format (expected: strings or objects matching { header: string, field: string })

Minimal reproduction of the problem with instructions

  • Implement a p-table and bind [columns] to an array of objects which do not contain the header and field fields.
  • Incorporate a global filter search bar into the table, as per the PrimeNG Table documentation
  • Load page in a browser
  • Type something into the global filter search bar
  • Observe the above error in the console

What is the motivation / use case for changing the behavior? From the code that prompted this bug report, we're generating a set of column metadata from our API, and passing it to the frontend in a specific format that includes data type. Since column naming, sorting, etc. works when we don't follow the { header: string, value: string } guideline, I'm curious about why global filtering is affected by this, and at the lack of either documentation guidelines regarding this being required, or the lack of error handling (the code spits out an error because it seems like it assumes that the column object value is a string, and tries to call indexOf on it).

Please tell us about your environment: Windows 10 Pro

  • Angular version: 5.2.1

  • PrimeNG version: 5.2.0-rc.2

  • Browser: Tested on Chrome, Firefox, and IE

  • Language: TypeScript 2.6.2

nickiannone avatar Jan 26 '18 21:01 nickiannone

This appears to happen because of this line - https://github.com/primefaces/primeng/blob/master/src/app/components/table/table.ts#L944

globalFilterFieldsArray[j].field doesn't exist on our column object but then the default is just to use globalFilterFieldsArray[j] which results in the column object instead of just the column field name.

I'm not sure if maybe there could be an additional property configuration to optionally specify the column field property name in the data table configuration (similar to the old p-column) or just having a Column interface that has field/header properties on it then for the columns input field with a note in the docs on the expected properties on the column object. Looks like there are other areas that are expecting column.field and column.header to exist e.g. CSV Export. The docs look like they call out these areas just missed the requirement on the column object properties:

Other alternative is binding the cols array to the columns property and then defining a template variable to access it within your templates. There are 3 cases where this is required which are csv export, reorderable columns and global filtering without the globalFilterFields property.

We worked around this in the meantime by just changing our column object to match the field/header properties.

ryanrupp avatar Jan 26 '18 22:01 ryanrupp

Will be documented.

cagataycivici avatar Jan 31 '18 14:01 cagataycivici

Is it resolved somehow? Can I use any coloumn.anyName now? Or column.field is must?

asheikh-alivia avatar May 14 '20 16:05 asheikh-alivia