Methods
addEventsListeners()
Assigning event listeners to static elements of the HTML
- Source:
changeSortBtn(clickedSortBtn) → {boolean}
Gives button from header class "active" and remove from previous clicked button Also it toggle class "ascending", if add this class return true, else false Returned value is used as parameter in sortResults
Parameters:
| Name | Type | Description |
|---|---|---|
clickedSortBtn |
object |
- Source:
Returns:
isAscending
- Type
- boolean
connectToOrigin(url) → {Promise}
Returns data from API as JSON
Parameters:
| Name | Type | Description |
|---|---|---|
url |
string |
Returns:
- Type
- Promise
countIncomes(companyIncomes) → {Object}
Return object with calculated: sum of all company incomes, average income from sum, and sum of incomes from last month
Parameters:
| Name | Type | Description |
|---|---|---|
companyIncomes |
array | Array of objects with income and date |
- Source:
Returns:
- Type
- Object
dataRowTemplate(result)
Template of row in table
Parameters:
| Name | Type | Description |
|---|---|---|
result |
array |
- Source:
Returns:
fetchData() → {object}
Using fetch(url) and get companies data, then convert response to json. Next, Promise.all make sure that another data table is loaded and converted to json before next step which is modifying companies array. Now the algorithm for every company object add some extra properties, which are calculated by function countIncomes. Function fetchData is used in init() with then(), where class variable wholeData and filteredData is filled with complete data about companies and their incomes.
- Source:
Returns:
- Type
- object
filterByKeyword(searchKeyword, data) → {array}
Filtering start when input text have some content and Search button is clickedSortBtn, if text is empty table will be filled with defaults start rows. Function is using filter with callback on global variable companiesData. If one of the properties is similar to keyWords, object from array is returning by filter to newArray. After complete filtering results are sorted by id, in ascending order.
Parameters:
| Name | Type | Description |
|---|---|---|
searchKeyword |
string | String from search input |
data |
array | All data from API |
- Source:
Returns:
- Type
- array
getTotalIncome(incomesObjects) → {string}
Calculate sum of properties of array
Parameters:
| Name | Type | Description |
|---|---|---|
incomesObjects |
array | Array of objects each one contains income and date |
Returns:
Sum
- Type
- string
init()
When application is loaded, it assigning static elements to variables fetch and then sort data ascending, slice from it 10 first items and render them. It also render pagination and add event listeners to static elements
- Source:
pageOptionTemplate(j)
Template of option in pagination
Parameters:
| Name | Type | Description |
|---|---|---|
j |
number | Page number |
- Source:
renderPagination(array, container)
Render option elements in select markup.
Parameters:
| Name | Type | Description |
|---|---|---|
array |
array | Filtered data which length is used to calculate number of pages |
container |
HTMLElement | Place for rendering pagination |
- Source:
renderResults(results, container)
Render given data as rows in container
Parameters:
| Name | Type | Description |
|---|---|---|
results |
array | Part of data to render, default: 10 items |
container |
HTMLElement | Place for rendering one |
- Source:
setupSelectors()
Assigning to variable static elements of the HTML
- Source:
slicedData(filteredData, pageNumber, pageSize) → {array}
Function serves for pagination, it needs filtered, or whole data, page which user requested and number of rows that will be rendered.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
filteredData |
array | ||
pageNumber |
number | 1 | Requested page by user |
pageSize |
number | 10 | Default 10 rows on page |
- Source:
Returns:
Part of data array which will fit in one page by, default 10 items
- Type
- array
sortResults(sortKey, isAscending, array) → {array}
Function is using custom method to compare two items Parameter from isAscending come from changeSortBtn If true sorting ascending else descending
Parameters:
| Name | Type | Description |
|---|---|---|
sortKey |
string | Column symbol by which sorting is made |
isAscending |
boolean | If true sorting ascending, else descending |
array |
array | Filtered or not data from API |
- Source:
Returns:
- Type
- array