Why without the JSON.parse method, I can't sort the data in lightning-datatable?Lightning Web Component -...

How can I get exact maximal value of this expression?

Employer asking for online access to bank account - Is this a scam?

Is there a way to make it so the cursor is included when I prtscr key?

When do characters level up?

Where is the logic in castrating fighters?

Why without the JSON.parse method, I can't sort the data in lightning-datatable?

Crossing US border with music files I'm legally allowed to possess

How did early x86 BIOS programmers manage to program full blown TUIs given very few bytes of ROM/EPROM?

Ticket sales for Queen at the Live Aid

Canon 70D often overexposing or underexposing shots

Can you heal a summoned creature?

Looking for a soft substance that doesn't dissolve underwater

How to prevent bad sectors?

Integrating an absolute function using Mathematica

What is the difference between nullifying your vote and not going to vote at all?

What's the Difference between Two Single-Quotes and One Double-Quote?

How does an ARM MCU run faster than the external crystal?

Riley Rebuses that Share a Common Theme

Why does the 6502 have the BIT instruction?

Approximate solution: factorial and exponentials

Is this resistor leaking? If so, is it a concern?

Infinite Sequence based on Simple Rule

Does this degree 12 genus 1 curve have only one point over infinitely many finite fields?

How do I align equations in three columns, justified right, center and left?



Why without the JSON.parse method, I can't sort the data in lightning-datatable?


Lightning Web Component - Sort table columnsJavaScript executing before controller method finishesWhy Lightning Web Componentlightning-tree-grid link to js function?Fetching data from Apex controller for lightning web componentIs it not possible to get a parent field in a datatable column specification, for a Lightning Web Component?Lightning Web Components inline editing Datatable - data update issueLightning Web Components lightning-datatable detect “select all” checkboxIssue while iterating lightning-tab, as expressions can't be usedLWC datatable column with a lightning-card insideHow to display lable to a url cell in LWC datatable?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















I'd like to understand the usefulness of the method JSON.parse in the JS file of a lwc when we want to perform certain actions on a list of objects retrieved from our apex controller. I give you an example.
I have implemented the sorting functionality on a lightning datatable in a lwc. but my problem is when I don't use the JSON.parse method when I get all the data, I get an empty proxy. Here is the the output when I'm debugging:



enter image description here



But when I use the JSON.parse method when I get the data I want to sort, I get and I see all the data when I'm debugging. Here is the output for this case:



enter image description here



Can someone help me please to understand why we can't see the data without the JSON.pase method and why without the JSON.parse method it doesn't work?



here is the html template:



      <lightning-datatable key-field="Id" 
data={opportunities}
columns={columns}
onsort={updateColumnSorting}
sorted-by={sortedBy}
sorted-direction={sortedDirection}>
</lightning-datatable>


the js file:



import { LightningElement ,wire,track} from 'lwc';
import getAllOpps from '@salesforce/apex/GetAllOpportunities.getAllOpps';

export default class OpportunityList extends LightningElement {
@track columns = [
{
label: 'Opportunity name',
fieldName: 'Name',
type: 'tex',
sortable: true
},
{
label: 'Stage Name',
fieldName: 'StageName',
type: 'text',
sortable: true
},
{
label: 'Close date',
fieldName: 'CloseDate',
type: 'date',
sortable: true
}

];

@track error;
@track opportunities = [];
@track sortedBy;
@track sortedDirection = 'asc';


@wire(getAllOpps)
wiredOpps({error,data}) {
if (data) {
this.opportunities = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.opportunities = undefined;
}
}

sortData(fieldName, sortDirection){

//let data = JSON.parse(JSON.stringify(this.opportunities));
let data = this.opportunities;
//function to return the value stored in the field
const key = (a) => {
let fieldValue = a[fieldName] ? (typeof a[fieldName] === 'string' ? a[fieldName].toLowerCase() : a[fieldName]) : '';
return fieldValue;
}
let reverse = sortDirection === 'asc' ? 1: -1;

//set sorted data to opportunities attribute
this.opportunities = data.sort((a,b) => {
return reverse * ((key(a) > key(b)) - (key(b) > key(a)));
});

}

updateColumnSorting(event){
this.sortedBy = event.detail.fieldName;
this.sortedDirection = event.detail.sortDirection;
this.sortData(this.sortedBy,this.sortedDirection);
}

}


