Skip to content

Commit

Permalink
current row number for grouped tables
Browse files Browse the repository at this point in the history
  • Loading branch information
xaksis committed Feb 17, 2021
1 parent 7d18500 commit 323207d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
1 change: 0 additions & 1 deletion dev/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
styleClass="vgt-table bordered"
:sort-options="{
enabled: true,
initialSortBy: [{field: 'name', type: 'asc'}],
}"
:search-options="{
enabled: true,
Expand Down
7 changes: 4 additions & 3 deletions dev/grouped-table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
}"
:pagination-options="{
enabled: true,
perPage: 2,
perPage: 5,
}"
:group-options="{
enabled: true,
headerPosition: 'top',
collapsable: true,
collapsable: false,
}"
styleClass="vgt-table condensed bordered"
ref="groupedTable"
Expand Down Expand Up @@ -84,7 +84,8 @@ export default {
],
},
{
name: 'Reptile Total',
label: 'Reptile Total',
mode: 'span',
diet: '',
count: '',
children: [
Expand Down
34 changes: 27 additions & 7 deletions src/components/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
:ofText="ofText"
:pageText="pageText"
:allText="allText"
:paginated="paginated"
></vgt-pagination>
</slot>
<vgt-global-search
Expand Down Expand Up @@ -173,8 +174,8 @@

<!-- Table body starts here -->
<tbody
v-for="(headerRow, index) in paginated"
:key="index"
v-for="(headerRow, hIndex) in paginated"
:key="hIndex"
>
<!-- if group row header is at the top -->
<vgt-header-row
Expand All @@ -191,7 +192,7 @@
:class="getRowStyleClass(headerRow)"
:get-classes="getClasses"
:full-colspan="fullColspan"
:groupIndex="index"
:groupIndex="hIndex"
@on-select-group-change="toggleSelectGroup($event, headerRow)"
>
<template
Expand Down Expand Up @@ -223,7 +224,7 @@
v-if="lineNumbers"
class="line-numbers"
>
{{ getCurrentIndex(index) }}
{{ getCurrentIndex(row.originalIndex) }}
</th>
<th
v-if="selectable"
Expand Down Expand Up @@ -330,6 +331,7 @@
:ofText="ofText"
:pageText="pageText"
:allText="allText"
:paginated="paginated"
></vgt-pagination>
</slot>
</div>
Expand Down Expand Up @@ -691,14 +693,14 @@ export default {
totalRowCount() {
let total = 0;
each(this.processedRows, (headerRow) => {
total += headerRow.children ? headerRow.children.length : 1;
total += headerRow.children ? headerRow.children.length : 0;
});
return total;
},
totalPageRowCount() {
let total = 0;
each(this.paginated, (headerRow) => {
total += headerRow.children ? headerRow.children.length : 1;
total += headerRow.children ? headerRow.children.length : 0;
});
return total;
},
Expand Down Expand Up @@ -1386,7 +1388,25 @@ export default {
this.filteredRows = computedRows;
},
getCurrentIndex(index) {
getCurrentIndex(rowId) {
console.log(rowId);
let index = 0;
let found = false;
for (let i = 0; i < this.paginated.length; i += 1) {
const headerRow = this.paginated[i];
const { children } = headerRow;
if (children && children.length) {
for (let j = 0; j < children.length; j += 1) {
const c = children[j];
if (c.originalIndex === rowId) {
found = true;
break;
}
index += 1;
}
}
if (found) break;
}
return ((this.currentPage - 1) * this.currentPerPage) + index + 1;
},
Expand Down
4 changes: 3 additions & 1 deletion src/components/VgtPagination.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export default {
ofText: { default: 'of' },
pageText: { default: 'page' },
allText: { default: 'All' },
paginated: {},
},
data() {
Expand Down Expand Up @@ -127,7 +128,8 @@ export default {
// Current displayed items
paginatedInfo() {
let first = ((this.currentPage - 1) * this.currentPerPage) + 1;
const last = Math.min(this.total, this.currentPage * this.currentPerPage);
let last = Math.min(this.total, this.currentPage * this.currentPerPage);
// last = last - this.paginated.length;
if (last === 0) {
first = 0;
Expand Down
15 changes: 11 additions & 4 deletions src/components/utils/sort.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
const DEFAULT_SORT_TYPE = 'asc';
const SORT_TYPES = {
Ascending: 'asc',
Descending: 'desc',
None: 'none',
};

function getColumnFirstSortType(column) {
return column.firstSortType || DEFAULT_SORT_TYPE;
Expand All @@ -11,9 +16,10 @@ function getCurrentPrimarySort(sortArray, column) {
}

function getNextSort(currentSort) {
return (currentSort === 'asc')
? 'desc'
: DEFAULT_SORT_TYPE;
if (currentSort === SORT_TYPES.Ascending) {
return SORT_TYPES.Descending;
}
return SORT_TYPES.Ascending;
}

function getIndex(sortArray, column) {
Expand All @@ -25,9 +31,10 @@ function getIndex(sortArray, column) {

exports.primarySort = (sortArray, column) => {
const currentPrimarySort = getCurrentPrimarySort(sortArray, column);
const nextPrimarySort = getNextSort(currentPrimarySort);
return [{
field: column.field,
type: currentPrimarySort ? getNextSort(currentPrimarySort) : getColumnFirstSortType(column),
type: currentPrimarySort ? nextPrimarySort : getColumnFirstSortType(column),
}];
};

Expand Down

0 comments on commit 323207d

Please sign in to comment.