3

I have a datatable in my LWC with sorting that all works well, except when I want to sort on a link it is obviously using the fieldName "nameUrl" to sort with, rather than the label. My column is defined like this

{
  label: "Name",
  fieldName: "nameUrl",
  type: "url",
  typeAttributes: { label: { fieldName: "Name" }, target: "_blank" },
  sortable: true
},

This is the loading of the grid:

@wire(getAccountsToVerify, { inputdate: "$inputDate" })
  accountResult(result) {
  this.wiredAccounts = result;
  if (result.data) {
    this.accounts = result.data.map((record) =>
      Object.assign({ nameUrl: "/" + record.Id }, record)
    );
    this.error = undefined;
  } else if (result.error) {
    this.error = error;
    this.accounts = undefined;
  }
}

And my sort code:

  updateSort(event) {
    let fieldName = event.detail.fieldName;
    let sortDirection = event.detail.sortDirection;
    this.sortBy = fieldName;
    this.sortDir = sortDirection;
    let reverse = sortDirection !== "asc";
    let data_clone = JSON.parse(JSON.stringify(this.accounts));
    this.accounts = data_clone.sort(this.sortData(fieldName, reverse));
  }

  sortData(field, reverse, primer) {
    var key = function (x) {
      return primer ? primer(x[field]) : x[field];
    };
    return function (a, b) {
      var A = key(a),
        B = key(b);
      if (A === undefined) A = "";
      if (B === undefined) B = "";
      return (A < B ? -1 : A > B ? 1 : 0) * [1, -1][+!!reverse];
    };
  }

The field is the Account Name, which is a PersonAccount so is made up of a person's first, middle and lastname. How can I get it to sort on the Account Name instead of the url?

enter image description here

1 Answer 1

5

In the sort method just set the sortBy conditionally.

updateSort(event) {
    let fieldName = event.detail.fieldName;
    fieldName = fieldName === 'nameUrl' ? 'Name' : fieldName;
    let sortDirection = event.detail.sortDirection;
    this.sortBy = fieldName;
    this.sortDir = sortDirection;
    let reverse = sortDirection !== "asc";
    let data_clone = JSON.parse(JSON.stringify(this.accounts));
    this.accounts = data_clone.sort(this.sortData(fieldName, reverse));
    // keep this line at the end to reset the name field back to url so we get the sort diections.
    this.sortBy = event.detail.fieldName;;
}
5
  • Rahul this works nicely, thanks, but strangely enough it doesn't do the reverse sort? On the other columns it changes asc to desc and vice versa, but on the url column it only sorts asc. Do you know why that is?
    – Irene
    Nov 8, 2020 at 19:43
  • No idea, it should sort in both the ways. @Irene Nov 9, 2020 at 4:07
  • The strangest thing - I've added console.log on the sortDirection. It never changes from 'asc' if I sort the 'nameUrl/Name' column, yet on all the other columns it does. If I comment out the line that sets the fieldName (fieldName = fieldName === 'nameUrl' ? 'Name' : fieldName;) it DOES change the sortDirection to 'asc/desc'! How can that be? I've tried moving the code lines around but it makes no difference.
    – Irene
    Nov 13, 2020 at 1:38
  • 2
    I found the answer! salesforce.stackexchange.com/questions/308534/…. I added a line at the end to set the sortBy back to the urlName: this.sortBy = event.detail.fieldName; and this fixed it to sort asc/desc
    – Irene
    Nov 13, 2020 at 2:25
  • @Irene Cool Thanks, I didn't know that. Let me add that in my answer.. Nov 13, 2020 at 6:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .