3

HTML:

<section class="cd-gallery">
    <ul id="courses">
    </ul>
    <div class="cd-fail-message">No results found</div>
</section>

<ul>
    <li><input id="buttonaz" type="button" value="Course name(a-z)"/></li>
    <li><input id="buttonza" type="button" value="Course name(z-a)"/></li>
    <li><input id="buttonlu" type="button" value="Last updated"></li>
<ul>

JavaScript:

var public_spreadsheet_url = 'https://docs.google.com/spreadsheets/..."

function init() {
    Tabletop.init( { key: public_spreadsheet_url,
                   callback: showInfo,
                   simpleSheet: true } );
}
window.addEventListener('DOMContentLoaded', init);

function sortAZ(a, b) {
    var x = a.Course.toLowerCase();
    var y = b.Course.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function sortZA(a, b) {
    var x = a.Course.toLowerCase();
    var y = b.Course.toLowerCase();
    return ((x > y) ? -1 : ((x < y) ? 1 : 0));
}

function showInfo(data) {
    var bodyContent = '';
    var sheetUrlRoot = 'https://docs.google.com/spreadsheets/d/';
    var buttonaz = document.getElementById("buttonaz");
    var buttonza = document.getElementById("buttonza");
    console.log(data)

    for (var i = 0; i < data.length; i++) {
        var sheetUrl = sheetUrlRoot + data[i].ActionId;
        var c = data[i].Course;
        var courseName = '<div class=\"courseName\">' + c + '</div>';
        var designer = data[i]['Designer'].toLowerCase();
        var numHolds = data[i]['On Hold']

        if (numHolds > 0) {
            bodyContent += '<li class="color-2 course mix ' + designer + ' "style="background-color: #E89696";>' + courseName + statusList+ sheetLink+ '</li>';
        } else if (numHolds <= 0){
            bodyContent += '<li class="color-1 course mix ' + designer + ' "style="background-color: #C2D5BE";>' + courseName + statusList+ sheetLink+'</li>';
        }
    }
    document.getElementById('courses').innerHTML = bodyContent;
    document.getElementById('buttonaz').onclick = data.sort(sortAZ);
    document.getElementById('buttonaz').onclick = data.sort(sortZA);
}

Hi Stack Overflow users, I have imported data using tabletop.js to display a set of courses that my university has in hand. However, I cannot have it to display the courses sorting alphabetically from a-z, as well as from z-a when the buttons "Course name (a-z)" and "Course name (z-a)" are clicked. The data are displayed when the page is first loaded, but will not do anything when I click the sorting buttons.

Please help and any input will be appreciated!

P.S. I'm also filtering the courses by the name of designer using mixitup jQuery plugin.

Geshode
  • 3,600
  • 6
  • 18
  • 32
Amy Yang
  • 31
  • 1
  • 2

2 Answers2

2

Refer the code which have two button , one is for sort aZ and one is for sort Za . Click on Expand snippet , you will see two button , click on them and enjoy sorting

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array Sort</h2>
<p>Click the buttons to sort the array alphabetically or numerically.</p>
<button onclick="myFunction1()">Sort Az</button>
<button onclick="myFunction2()">Sort zA</button>
<p id="demo"></p>
<script>
var points = ["z", "b", "d", "a"];
var data1=Array.prototype.slice.call(points);
console.log('Za Sort ',data1.sort().reverse());
document.getElementById("demo").innerHTML = points;    

function myFunction1() {
    points.sort();
    document.getElementById("demo").innerHTML = points;
}
function myFunction2() {
    
    document.getElementById("demo").innerHTML = data1.sort().reverse();
}
</script>
</body>
</html>

If incoming data is array use javascript built in sort() function to sort data

var data = ["z", "b", "d", "a"];
data.sort();
console.log('Ascending order aZ ',data)
data.reverse();
console.log('Descending order zA',data);

output
Ascending order ["a", "b", "d", "z"]
Descending order["z", "d", "b", "a"]
  • Hi! Thanks for your answer. I tried that and it doesn't work. How am I able to have the page sort when I click the sorting (a-z, z-a) buttons? Thanks again in advance. – Amy Yang Sep 25 '18 at 22:09
  • Refer the just added code .run it and click on full page , click on button , it will sort data – Sandip Bailkar Sep 26 '18 at 17:17
0

If you want to use library https://underscorejs.org/#

var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
_.sortBy(stooges, 'name');
Ferry Sanjaya
  • 224
  • 2
  • 17