class Sorting {
sorting (a, b) {
return (a > b) ? 1 : -1
}
sortingByDate (a, b) {
let dmy1, dmy2, date1, date2
let months = {
'января': '01',
'февраля': '02',
'марта': '03',
'апреля': '04',
'мая': '05',
'июня': '06',
'июля': '07',
'августа': '08',
'сентября': '09',
'октября': '10',
'ноября': '11',
'декабря': '12'
}
let regexp = /(\d+)[/.\s](.+)[/.\s](\d+)/i
dmy1 = a.split(regexp).splice(1, 3)
dmy2 = b.split(regexp).splice(1, 3)
if (dmy1[1] in months) {
dmy1[1] = months[dmy1[1]]
}
if (dmy2[1] in months) {
dmy2[1] = months[dmy2[1]]
}
date1 = new Date(dmy1[2], dmy1[1], dmy1[0]).getTime()
date2 = new Date(dmy2[2], dmy2[1], dmy2[0]).getTime()
return (date1 > date2) ? 1 : -1
}
}
class ElementsTable extends Sorting {
createOptionTags (arr) {
let optionTags = ''
optionTags += '<option selected="" disabled="" value="all">Выберите столбец</option>'
optionTags += '<option value="all">Все столбцы</option>'
for (let key in arr[0]) {
optionTags += `<option value="${key}">${key}</option>`
}
return optionTags
}
createThTags (arr) {
let thTags = ''
for (let key in arr[0]) {
thTags += `<th>${key}</th>`
}
return thTags
}
createTrTags (arr) {
let trTags = ''
for (let i = 0; i < arr.length; i++) {
let tdTags = ''
for (let key in arr[i]) {
tdTags += `<td>${arr[i][key]}</td>`
}
trTags += `<tr>${tdTags}</tr>`
}
return trTags
}
}
class Table extends ElementsTable {
/**
* @param {Array} data - data array
* @param {Number} id - table id
*/
constructor ({ data, id }) {
super()
this.id = id
this.inData = data // входные данные
this.outData = data // данные для вывода
this.selectedColumn = 'all'
this.typeSort = 0
let optionTags = this.createOptionTags(this.inData)
$('[name=column]').html(optionTags)
}
createArrayFromObj (obj) {
let arr = []
for (let i = 0; i < obj.length; i++) {
let row = obj[i]
arr.push(row[this.selectedColumn])
}
return arr
}
createObjFromArray (obj) {
let arr = []
for (let i = 0; i < arr.length; i++) {
let obj = {}
obj[this.selectedColumn] = arr[i]
arr.push(obj)
}
return arr
}
/*
* Find string in data array and return array with all matches
* @param {string} str
* @return {Array}
*/
findStringInData (str) {
let result = []
for (let j = 0; j < this.inData.length; j++) {
let dataRow = this.inData[j]
let dataValue = dataRow[this.selectedColumn]
let dataStr = String(dataValue).toLowerCase()
if (dataStr.indexOf(str) > -1) {
result.push(dataRow)
}
}
return result
}
/**
* @param {string} type - type of sorting
*/
sort () {
let arr = this.createArrayFromObj(this.inData)
this.typeSort = $('[name=sorting]').val()
switch (this.typeSort) {
case 'lower':
if (this.selectedColumn === 'date') {
arr = arr.sort(this.sortingByDate).reverse()
break
}
arr = arr.sort(this.sorting).reverse()
break
case 'upper':
if (this.selectedColumn === 'date') {
arr = arr.sort(this.sortingByDate)
break
}
arr = arr.sort(this.sorting)
break
default:
this.selectColumn(this.selectedColumn)
break
}
return this.createObjFromArray(arr)
}
search () {
let str = $('[name=search]')[0].value
let result = this.findStringInData(str.toLowerCase())
if (str === '') {
return this.selectColumn()
}
if (result.length === 0) {
alert('Нет результатов')
return
}
return result
}
selectColumn () {
let data = []
this.selectedColumn = $('[name=column]').val()
if (this.selectedColumn === 'all') {
return this.inData
}
for (let i = 0; i < this.inData.length; i++) {
let key = this.selectedColumn
let value = this.inData[i][key]
let obj = {}
obj[key] = value
data.push(obj)
}
return data
}
/**
* @param {string} param - function of class
* @param {string} name - name of field
*/
click (param, name) {
if (this.selectedColumn === 'all' && param !== 'selectColumn') {
alert('Выберите столбец!')
return
}
this.outData = this[param]()
this.printData()
}
printData () {
let thTags = this.createThTags(this.outData)
let trTags = this.createTrTags(this.outData)
$(`#${this.id} thead`).html(thTags)
$(`#${this.id} tbody`).html(trTags)
}
}