Netsuite Sorting: How to show sorted result in HTML Table using Suitelet?

Hi All,

Subject: How to show sorted data in HTML Table using SUITELET .

There was a requirement where data should be sort by 2 fields on record.
There are 2 ways to do this,
1: Using Saved Search Sorting:
    If inside script, data can come from saved search in a way it is needed, then Saved search sorting is a best way. Create a saved search and use sorting functionality to sort , result will come in a sorted way. Then display on suitelet
2. Use column sorting inside script:
  This is actually same as saved search only difference is that script is not using saved search , script create search inside script and use sort in column fields.Display on suitelet

3. If data is not coming from Saved search or search:
   This is complex scenario where data is not coming from record using saved search/simple search.
 Usually developer search for a function which can sort it in a simple way but 2 way sorting is very complex and need more attention.
  Lets understand in detail:
   Suppose data is coming from CSV file and have 2 columns a.Item b.location.
   Now requirement is to sort it ascending first by item and then by name.
   It should look like
   ITEM A: Australia
   ITEM A: Japan
   ITEM B: Austria
   ITEM C: New Zealand
   and so on.

 If we make a string using item and location and add it in table's column(hidden) and if we pass that string into function below it will automatically sort it in ascending order. We can use same logic for 3 fields or n number of field.
Only limitation is that it should be ascending. If we want one in ascending another in descending then we have to reverse and concat .

function sortByText() {
          var table, rows, switching, i, x, y, shouldSwitch;
          table = document.getElementById("mytable");
          switching = true;
          /*Make a loop that will continue until
          no switching has been done:*/
          while (switching) {
            //start by saying: no switching is done:
            switching = false;
            rows = table.rows;
            /*Loop through all table rows (except the
            first, which contains table headers):*/
            for (i = 0; i < (rows.length - 1); i++) {
        //    for (i = (rows.length - 2); i >= 0; i--) {
              //start by saying there should be no switching:
              shouldSwitch = false;
              /*Get the two elements you want to compare,
              one from current row and one from the next:*/
              x = rows[i].getElementsByTagName("td")[4];
              y = rows[i + 1].getElementsByTagName("td")[4];
              //check if the two rows should switch place:
             if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
                //if so, mark as a switch and break the loop:
                shouldSwitch = true;
                break;
              }
            }
            if (shouldSwitch) {
              /*If a switch has been marked, make the switch
              and mark that a switch has been done:*/
              rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
              switching = true;
            }
          }
        }

Please comment below if you have any question.



Download Netsuite Guru Android App
Follow Me on Linkedin
Our FB Page

Comments