Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions m1.lesson8.flights-server-handlebars-lodash/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ <h1>handlebars and lodash</h1>

<section>
<button class="sort-dep">Sort By Departure</button>
<button class="sort-arr">Sort By Arrival</button>
<select id="actualSelect">
<option>nothing to show yet</option>
</select>
<script id='Airline-dropdown-template' type='text/x-handlebars-template'>
{{#each this}}
<option>{{by}}</option>
{{/each}}
</script>
<button class="sort-arg">filter by airline</button>
<table id="flight-table">
<caption>flights</caption>
<thead>
Expand Down Expand Up @@ -54,28 +64,50 @@ <h1>handlebars and lodash</h1>

<script>
const tBodyTmpl = Handlebars.compile(document.getElementById('table-row-template').innerHTML);
const selectOptionTmpl = Handlebars.compile(document.getElementById('Airline-dropdown-template').innerHTML);

let flightsData = [];
let selectOptions = [];


document.querySelector('button.load').onclick = function() {
fetch('flights.json')
.then(res => res.json())
.then(data => {
flightsData = data;
renderTable(flightsData);
selectOptions = _.uniqBy(flightsData, 'by');
addOption(selectOptions);
console.log(selectOptions);
});
};


// render tbody with handlebars
function renderTable(data) {
document.querySelector('#flight-table tbody').innerHTML = tBodyTmpl(data);
}

// sorting
// add option to dropdown with handlebars
function addOption(data) {
document.querySelector('#actualSelect').innerHTML = selectOptionTmpl(data);
}

// sorting by departure
document.querySelector('button.sort-dep').onclick = function() {
flightsData = _.sortBy(flightsData, flight => new Date(flight.departure));
renderTable(flightsData);
};

//sorting by arrival
document.querySelector('button.sort-arr').onclick = function() {
flightsData = _.orderBy(flightsData,['arrival'],['desc']);
renderTable(flightsData);
};

//filter according to selected option in dropdown
document.querySelector('button.sort-arg').onclick = function() {
let selectedAirline = document.getElementById("actualSelect").value;
flightsData = _.filter(flightsData,['by',selectedAirline]);
renderTable(flightsData);
};
</script>
Expand Down