mirror of
https://github.com/crater-invoice/crater.git
synced 2025-10-29 20:51:09 -04:00
init crater
This commit is contained in:
64
resources/assets/js/components/base/base-table/classes/Column.js
Executable file
64
resources/assets/js/components/base/base-table/classes/Column.js
Executable file
@ -0,0 +1,64 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
61
resources/assets/js/components/base/base-table/classes/Row.js
Executable file
61
resources/assets/js/components/base/base-table/classes/Row.js
Executable file
@ -0,0 +1,61 @@
|
||||
import moment from 'moment'
|
||||
import { get } from '../helpers'
|
||||
|
||||
export default class Row {
|
||||
constructor (data, columns) {
|
||||
this.data = data
|
||||
this.columns = columns
|
||||
}
|
||||
|
||||
getValue (columnName) {
|
||||
return get(this.data, columnName)
|
||||
}
|
||||
|
||||
getColumn (columnName) {
|
||||
return this.columns.find(column => (column.show === columnName || column.sortAs === columnName))
|
||||
}
|
||||
|
||||
getFilterableValue (columnName) {
|
||||
const value = this.getValue(columnName)
|
||||
|
||||
if (!value) {
|
||||
return ''
|
||||
}
|
||||
|
||||
return value.toString().toLowerCase()
|
||||
}
|
||||
|
||||
getSortableValue (columnName) {
|
||||
const dataType = this.getColumn(columnName).dataType
|
||||
|
||||
let value = this.getValue(columnName)
|
||||
|
||||
if (value === undefined || value === null) {
|
||||
return ''
|
||||
}
|
||||
|
||||
if (value instanceof String) {
|
||||
value = value.toLowerCase()
|
||||
}
|
||||
|
||||
if (dataType.startsWith('date')) {
|
||||
const format = dataType.replace('date:', '')
|
||||
|
||||
return moment(value, format).format('YYYYMMDDHHmmss')
|
||||
}
|
||||
|
||||
if (dataType === 'numeric') {
|
||||
return value
|
||||
}
|
||||
|
||||
return value.toString()
|
||||
}
|
||||
|
||||
passesFilter (filter) {
|
||||
return this.columns
|
||||
.filter(column => column.isFilterable())
|
||||
.map(column => this.getFilterableValue(column.getFilterFieldName()))
|
||||
.filter(filterableValue => filterableValue.indexOf(filter.toLowerCase()) >= 0)
|
||||
.length
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user