Skip to content

Commit

Permalink
Deploying to package from @ be7e875 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbitgood committed Jun 22, 2022
1 parent 32656ca commit f32cce8
Show file tree
Hide file tree
Showing 17 changed files with 826 additions and 824 deletions.
51 changes: 24 additions & 27 deletions Datatable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import RowsPerPage from './components/RowsPerPage.svelte';
import { onMount, onDestroy } from 'svelte';
import Datatable from './app/Datatable.js';
import Datatable from './app/DataTable.js';
import Component from './app/Component.js';
import { context } from './app/context';
import { context } from './app/Context.js';
export let data = [];
export let classList = '';
export let id = 'svelte-simple-datatable';
export let filters = {};
export let settings = {
Expand All @@ -25,7 +24,8 @@
scrollY: false,
labels: {
info: '{start} - {end} / {rows}', // $t("table_info", {rows: rows}), //
previous: '<svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"><path fill-rule="evenodd" d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd" /></svg>',
previous:
'<svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"><path fill-rule="evenodd" d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd" /></svg>',
next: '<svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"><path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd" /></svg>'
},
filters: filters
Expand All @@ -37,53 +37,50 @@
const options = datatable.getOptions();
const component = new Component(id);
const rows = datatable.getRows();
export const dataRows = rows;
$: {
component.setRows(data);
options.set(options.parse(settings));
}
onMount(() => component.init());
onDestroy(() => component.reset());
</script>


</script>

<section {id} class="datatable {classList}" class:scroll-y={$options.scrollY}>

<div class="relative flex items-stretch grow focus-within:z-10 mt-6">
<div class="mt-1 relative rounded-md shadow-sm border mx-auto w-2/3 max-w-lg">
<div class="absolute inset-y-0 left-0 flex items-center">
<RowsPerPage />
</div>
<SearchInput classList="h-9 block w-full pl-16 sm:text-sm border-gray-300 rounded-md sm:text-sm border-gray-300 text-gray-700 dark:bg-gray-300
bg-[url('/img/icons/filter.png')] bg-[length:15px_15px] bg-no-repeat bg-[top_10px_right_8px]" />
<div class="absolute inset-y-0 left-0 flex items-center">
<RowsPerPage />
</div>
<SearchInput
classList="h-12 block w-full pl-16 text-sm border-gray-300 rounded-md border-gray-300 text-gray-700 dark:bg-gray-300
md:bg-[url('/img/icons/filter.png')] md:bg-[length:15px_15px] bg-no-repeat md:bg-[top_10px_right_8px]"
/>
</div>
</div>


<div class="dt-table flex flex-col lg:flex-row">
<div class="lg:w-1/6">
<ColumnFilterInputs />
</div>

<table class="relative w-full lg:w-5/6 mt-3">
<table class="relative w-full lg:w-5/6 mt-3 ml-2 lg:ml-6">
<slot />
</table>
</div>

<div
class="bg-white dark:bg-gray-800 text-gray-700 dark:text-gray-100 px-4 py-3 flex flex-col items-center justify-between border-t border-gray-200 sm:px-6"
class="text-gray-700 dark:text-gray-100 mt-6 px-4 sm:px-6 pt-4 md:pt-8 flex flex-col
items-center justify-between border-t border-gray-200 dark:border-gray-500 "
>

<nav class="relative z-0 inline-flex mx-auto rounded-md -space-x-px" aria-label="Pagination">
<PaginationButtons context={context} />
</nav>
<section class="mx-auto" class:css={$options.css}>
<RowCount {id} />
<nav class="relative z-0 inline-flex mx-auto rounded-md -space-x-px" aria-label="Pagination">
<PaginationButtons {context} />
</nav>
<section class="mx-auto mt-2 text-primary-600 dark:text-primary-300" class:css={$options.css}>
<RowCount {id} />
</section>

</div>
</section>
</section>
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Simple Svelte-Tailwind Datatables
# Simple Svelte-Tailwind Datatables

### An opinionated fork of [svelte simple-datatables](https://github.com/vincjo/svelte-simple-datatables/)

## Developing
Expand Down
247 changes: 127 additions & 120 deletions app/Component.js
Original file line number Diff line number Diff line change
@@ -1,131 +1,138 @@
import { context } from './context.js'
import { context } from './Context.js';

export default class Component
{
constructor( id = 'svelte-simple-datatable' )
{
this.context = context.get(id)
this.id = id
}
init()
{
this.setColumns()
this.resize()
this.addEventScrollX()
new ResizeObserver((mutations) => {
this.resize()
}).observe(document.querySelector(`#${this.id}`).parentElement)
}
export default class Component {
constructor(id = 'svelte-simple-datatable') {
this.context = context.get(id);
this.id = id;
}
init() {
this.setColumns();
this.resize();
this.addEventScrollX();
new ResizeObserver(() => {
this.resize();
}).observe(document.querySelector(`#${this.id}`).parentElement);
}

reset ()
{
this.context.getPageNumber().update(store => store = 1)
this.context.getGlobalFilter().remove()
this.context.getLocalFilters().remove()
this.context.getColumns().set([])
}
reset() {
// eslint-disable-next-line no-unused-vars
this.context.getPageNumber().update((store) => (store = 1));
this.context.getGlobalFilter().remove();
this.context.getLocalFilters().remove();
this.context.getColumns().set([]);
}

setRows(data)
{
for ( const item of data ) {
setRows(data) {
for (const item of data) {
for (const key of Object.keys(item)) {
if (item[key] === null) {
item[key] = '';
}
}
}
this.context.getData().set(data);
return;
}

for ( const key of Object.keys(item) ) {
if (item[key] === null) {
item[key] = ''
}
}
getSize() {
const parent = document.querySelector(`#${this.id}`).parentNode;
const style = getComputedStyle(parent);
const rect = parent.getBoundingClientRect();
const getNumber = (pxValue) => {
return parseFloat(pxValue.replace('px', ''));
};
return {
parentWidth: rect.width,
parentHeight: rect.height,
width:
(rect.width -
getNumber(style.paddingLeft) -
getNumber(style.paddingRight) -
getNumber(style.borderLeftWidth) -
getNumber(style.borderRightWidth)) /
rect.width,
height:
(rect.height -
getNumber(style.paddingTop) -
getNumber(style.paddingBottom) -
getNumber(style.borderTopWidth) -
getNumber(style.borderBottomWidth)) /
rect.height,
top: style.paddingTop,
right: style.paddingRight,
bottom: style.paddingBottom,
left: style.paddingLeft
};
}

}
this.context.getData().set(data)
return
}
resize() {
if (!document.querySelector(`#${this.id}`)) return;
const size = this.getSize();
const tableContainer = document.querySelector(`#${this.id} .dt-table`);
// if ( this.getOptions().scrollY ) {
// tableContainer.style.height = this.setTableContainerHeight(size.parentHeight * size.height) + 'px'
// this.context.getColumns().redraw()
// }
this.context.getDatatableWidth().set(size.parentWidth * size.width);
if (size.parentWidth * size.width < document.querySelector(`#${this.id} table`).offsetWidth) {
tableContainer.style.overflowX = 'auto';
}
}

getSize()
{
const parent = document.querySelector(`#${this.id}`).parentNode
const style = getComputedStyle(parent)
const rect = parent.getBoundingClientRect()
const getNumber = (pxValue) => { return parseFloat(pxValue.replace('px', '')) }
return {
parentWidth: rect.width,
parentHeight: rect.height,
width: (rect.width - getNumber(style.paddingLeft) - getNumber(style.paddingRight) - getNumber(style.borderLeftWidth) - getNumber(style.borderRightWidth)) / rect.width,
height: (rect.height - getNumber(style.paddingTop) - getNumber(style.paddingBottom) - getNumber(style.borderTopWidth) - getNumber(style.borderBottomWidth)) / rect.height,
top: style.paddingTop,
right: style.paddingRight,
bottom: style.paddingBottom,
left: style.paddingLeft
}
}
setTableContainerHeight(height) {
const calc = [
document.querySelector(`#${this.id} .dt-search`).getBoundingClientRect().height,
document.querySelector(`#${this.id} .dt-pagination`).getBoundingClientRect().height
];
const sum = (a, b) => a + b;
document.querySelector(`#${this.id} .dt-table`).style.height = height - calc.reduce(sum) + 'px';
}

resize()
{
if ( !document.querySelector(`#${this.id}`) ) return
const size = this.getSize()
const tableContainer = document.querySelector(`#${this.id} .dt-table`)
// if ( this.getOptions().scrollY ) {
// tableContainer.style.height = this.setTableContainerHeight(size.parentHeight * size.height) + 'px'
// this.context.getColumns().redraw()
// }
this.context.getDatatableWidth().set( size.parentWidth * size.width )
if (size.parentWidth * size.width < document.querySelector(`#${this.id} table`).offsetWidth) {
tableContainer.style.overflowX = 'auto'
}
}
addEventScrollX() {
if (this.getOptions().scrollY) {
document.querySelector(`#${this.id} .dt-table`).addEventListener('scroll', (e) => {
document.querySelector(`#${this.id} .dt-header`).style.left =
-1 * e.target.scrollLeft + 'px';
});
}
}

setTableContainerHeight(height)
{
const calc = [
document.querySelector(`#${this.id} .dt-search`).getBoundingClientRect().height,
document.querySelector(`#${this.id} .dt-pagination`).getBoundingClientRect().height
]
const sum = (a, b) => a + b
document.querySelector(`#${this.id} .dt-table`).style.height = height - calc.reduce(sum) + 'px'
}

addEventScrollX()
{
if ( this.getOptions().scrollY ) {
document.querySelector(`#${this.id} .dt-table`).addEventListener('scroll', (e) => {
document.querySelector(`#${this.id} .dt-header`).style.left = (-1 * e.target.scrollLeft) + 'px'
})
}
}

setColumns() {
setTimeout( () => {
const columnList = []
let i = 0
document.querySelectorAll(`#${this.id} table thead th`).forEach(th => {
columnList.push({
index: i,
html: th.innerHTML,
key: this.getKey(th.dataset.key),
setColumns() {
setTimeout(() => {
const columnList = [];
let i = 0;
document.querySelectorAll(`#${this.id} table thead th`).forEach((th) => {
columnList.push({
index: i,
html: th.innerHTML,
key: this.getKey(th.dataset.key),
name: th.dataset.name,
sort: null,
classList: th.classList,
minWidth: th.getBoundingClientRect().width
})
th.addEventListener('click', (e) => {
this.context.getColumns().sort(e.target, this.getKey(th.dataset.key))
}, true)
i++
})
this.context.getColumns().set(columnList)
}, 25)
}
sort: null,
classList: th.classList,
minWidth: th.getBoundingClientRect().width
});
th.addEventListener(
'click',
(e) => {
this.context.getColumns().sort(e.target, this.getKey(th.dataset.key));
},
true
);
i++;
});
this.context.getColumns().set(columnList);
}, 25);
}

getKey(key)
{
if (!key) return
if (key && key.indexOf('=>') > 0) {
return new Function(`'use strict';return (${key})`)()
}
return (x) => x[key]
}
getKey(key) {
if (!key) return;
if (key && key.indexOf('=>') > 0) {
return new Function(`'use strict';return (${key})`)();
}
return (x) => x[key];
}

getOptions()
{
return this.context.getOptions().get(this.context.getOptions())
}
}
getOptions() {
return this.context.getOptions().get(this.context.getOptions());
}
}
Loading

0 comments on commit f32cce8

Please sign in to comment.