And the apex controller:



public with sharing class GetAllOpportunities {
@AuraEnabled(cacheable=true)
public static List<Opportunity> getAllOpps() {

return [SELECT Id, Name ,StageName, CloseDate FROM Opportunity Limit 10];
}

}




Thanks in advance!










share|improve this question









New contributor



dibocor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


























    1















    I'd like to understand the usefulness of the method JSON.parse in the JS file of a lwc when we want to perform certain actions on a list of objects retrieved from our apex controller. I give you an example.
    I have implemented the sorting functionality on a lightning datatable in a lwc. but my problem is when I don't use the JSON.parse method when I get all the data, I get an empty proxy. Here is the the output when I'm debugging:



    enter image description here



    But when I use the JSON.parse method when I get the data I want to sort, I get and I see all the data when I'm debugging. Here is the output for this case:



    enter image description here



    Can someone help me please to understand why we can't see the data without the JSON.pase method and why without the JSON.parse method it doesn't work?



    here is the html template:



          <lightning-datatable key-field="Id" 
    data={opportunities}
    columns={columns}
    onsort={updateColumnSorting}
    sorted-by={sortedBy}
    sorted-direction={sortedDirection}>
    </lightning-datatable>


    the js file:



    import { LightningElement ,wire,track} from 'lwc';
    import getAllOpps from '@salesforce/apex/GetAllOpportunities.getAllOpps';

    export default class OpportunityList extends LightningElement {
    @track columns = [
    {
    label: 'Opportunity name',
    fieldName: 'Name',
    type: 'tex',
    sortable: true
    },
    {
    label: 'Stage Name',
    fieldName: 'StageName',
    type: 'text',
    sortable: true
    },
    {
    label: 'Close date',
    fieldName: 'CloseDate',
    type: 'date',
    sortable: true
    }

    ];

    @track error;
    @track opportunities = [];
    @track sortedBy;
    @track sortedDirection = 'asc';


    @wire(getAllOpps)
    wiredOpps({error,data}) {
    if (data) {
    this.opportunities = data;
    this.error = undefined;
    } else if (error) {
    this.error = error;
    this.opportunities = undefined;
    }
    }

    sortData(fieldName, sortDirection){

    //let data = JSON.parse(JSON.stringify(this.opportunities));
    let data = this.opportunities;
    //function to return the value stored in the field
    const key = (a) => {
    let fieldValue = a[fieldName] ? (typeof a[fieldName] === 'string' ? a[fieldName].toLowerCase() : a[fieldName]) : '';
    return fieldValue;
    }
    let reverse = sortDirection === 'asc' ? 1: -1;

    //set sorted data to opportunities attribute
    this.opportunities = data.sort((a,b) => {
    return reverse * ((key(a) > key(b)) - (key(b) > key(a)));
    });

    }

    updateColumnSorting(event){
    this.sortedBy = event.detail.fieldName;
    this.sortedDirection = event.detail.sortDirection;
    this.sortData(this.sortedBy,this.sortedDirection);
    }

    }


    And the apex controller:



    public with sharing class GetAllOpportunities {
    @AuraEnabled(cacheable=true)
    public static List<Opportunity> getAllOpps() {

    return [SELECT Id, Name ,StageName, CloseDate FROM Opportunity Limit 10];
    }

    }




    Thanks in advance!










    share|improve this question









    New contributor



    dibocor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      1












      1








      1








      I'd like to understand the usefulness of the method JSON.parse in the JS file of a lwc when we want to perform certain actions on a list of objects retrieved from our apex controller. I give you an example.
      I have implemented the sorting functionality on a lightning datatable in a lwc. but my problem is when I don't use the JSON.parse method when I get all the data, I get an empty proxy. Here is the the output when I'm debugging:



      enter image description here



      But when I use the JSON.parse method when I get the data I want to sort, I get and I see all the data when I'm debugging. Here is the output for this case:



      enter image description here



      Can someone help me please to understand why we can't see the data without the JSON.pase method and why without the JSON.parse method it doesn't work?



      here is the html template:



            <lightning-datatable key-field="Id" 
      data={opportunities}
      columns={columns}
      onsort={updateColumnSorting}
      sorted-by={sortedBy}
      sorted-direction={sortedDirection}>
      </lightning-datatable>


      the js file:



      import { LightningElement ,wire,track} from 'lwc';
      import getAllOpps from '@salesforce/apex/GetAllOpportunities.getAllOpps';

      export default class OpportunityList extends LightningElement {
      @track columns = [
      {
      label: 'Opportunity name',
      fieldName: 'Name',
      type: 'tex',
      sortable: true
      },
      {
      label: 'Stage Name',
      fieldName: 'StageName',
      type: 'text',
      sortable: true
      },
      {
      label: 'Close date',
      fieldName: 'CloseDate',
      type: 'date',
      sortable: true
      }

      ];

      @track error;
      @track opportunities = [];
      @track sortedBy;
      @track sortedDirection = 'asc';


      @wire(getAllOpps)
      wiredOpps({error,data}) {
      if (data) {
      this.opportunities = data;
      this.error = undefined;
      } else if (error) {
      this.error = error;
      this.opportunities = undefined;
      }
      }

      sortData(fieldName, sortDirection){

      //let data = JSON.parse(JSON.stringify(this.opportunities));
      let data = this.opportunities;
      //function to return the value stored in the field
      const key = (a) => {
      let fieldValue = a[fieldName] ? (typeof a[fieldName] === 'string' ? a[fieldName].toLowerCase() : a[fieldName]) : '';
      return fieldValue;
      }
      let reverse = sortDirection === 'asc' ? 1: -1;

      //set sorted data to opportunities attribute
      this.opportunities = data.sort((a,b) => {
      return reverse * ((key(a) > key(b)) - (key(b) > key(a)));
      });

      }

      updateColumnSorting(event){
      this.sortedBy = event.detail.fieldName;
      this.sortedDirection = event.detail.sortDirection;
      this.sortData(this.sortedBy,this.sortedDirection);
      }

      }


      And the apex controller:



      public with sharing class GetAllOpportunities {
      @AuraEnabled(cacheable=true)
      public static List<Opportunity> getAllOpps() {

      return [SELECT Id, Name ,StageName, CloseDate FROM Opportunity Limit 10];
      }

      }




      Thanks in advance!










      share|improve this question









      New contributor



      dibocor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I'd like to understand the usefulness of the method JSON.parse in the JS file of a lwc when we want to perform certain actions on a list of objects retrieved from our apex controller. I give you an example.
      I have implemented the sorting functionality on a lightning datatable in a lwc. but my problem is when I don't use the JSON.parse method when I get all the data, I get an empty proxy. Here is the the output when I'm debugging:



      enter image description here



      But when I use the JSON.parse method when I get the data I want to sort, I get and I see all the data when I'm debugging. Here is the output for this case:



      enter image description here



      Can someone help me please to understand why we can't see the data without the JSON.pase method and why without the JSON.parse method it doesn't work?



      here is the html template:



            <lightning-datatable key-field="Id" 
      data={opportunities}
      columns={columns}
      onsort={updateColumnSorting}
      sorted-by={sortedBy}
      sorted-direction={sortedDirection}>
      </lightning-datatable>


      the js file:



      import { LightningElement ,wire,track} from 'lwc';
      import getAllOpps from '@salesforce/apex/GetAllOpportunities.getAllOpps';

      export default class OpportunityList extends LightningElement {
      @track columns = [
      {
      label: 'Opportunity name',
      fieldName: 'Name',
      type: 'tex',
      sortable: true
      },
      {
      label: 'Stage Name',
      fieldName: 'StageName',
      type: 'text',
      sortable: true
      },
      {
      label: 'Close date',
      fieldName: 'CloseDate',
      type: 'date',
      sortable: true
      }

      ];

      @track error;
      @track opportunities = [];
      @track sortedBy;
      @track sortedDirection = 'asc';


      @wire(getAllOpps)
      wiredOpps({error,data}) {
      if (data) {
      this.opportunities = data;
      this.error = undefined;
      } else if (error) {
      this.error = error;
      this.opportunities = undefined;
      }
      }

      sortData(fieldName, sortDirection){

      //let data = JSON.parse(JSON.stringify(this.opportunities));
      let data = this.opportunities;
      //function to return the value stored in the field
      const key = (a) => {
      let fieldValue = a[fieldName] ? (typeof a[fieldName] === 'string' ? a[fieldName].toLowerCase() : a[fieldName]) : '';
      return fieldValue;
      }
      let reverse = sortDirection === 'asc' ? 1: -1;

      //set sorted data to opportunities attribute
      this.opportunities = data.sort((a,b) => {
      return reverse * ((key(a) > key(b)) - (key(b) > key(a)));
      });

      }

      updateColumnSorting(event){
      this.sortedBy = event.detail.fieldName;
      this.sortedDirection = event.detail.sortDirection;
      this.sortData(this.sortedBy,this.sortedDirection);
      }

      }


      And the apex controller:



      public with sharing class GetAllOpportunities {
      @AuraEnabled(cacheable=true)
      public static List<Opportunity> getAllOpps() {

      return [SELECT Id, Name ,StageName, CloseDate FROM Opportunity Limit 10];
      }

      }




      Thanks in advance!







      javascript lightning-web-components






      share|improve this question









      New contributor



      dibocor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share|improve this question









      New contributor



      dibocor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share|improve this question




      share|improve this question








      edited 5 hours ago









      Mohith Shrivastava

      62.3k7106150




      62.3k7106150






      New contributor



      dibocor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      asked 8 hours ago









      dibocordibocor

      237




      237




      New contributor



      dibocor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      New contributor




      dibocor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes


















          3














          The behavior you are seeing is because of Salesforce feature called lightning locker.



          Any object in lwc JavaScript will use the proxy object.



          https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/security_proxy.htm



          It's a security feature that makes debugging difficult but adds additional security.



          You don't need to use JSON.stringify and JSON.parse in prod .You can use it for debugging purposes.



          Update



          Looking into the code , it looks like lockerservice proxy is forcing the object to readonly on datatable . You should report this as a bug to salesforce .






          share|improve this answer


























          • Thank you @Mohith Shrivastava for your reply. But is it normal to get a proxy with an empty array?

            – dibocor
            8 hours ago











          • No that's not normal.Do you see the data on the UI ?

            – Mohith Shrivastava
            8 hours ago











          • Can you share the code snippet so we can see what's going on ?

            – Mohith Shrivastava
            8 hours ago






          • 1





            I edited the question and added the code

            – dibocor
            7 hours ago






          • 1





            ok I added the apex class

            – dibocor
            6 hours ago












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "459"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          dibocor is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f263740%2fwhy-without-the-json-parse-method-i-cant-sort-the-data-in-lightning-datatable%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          The behavior you are seeing is because of Salesforce feature called lightning locker.



          Any object in lwc JavaScript will use the proxy object.



          https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/security_proxy.htm



          It's a security feature that makes debugging difficult but adds additional security.



          You don't need to use JSON.stringify and JSON.parse in prod .You can use it for debugging purposes.



          Update



          Looking into the code , it looks like lockerservice proxy is forcing the object to readonly on datatable . You should report this as a bug to salesforce .






          share|improve this answer


























          • Thank you @Mohith Shrivastava for your reply. But is it normal to get a proxy with an empty array?

            – dibocor
            8 hours ago











          • No that's not normal.Do you see the data on the UI ?

            – Mohith Shrivastava
            8 hours ago











          • Can you share the code snippet so we can see what's going on ?

            – Mohith Shrivastava
            8 hours ago






          • 1





            I edited the question and added the code

            – dibocor
            7 hours ago






          • 1





            ok I added the apex class

            – dibocor
            6 hours ago
















          3














          The behavior you are seeing is because of Salesforce feature called lightning locker.



          Any object in lwc JavaScript will use the proxy object.



          https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/security_proxy.htm



          It's a security feature that makes debugging difficult but adds additional security.



          You don't need to use JSON.stringify and JSON.parse in prod .You can use it for debugging purposes.



          Update



          Looking into the code , it looks like lockerservice proxy is forcing the object to readonly on datatable . You should report this as a bug to salesforce .






          share|improve this answer


























          • Thank you @Mohith Shrivastava for your reply. But is it normal to get a proxy with an empty array?

            – dibocor
            8 hours ago











          • No that's not normal.Do you see the data on the UI ?

            – Mohith Shrivastava
            8 hours ago











          • Can you share the code snippet so we can see what's going on ?

            – Mohith Shrivastava
            8 hours ago






          • 1





            I edited the question and added the code

            – dibocor
            7 hours ago






          • 1





            ok I added the apex class

            – dibocor
            6 hours ago














          3












          3








          3







          The behavior you are seeing is because of Salesforce feature called lightning locker.



          Any object in lwc JavaScript will use the proxy object.



          https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/security_proxy.htm



          It's a security feature that makes debugging difficult but adds additional security.



          You don't need to use JSON.stringify and JSON.parse in prod .You can use it for debugging purposes.



          Update



          Looking into the code , it looks like lockerservice proxy is forcing the object to readonly on datatable . You should report this as a bug to salesforce .






          share|improve this answer















          The behavior you are seeing is because of Salesforce feature called lightning locker.



          Any object in lwc JavaScript will use the proxy object.



          https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/security_proxy.htm



          It's a security feature that makes debugging difficult but adds additional security.



          You don't need to use JSON.stringify and JSON.parse in prod .You can use it for debugging purposes.



          Update



          Looking into the code , it looks like lockerservice proxy is forcing the object to readonly on datatable . You should report this as a bug to salesforce .







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 5 hours ago

























          answered 8 hours ago









          Mohith ShrivastavaMohith Shrivastava

          62.3k7106150




          62.3k7106150













          • Thank you @Mohith Shrivastava for your reply. But is it normal to get a proxy with an empty array?

            – dibocor
            8 hours ago











          • No that's not normal.Do you see the data on the UI ?

            – Mohith Shrivastava
            8 hours ago











          • Can you share the code snippet so we can see what's going on ?

            – Mohith Shrivastava
            8 hours ago






          • 1





            I edited the question and added the code

            – dibocor
            7 hours ago






          • 1





            ok I added the apex class

            – dibocor
            6 hours ago



















          • Thank you @Mohith Shrivastava for your reply. But is it normal to get a proxy with an empty array?

            – dibocor
            8 hours ago











          • No that's not normal.Do you see the data on the UI ?

            – Mohith Shrivastava
            8 hours ago











          • Can you share the code snippet so we can see what's going on ?

            – Mohith Shrivastava
            8 hours ago






          • 1





            I edited the question and added the code

            – dibocor
            7 hours ago






          • 1





            ok I added the apex class

            – dibocor
            6 hours ago

















          Thank you @Mohith Shrivastava for your reply. But is it normal to get a proxy with an empty array?

          – dibocor
          8 hours ago





          Thank you @Mohith Shrivastava for your reply. But is it normal to get a proxy with an empty array?

          – dibocor
          8 hours ago













          No that's not normal.Do you see the data on the UI ?

          – Mohith Shrivastava
          8 hours ago





          No that's not normal.Do you see the data on the UI ?

          – Mohith Shrivastava
          8 hours ago













          Can you share the code snippet so we can see what's going on ?

          – Mohith Shrivastava
          8 hours ago





          Can you share the code snippet so we can see what's going on ?

          – Mohith Shrivastava
          8 hours ago




          1




          1





          I edited the question and added the code

          – dibocor
          7 hours ago





          I edited the question and added the code

          – dibocor
          7 hours ago




          1




          1





          ok I added the apex class

          – dibocor
          6 hours ago





          ok I added the apex class

          – dibocor
          6 hours ago










          dibocor is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          dibocor is a new contributor. Be nice, and check out our Code of Conduct.













          dibocor is a new contributor. Be nice, and check out our Code of Conduct.












          dibocor is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Salesforce Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f263740%2fwhy-without-the-json-parse-method-i-cant-sort-the-data-in-lightning-datatable%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

          The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

          Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...