In the source code file webpack:///./node_modules/@syncfusion/ej2-grids/src/grid/models/column.js, the Column constructor has the following code:
if (this.sortComparer) {
var a_1 = this.sortComparer;
this.sortComparer = function comparer(x, y, xObj, yObj) {
if (typeof a_1 === 'string') {
a_1 = getObject(a_1, window);
}
if (this.sortDirection === 'Descending') {
var z = x;
x = y;
y = z;
var obj = xObj;
xObj = yObj;
yObj = obj;
}
return a_1(x, y, xObj, yObj);
};
}
Note that inside the comparer function, it references 'this', but there is no scope. Doesn't it need to use the hoisted variable '_this'? With a grid containing a column configuring a custom sortComparer, an error is thrown when printing the grid:
column.js?3b5d:225 Uncaught TypeError: Cannot read properties of undefined (reading 'sortDirection')
at comparer (column.js?3b5d:225:1)
at Column.comparer [as sortComparer] (column.js?3b5d:233:1)
at Function.DataUtil.merge (util.js?d77f:355:1)
at Function.DataUtil.sort (util.js?d77f:339:1)
at Function.DataUtil.sort (util.js?d77f:337:1)
at Function.DataUtil.sort (util.js?d77f:337:1)
at Function.DataUtil.sort (util.js?d77f:337:1)
at JsonAdaptor.onSortBy (adaptors.js?0f5f:305:1)
at JsonAdaptor.processQuery (adaptors.js?0f5f:109:1)
at DataManager.executeLocal (manager.js?4f3e:122:1)
I think what's happening is that when printing this.sortComparer gets double wrapped, i.e., was set once during initial render of html grid, only for columns that have a custom sortComparer. Then during printing it gets wrapped again.
The following patch fixes this bug:
Hi Vignesh, I did a monkey patch for the bug. Are you going to fix it?
Hi Rajapandiyan,
Please see attached zip sample. When clicking Print, the error occurs:
column.js?a57a:225 Uncaught TypeError: Cannot read properties of undefined (reading 'sortDirection')
at comparer (column.js?a57a:225:1)
at Column.comparer [as sortComparer] (column.js?a57a:233:1)
at Function.DataUtil.merge (util.js?7b35:355:1)
at Function.DataUtil.sort (util.js?7b35:339:1)
at Function.DataUtil.sort (util.js?7b35:337:1)
at JsonAdaptor.onSortBy (adaptors.js?aad1:305:24)
at JsonAdaptor.processQuery (adaptors.js?aad1:109:1)
at DataManager.executeLocal (manager.js?2378:122:1)
at eval (manager.js?2378:181:1)
at run (setImmediate.js?9b2a:43:1)
Thank you Joseph.
Hi Joseph,
The error is gone, but it won't print in descending order. See attached project sample. It sorts name in descending order, and displays correct in html. But when printing, it's in ascending order. Thanks.
Correction: Please use the project sample attached to this message, and ignore the one in my last message.
Hi, the video you sent doesn't shown descending order. It shows ascending order. The bug is that when you sort the html in descending order, the print version is not in descending order.
Thanks very much Joseph.
Ok, thanks for the update Joseph.
Hi Bill,
Sorry for the late reply.
We have come up with a custom solution for the issue ‘Grid not printing in descending when sorting is descending and sortComparer property set to a column’ by overwriting the grid print function. In this function, change the column’s 'sortCompare' function by assigning the user defined 'sortComparerFunction' function. So that we can print the grid in descending order. And reassigning the previous 'sortCompare' function so that we can sort the grid.
Please refer the below code example.
|
print() { var grid = this.$refs.grid.ej2Instances; var sortFun = grid.columnModel[0].sortComparer; grid.columnModel[0].sortComparer = this.sortComparerFunction; if (grid.printModule) { grid.printModule.print(); } grid.columnModel[0].sortComparer = sortFun; }, sortComparerFunction: function(reference, comparer, referenceObj, comparerObj) { if (reference < comparer) return -1 else if (reference > comparer) return 1 return 0 },
|
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/SYNCFU~1-778936367
Please get back to us for further details.
Regards,
Joseph I.