Created
April 17, 2023 13:28
-
-
Save fininhors/e48e8cd253dc4415d8b04bd25b1e171e to your computer and use it in GitHub Desktop.
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
export class Datatable { | |
constructor(selector) { | |
this.table = null; | |
this.url = null; | |
this.buttons = [ | |
// { extend: 'copy', className: 'btn' }, | |
// { extend: 'csv', className: 'btn' }, | |
// { extend: 'excel', className: 'btn' }, | |
// { extend: 'pdfHtml5', className: 'btn', exportOptions: { columns: ':not(.no-export)' } }, | |
// { extend: 'print', className: 'btn' }, | |
]; | |
this.columns = null; | |
this.columnDefs = null; | |
this.lengthMenu = [5, 10, 20, 50, 100]; | |
this.pageLength = 10; | |
this.dom = `<'dt--top-section'<'row'<'col-12 col-sm-2 d-flex justify-content-sm-start justify-content-center'l> | |
<'col-12 col-sm-5 d-flex justify-content-center'B> | |
<'col-12 col-sm-5 d-flex justify-content-md-end justify-content-center mt-md-0 mt-3'f>>> | |
<'table-responsive'tr> | |
<'dt--bottom-section d-sm-flex justify-content-sm-between text-center' | |
<'dt--pages-count mb-sm-0 mb-3'i><'dt--pagination'p>>`; | |
this.selector = selector; | |
this.language = { | |
url: 'https://cdn.datatables.net/plug-ins/1.10.21/i18n/Portuguese-Brasil.json' | |
}; | |
this.processing = true; | |
this.serverSide = true; | |
this.stripeClasses = []; | |
} | |
setUrl (url) { | |
if (!url) { | |
throw new Error("URL não pode ser vazia."); | |
} | |
this.url = url; | |
return this; | |
} | |
setData (data) { | |
if (!data || !Array.isArray(data) || !data.length) { | |
throw new Error('O parâmetro Data precisa ser um array e não pode ser vazio'); | |
} | |
this.data = data; | |
return this; | |
} | |
setColumns (columns) { | |
if (!columns || !Array.isArray(columns) || !columns.length) { | |
throw new Error("O parâmetro Colunas deve ser um array e não pode estar vazio."); | |
} | |
this.columns = columns; | |
return this; | |
} | |
setColumnDefs (columnDefs) { | |
if (!columnDefs || !Array.isArray(columnDefs) || !columnDefs.length) { | |
throw new Error("O parâmetro de definição de colunas deve ser um array e não pode estar vazio."); | |
} | |
this.columnDefs = columnDefs; | |
return this; | |
} | |
setLengthMenu (lengthMenu) { | |
if (!lengthMenu || !Array.isArray(lengthMenu) || !lengthMenu.every(Number.isInteger)) { | |
throw new Error("O parâmetro Menu de itens por página deve ser um array de números inteiros."); | |
} | |
this.lengthMenu = lengthMenu; | |
return this; | |
} | |
setPageLength (pageLength) { | |
if (!pageLength || isNaN(pageLength)) { | |
throw new Error("O parâmetro Itens por página deve ser um número."); | |
} | |
this.pageLength = pageLength; | |
return this; | |
} | |
setDom (dom) { | |
if (!dom) { | |
throw new Error('O DOM precisa ser definido'); | |
} | |
this.dom = dom; | |
return this; | |
} | |
setStripeClasses (stripeClasses) { | |
if (!stripeClasses) { | |
throw new Error('O parâmetro de definição das classes precisa ser definido.'); | |
} | |
if (typeof stripeClasses !== 'string' && !Array.isArray(stripeClasses)) { | |
throw new Error('O parâmetro de definição das classes deve ser uma string ou um array.'); | |
} | |
this.stripeClasses = stripeClasses; | |
return this; | |
} | |
setButtons (buttons) { | |
if (!buttons || !Array.isArray(buttons) || !buttons.length) { | |
throw new Error('O parâmetro de definição dos botões deve ser um array e não pode estar vazio.'); | |
} | |
this.buttons = buttons; | |
return this; | |
} | |
setProcessing (value) { | |
if (value === undefined || value === null) { | |
throw new Error('O valor de "processing" deve ser definido.'); | |
} | |
this.processing = value && !this.data; | |
return this; | |
} | |
setServerSide (value) { | |
if (value === undefined || value === null) { | |
throw new Error('O valor de "serverSide" deve ser definido.'); | |
} | |
this.serverSide = value && !this.data; | |
return this; | |
} | |
init () { | |
if (!this.url && !this.data) { | |
throw new Error('Você deve definir a opção "url" ou "data"'); | |
} | |
if (!this.columns) { | |
throw new Error("As colunas precisam estar definidas."); | |
} | |
const dataProp = this.data ? "data" : "ajax"; | |
const dataValue = this.data || this.url; | |
console.clear(); | |
console.table({ | |
[dataProp]: dataValue, | |
processing: this.processing, | |
serverSide: this.serverSide, | |
columns: this.columns, | |
columnDefs: this.columnDefs, | |
lengthMenu: this.lengthMenu, | |
pageLength: this.pageLength, | |
dom: this.dom, | |
language: this.language, | |
stripeClasses: this.stripeClasses, | |
}); | |
this.table = $(this.selector).DataTable({ | |
[dataProp]: dataValue, | |
processing: this.processing, | |
serverSide: this.serverSide, | |
columns: this.columns, | |
columnDefs: this.columnDefs, | |
lengthMenu: this.lengthMenu, | |
pageLength: this.pageLength, | |
dom: this.dom, | |
language: this.language, | |
stripeClasses: this.stripeClasses, | |
}); | |
} | |
reload () { | |
if (!this.table) { | |
throw new Error("Datatable não foi inicializada."); | |
} | |
this.table.ajax.reload(); | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment