0

Using the below JS method for the custom sorting to work on a lightning data table. The same works perfectly in aura frame work. I suspect key() doesn't work in lwc. Documentation didn't help much. Can someone suggest what's going wrong with below piece.

updateColumnSorting(event) {
    var fieldName = event.detail.fieldName;
    var sortDirection = event.detail.sortDirection;
    this.sortedBy = fieldName;
    this.sortedDirection = sortDirection;
    var reverse = sortDirection !== 'asc';
    var dataToSort = this.data;
    dataToSort.sort(this.sortBy(fieldName, reverse));
    this.data = dataToSort;
}

sortBy(field, reverse, primer) {
    var key = primer ?
            function(x) {return primer(x.hasOwnProperty(field) ? (typeof x[field] === 'text' ? x[field].toLowerCase() : x[field]) : 'aaa')} :
            function(x) {return x.hasOwnProperty(field) ? (typeof x[field] === 'text' ? x[field].toLowerCase() : x[field]) : 'aaa'};
        reverse = !reverse ? 1 : -1;
        return function (a, b) {            
            return a = key(a), b = key(b), reverse * ((a > b) - (b > a));
        }

}

Error LOG

[NoErrorObjectAvailable] Script error. newHandler()@https://static.lightning.force.com/cs94/auraFW/javascript/pxtF0ZdGYF-z00XsmRUcGQ/aura_proddebug.js:46041:14 {anonymous}()@https://static.lightning.force.com/cs94/auraFW/javascript/pxtF0ZdGYF-z00XsmRUcGQ/aura_proddebug.js:46057:25 vo.dispatchEvent()@https://static.lightning.force.com/cs94/auraFW/javascript/pxtF0ZdGYF-z00XsmRUcGQ/aura_proddebug.js:6810:34 vo.fireSortedColumnChange()@https://efficiency-computing-5859-dev-ed.lightning.force.com/components/lightning/datatable.js:11:19414 vo.handleUpdateColumnSort()@https://efficiency-computing-5859-dev-ed.lightning.force.com/components/lightning/datatable.js:11:19042

4
  • if you debug this part, dataToSort.sort(this.sortBy(fieldName, reverse));, what do you notice? can you update your post with additional troubleshooting? thanks – glls May 13 '19 at 3:15
  • Error: ----- TypeError: 'set' on proxy: trap returned falsish for property '0' LINE: ------ d.sort(this.sortBy(b,"asc"!==c) – Karthik May 13 '19 at 4:48
  • @sfdcfox I see your comments and new to lightning development link can you please advise what should be the fix – Karthik May 13 '19 at 5:30
  • Thanks everyone for looking. Couple of actions i took which helped resolve the issue a. Removed the cacheable flag from controller method b. de-serialized the data and pass it back at the end of event – Karthik May 13 '19 at 11:58