Issue with Table.data()

  • 1
  • Question
  • Updated 2 years ago
  • Answered
var params = arguments[0], $ = skuid.$;

var table = $('#MyTable'),
   list,
   items;
   
 alert("Hello World");
 alert(table.length);
 
if (table.length > 0) {
   list = table.data('Scholarship_Award__c').list;
   items = list.getSelectedItems();
}

alert(list.length);

In the code above the first 2 alerts are shown, and the table.length is greater than 0 but the 3 alert is not shown the table is populated with data form the 'Scholarship Award' object.

Am I missing the obvious, do not want to use mass action on the table hence trying to use this snippet.
Photo of Paul Heald

Paul Heald

  • 420 Points 250 badge 2x thumb

Posted 2 years ago

  • 1
Photo of Karen Waldschmitt

Karen Waldschmitt, Official Rep

  • 8,240 Points 5k badge 2x thumb
Paul~

Clarification question regarding your code. Are you trying to get the 3rd alert to be conditionally displayed? If yes, your curly bracket is in the wrong place. If no, you can just move the 3rd alert up with the other 2?

Hope that resolves your issue!
Karen
Photo of Barry Schnell

Barry Schnell, Champion

  • 18,076 Points 10k badge 2x thumb
Hello Paul -

The reason the third alert is not firing is because there is a javascript exception being encountered based on the code.  If you open up the developer console when the page is running and trigger the snippet to get called, you'll find the details of that exception.  It will read something like 

"cannot read property of 'list' of undefined

If I'm understanding your code correctly, you are likely not getting any items and the alerts are simply a means for you to debug what is going wrong.

The reason for the exception is due to this line of code

list = table.data('Scholarship_Award__c').list

In your code, the 'table' variable is an HTML DOM element accessed through jQuery.  jQuery uses selectors to access DOM elements and the 'length' property of the result returned is how many it found.  This is why table.length is one (1) in your case.

If you want to retrieve the list associated to the table, change your code to the following:

table.data('object').list

Skuid stores the "Table" component in a data property called 'object' on the Table DOM element.  The table component has property called list and from the list, you can then get the selected items.

Hope this helps!
(Edited)