mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-27 01:05:21 +08:00
Add checkbox selector to datatable
This commit is contained in:
parent
fd930ff5d5
commit
2b9062ac92
4 changed files with 108 additions and 7 deletions
|
@ -1,13 +1,15 @@
|
||||||
/* global DataTableHelpers */
|
/* global DataTableHelpers DataTableCheckboxes */
|
||||||
(function() {
|
(function() {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var REPOSITORIES_TABLE;
|
var REPOSITORIES_TABLE;
|
||||||
|
var CHECKBOX_SELECTOR;
|
||||||
|
|
||||||
function initRepositoriesDataTable(tableContainer, archived = false) {
|
function initRepositoriesDataTable(tableContainer, archived = false) {
|
||||||
var tableTemplate = archived ? $('#archivedRepositoriesListTable').html() : $('#activeRepositoriesListTable').html();
|
var tableTemplate = archived ? $('#archivedRepositoriesListTable').html() : $('#activeRepositoriesListTable').html();
|
||||||
$.get($(tableTemplate).data('source'), function(data) {
|
$.get($(tableTemplate).data('source'), function(data) {
|
||||||
if (REPOSITORIES_TABLE) REPOSITORIES_TABLE.destroy();
|
if (REPOSITORIES_TABLE) REPOSITORIES_TABLE.destroy();
|
||||||
|
CHECKBOX_SELECTOR = null;
|
||||||
$('.content-body').html(tableTemplate);
|
$('.content-body').html(tableTemplate);
|
||||||
REPOSITORIES_TABLE = $(tableContainer).DataTable({
|
REPOSITORIES_TABLE = $(tableContainer).DataTable({
|
||||||
aaData: data,
|
aaData: data,
|
||||||
|
@ -35,14 +37,23 @@
|
||||||
return `<a href="${row.repositoryUrl}">${value}</a>`;
|
return `<a href="${row.repositoryUrl}">${value}</a>`;
|
||||||
}
|
}
|
||||||
}],
|
}],
|
||||||
|
fnInitComplete: function(e) {
|
||||||
fnInitComplete: function() {
|
var dataTableWrapper = $(e.nTableWrapper);
|
||||||
var dataTableWrapper = $(tableContainer).closest('.dataTables_wrapper');
|
CHECKBOX_SELECTOR = new DataTableCheckboxes(dataTableWrapper, {
|
||||||
|
checkboxSelector: '.repository-row-selector',
|
||||||
|
selectAllSelector: '.select-all-checkbox'
|
||||||
|
});
|
||||||
DataTableHelpers.initLengthApearance(dataTableWrapper);
|
DataTableHelpers.initLengthApearance(dataTableWrapper);
|
||||||
DataTableHelpers.initSearchField(dataTableWrapper);
|
DataTableHelpers.initSearchField(dataTableWrapper);
|
||||||
$('.content-body .toolbar').html($('#repositoriesListButtons').html());
|
$('.content-body .toolbar').html($('#repositoriesListButtons').html());
|
||||||
dataTableWrapper.find('.main-actions, .pagination-row').removeClass('hidden');
|
dataTableWrapper.find('.main-actions, .pagination-row').removeClass('hidden');
|
||||||
$('.create-new-repository').initializeModal('#create-repo-modal');
|
$('.create-new-repository').initializeModal('#create-repo-modal');
|
||||||
|
},
|
||||||
|
drawCallback: function() {
|
||||||
|
if (CHECKBOX_SELECTOR) CHECKBOX_SELECTOR.checkSelectAllStatus();
|
||||||
|
},
|
||||||
|
rowCallback: function(row) {
|
||||||
|
if (CHECKBOX_SELECTOR) CHECKBOX_SELECTOR.checkRowStatus(row);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -35,3 +35,93 @@ var DataTableHelpers = (function() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}());
|
}());
|
||||||
|
|
||||||
|
class DataTableCheckboxes {
|
||||||
|
|
||||||
|
/* config = {
|
||||||
|
checkboxSelector: selector for checkboxes,
|
||||||
|
selectAllSelector: selector for select all checkbox
|
||||||
|
}*/
|
||||||
|
|
||||||
|
constructor(tableWrapper, config) {
|
||||||
|
this.selectedRows = [];
|
||||||
|
this.tableWrapper = $(tableWrapper);
|
||||||
|
this.config = config;
|
||||||
|
|
||||||
|
this.#initCheckboxes();
|
||||||
|
this.#initSelectAllCheckbox();
|
||||||
|
}
|
||||||
|
|
||||||
|
checkRowStatus = (row) => {
|
||||||
|
var checkbox = $(row).find(this.config.checkboxSelector);
|
||||||
|
if (this.selectedRows.includes(row.id)) {
|
||||||
|
$(row).addClass('selected');
|
||||||
|
checkbox.attr('checked', true);
|
||||||
|
} else {
|
||||||
|
$(row).removeClass('selected');
|
||||||
|
checkbox.attr('checked', false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
checkSelectAllStatus = () => {
|
||||||
|
var checkboxes = this.tableWrapper.find(this.config.checkboxSelector);
|
||||||
|
var selectedCheckboxes = this.tableWrapper.find(this.config.checkboxSelector + ':checked');
|
||||||
|
var selectAllCheckbox = this.tableWrapper.find(this.config.selectAllSelector);
|
||||||
|
selectAllCheckbox.prop('indeterminate', false);
|
||||||
|
if (selectedCheckboxes.length === 0) {
|
||||||
|
selectAllCheckbox.prop('checked', false);
|
||||||
|
} else if (selectedCheckboxes.length === checkboxes.length) {
|
||||||
|
selectAllCheckbox.prop('checked', true);
|
||||||
|
} else {
|
||||||
|
selectAllCheckbox.prop('indeterminate', true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clearSelection = () => {
|
||||||
|
var rows = this.tableWrapper.find('tbody tr');
|
||||||
|
this.selectedRows = [];
|
||||||
|
$.each(rows, (i, row) => {
|
||||||
|
$(row).removeClass('selected');
|
||||||
|
$(row).find(this.config.checkboxSelector).attr('checked', false);
|
||||||
|
});
|
||||||
|
this.checkSelectAllStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
// private methods
|
||||||
|
|
||||||
|
#initCheckboxes = () => {
|
||||||
|
this.tableWrapper.on('click', '.table tbody tr', (e) => {
|
||||||
|
var checkbox = $(e.currentTarget).find(this.config.checkboxSelector);
|
||||||
|
checkbox.prop('checked', !checkbox.prop('checked'));
|
||||||
|
this.#selectRow(e.currentTarget);
|
||||||
|
}).on('click', this.config.checkboxSelector, (e) => {
|
||||||
|
this.#selectRow($(e.currentTarget).closest('tr')[0]);
|
||||||
|
e.stopPropagation();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#selectRow = (row) => {
|
||||||
|
var id = row.id;
|
||||||
|
if (this.selectedRows.includes(id)) {
|
||||||
|
this.selectedRows.splice(this.selectedRows.indexOf(id), 1);
|
||||||
|
} else {
|
||||||
|
this.selectedRows.push(id);
|
||||||
|
}
|
||||||
|
$(row).toggleClass('selected');
|
||||||
|
this.checkSelectAllStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
#initSelectAllCheckbox = () => {
|
||||||
|
this.tableWrapper.on('click', this.config.selectAllSelector, (e) => {
|
||||||
|
var selectAllCheckbox = $(e.currentTarget);
|
||||||
|
var rows = this.tableWrapper.find('tbody tr');
|
||||||
|
$.each(rows, (i, row) => {
|
||||||
|
var checkbox = $(row).find(this.config.checkboxSelector);
|
||||||
|
if (checkbox.prop('checked') === selectAllCheckbox.prop('checked')) return;
|
||||||
|
|
||||||
|
checkbox.prop('checked', !checkbox.prop('checked'));
|
||||||
|
this.#selectRow(row);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ module RepositoriesDatatableHelper
|
||||||
repositories = repositories.includes(:repository_rows, :team, :created_by, :archived_by)
|
repositories = repositories.includes(:repository_rows, :team, :created_by, :archived_by)
|
||||||
repositories.each do |repository|
|
repositories.each do |repository|
|
||||||
result.push(
|
result.push(
|
||||||
'DT_RepositoryId': repository.id,
|
'DT_RowId': repository.id,
|
||||||
'1': escape_input(repository.name),
|
'1': escape_input(repository.name),
|
||||||
'2': repository.repository_rows.size,
|
'2': repository.repository_rows.size,
|
||||||
'3': repository.shared_with?(team),
|
'3': repository.shared_with?(team),
|
||||||
|
|
|
@ -32,7 +32,7 @@
|
||||||
<tr>
|
<tr>
|
||||||
<th>
|
<th>
|
||||||
<div class="sci-checkbox-container">
|
<div class="sci-checkbox-container">
|
||||||
<input value="1" type="checkbox" class="sci-checkbox">
|
<input value="1" type="checkbox" class="sci-checkbox select-all-checkbox">
|
||||||
<span class="sci-checkbox-label"></span>
|
<span class="sci-checkbox-label"></span>
|
||||||
</div>
|
</div>
|
||||||
</th>
|
</th>
|
||||||
|
@ -57,7 +57,7 @@
|
||||||
<tr>
|
<tr>
|
||||||
<th>
|
<th>
|
||||||
<div class="sci-checkbox-container">
|
<div class="sci-checkbox-container">
|
||||||
<input value="1" type="checkbox" class="sci-checkbox">
|
<input value="1" type="checkbox" class="sci-checkbox select-all-checkbox">
|
||||||
<span class="sci-checkbox-label"></span>
|
<span class="sci-checkbox-label"></span>
|
||||||
</div>
|
</div>
|
||||||
</th>
|
</th>
|
||||||
|
|
Loading…
Reference in a new issue