-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Задача 5.2 #5
Open
Veronika143
wants to merge
2
commits into
js-tasks-ru:master
Choose a base branch
from
Veronika143:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Задача 5.2 #5
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,172 @@ | ||
export default class SortableTable { | ||
subElements = {}; | ||
orderValue; | ||
fieldValue; | ||
|
||
constructor(headerConfig = [], data = []) { | ||
this.headerConfig = headerConfig; | ||
this.data = data; | ||
this.element = this.createElement(this.createTemplate()); | ||
this.selectSubElements(); | ||
} | ||
|
||
createElement(template) { | ||
const element = document.createElement("div"); | ||
element.innerHTML = template; | ||
return element.firstElementChild; | ||
} | ||
|
||
selectSubElements() { | ||
this.element.querySelectorAll('[data-element]').forEach(element => { | ||
this.subElements[element.dataset.element] = element; | ||
}); | ||
} | ||
|
||
createTableHeaderArrowTemplate() { | ||
return `<span data-element="arrow" class="sortable-table__sort-arrow"> | ||
<span class="sort-arrow"></span> | ||
</span>`; | ||
} | ||
|
||
createTableHeaderCellTemplate(columnData) { | ||
const isSortable = columnData.sortable && this.orderValue && columnData.id === this.fieldValue; | ||
const dataOrder = isSortable ? `data-order="${this.orderValue}"` : ''; | ||
return ` | ||
<div | ||
class="sortable-table__cell" | ||
data-id="${columnData.id}" | ||
data-sortable="${columnData.sortable}" | ||
${dataOrder} | ||
> | ||
<span>${columnData.title}</span> | ||
${isSortable ? this.createTableHeaderArrowTemplate() : ''} | ||
</div> | ||
`; | ||
} | ||
|
||
createTableHeaderTemplate() { | ||
return this.headerConfig.map(headerColumn => ( | ||
this.createTableHeaderCellTemplate(headerColumn) | ||
)).join(''); | ||
} | ||
|
||
createTableBodyCellTemplate(product, columnConfig) { | ||
const id = columnConfig.id; | ||
|
||
if (columnConfig.template) { | ||
return columnConfig.template(product); | ||
} | ||
return ( | ||
` | ||
<div class="sortable-table__cell">${product[id]}</div> | ||
` | ||
); | ||
} | ||
|
||
createTableBodyRowTemplate(product) { | ||
return ( | ||
` | ||
<a href="/products/3d-ochki-epson-elpgs03" class="sortable-table__row"> | ||
${this.headerConfig.map(column => this.createTableBodyCellTemplate(product, column)).join('')} | ||
</a> | ||
` | ||
); | ||
} | ||
|
||
createTableBodyTemplate(data) { | ||
return data.map((product) => ( | ||
this.createTableBodyRowTemplate(product) | ||
)).join(''); | ||
} | ||
|
||
createTemplate() { | ||
return ( | ||
` | ||
<div class="sortable-table"> | ||
<div data-element="header" class="sortable-table__header sortable-table__row"> | ||
${this.createTableHeaderTemplate()} | ||
</div> | ||
<div data-element="body" class="sortable-table__body"> | ||
${this.createTableBodyTemplate(this.data)} | ||
</div> | ||
<div data-element="loading" class="loading-line sortable-table__loading-line"></div> | ||
|
||
<div data-element="emptyPlaceholder" class="sortable-table__empty-placeholder"> | ||
<div> | ||
<p>No products satisfies your filter criteria</p> | ||
<button type="button" class="button-primary-outline">Reset all filters</button> | ||
</div> | ||
</div> | ||
</div> | ||
` | ||
); | ||
} | ||
|
||
sortByType(sortType) { | ||
if (sortType === 'number') { | ||
return this.sortNumbers(this.data, this.fieldValue, this.orderValue); | ||
} | ||
if (sortType === 'string') { | ||
return this.sortStrings(this.data, this.fieldValue, this.orderValue); | ||
} | ||
} | ||
|
||
sortNumbers(data) { | ||
if (this.orderValue === 'asc') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В этом и методе ниже можно использовать
и умножать на этот коэффициент результат сравнения, например
|
||
return [...data].sort((a, b) => a[this.fieldValue] - b[this.fieldValue]); | ||
} | ||
if (this.orderValue === 'desc') { | ||
return [...data].sort((a, b) => b[this.fieldValue] - a[this.fieldValue]); | ||
} | ||
return [...data]; | ||
} | ||
|
||
sortStrings(data) { | ||
if (this.orderValue === 'asc') { | ||
return [...data].sort( | ||
(a, b) => { | ||
return a[this.fieldValue].localeCompare(b[this.fieldValue], ['ru', 'en'], { caseFirst: "upper" }); | ||
}); | ||
} | ||
if (this.orderValue === 'desc') { | ||
return [...data].sort( | ||
(a, b) => { | ||
return b[this.fieldValue].localeCompare(a[this.fieldValue], ['ru', 'en'], { caseFirst: "upper" }); | ||
}); | ||
} | ||
return [...data]; | ||
} | ||
|
||
sort(fieldValue, orderValue) { | ||
this.fieldValue = fieldValue; | ||
this.orderValue = orderValue; | ||
|
||
this.setDataOrderAttribute(orderValue); | ||
|
||
const sortType = this.headerConfig.find(obj => obj.id === fieldValue).sortType; | ||
const sortedData = this.sortByType(sortType); | ||
|
||
this.updateTemplate(sortedData); | ||
} | ||
|
||
setDataOrderAttribute() { | ||
const sortableElems = document.body.querySelectorAll('.sortable-table__cell'); | ||
for (let element of sortableElems) { | ||
element.setAttribute('data-order', this.orderValue); | ||
} | ||
} | ||
|
||
updateTemplate(sortedData) { | ||
this.subElements.header.innerHTML = this.createTableHeaderTemplate(); | ||
this.subElements.body.innerHTML = this.createTableBodyTemplate(sortedData); | ||
} | ||
|
||
remove() { | ||
this.element.remove(); | ||
} | ||
|
||
destroy() { | ||
this.remove(); | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/product/${product.id}
?