Files
crater/resources/assets/js/components/base/base-table/classes/Column.js
Mohit Panjwani bdf2ba51d6 init crater
2019-11-11 12:16:00 +05:30

65 lines
1.6 KiB
JavaScript
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { pick } from '../helpers'
export default class Column {
constructor (columnComponent) {
const properties = pick(columnComponent, [
'show', 'label', 'dataType', 'sortable', 'sortBy', 'filterable',
'filterOn', 'hidden', 'formatter', 'cellClass', 'headerClass', 'sortAs'
])
for (const property in properties) {
this[property] = columnComponent[property]
}
this.template = columnComponent.$scopedSlots.default
}
isFilterable () {
return this.filterable
}
getFilterFieldName () {
return this.filterOn || this.show
}
isSortable () {
return this.sortable
}
getSortPredicate (sortOrder, allColumns) {
const sortFieldName = this.getSortFieldName()
const sortColumn = allColumns.find(column => (column.sortAs === sortFieldName || column.show === sortFieldName))
const dataType = sortColumn.dataType
if (dataType.startsWith('date') || dataType === 'numeric') {
return (row1, row2) => {
const value1 = row1.getSortableValue(sortFieldName)
const value2 = row2.getSortableValue(sortFieldName)
if (sortOrder === 'desc') {
return value2 < value1 ? -1 : 1
}
return value1 < value2 ? -1 : 1
}
}
return (row1, row2) => {
const value1 = row1.getSortableValue(sortFieldName)
const value2 = row2.getSortableValue(sortFieldName)
if (sortOrder === 'desc') {
return value2.localeCompare(value1)
}
return value1.localeCompare(value2)
}
}
getSortFieldName () {
return this.sortBy || this.sortAs || this.show
}
}