Add intuitive table selection (#35444)

Add intuitive table selection with class .row-with-select
Added to propal, order and invoice list
This commit is contained in:
Anthony Damhet
2025-09-22 22:20:52 +02:00
committed by GitHub
parent 00fe10a75f
commit 1df61bbd98
6 changed files with 113 additions and 5 deletions

View File

@@ -1704,5 +1704,94 @@ function onKanbanColumnChange(item, newColumn) {
item.data('original-column', newColumn);
}
/*
* Intuitive table selection
*/
$(function() {
/**
* @param {jQuery} el
* @param {Integer} status
*/
let setLastClickedRowStatus = function (el, status = 1){
$('.row-with-select').attr('data-is-last-changed', 0);
el.attr('data-is-last-changed', status === 0 ? 0 : 1);
}
/**
* Remove data-is-last-changed on double click
* Because if data-is-last-changed is present the user can't select text
*/
$(document).on("dblclick", ".row-with-select", function(e) {
$('.row-with-select[data-is-last-changed]').removeAttr( 'data-is-last-changed' );
});
/**
* DISABLE on click a and button
* Because Ctrl + Click on link is also used for open ion a new tab
* we need to block select tool
*/
$(document).on("click", ".row-with-select a, .row-with-select button", function (e) {
// we need to block select tool
if (e.ctrlKey) {
e.stopPropagation();
}
});
$(document).on("mousedown click", ".row-with-select input.checkforselect", function (e) {
// Prevents automatic change of “checked”
e.preventDefault();
e.stopPropagation(); // parent click trigger will be done below
let parentRow = $(this).closest(".row-with-select");
// this part of code prevent weird behavior when user (ctrl or maj) + click directly on checkbox
// We simulate a click on the parent line
parentRow.trigger({
type: "click",
ctrlKey: !e.shiftKey, // simulate ctrlKey click will automatically prop activate the checkbox with parent event but not if shift key is pressed.
metaKey: !e.shiftKey, // simulate metaKey click will automatically prop activate the checkbox with parent event but not if shift key is pressed.
shiftKey: e.shiftKey,
originalEvent: e
});
});
$(document).on("click", ".row-with-select", function (e) {
let checkBox = $(this).find('.checkforselect');
let nextCheckStatus = !checkBox.is(':checked')
if (e.ctrlKey || e.metaKey) {
// Add line to selection
if(checkBox){
checkBox.prop('checked', nextCheckStatus).trigger('change');
}
setLastClickedRowStatus($(this), 1);
}
if (e.shiftKey) {
let lastLastChanged = $(this).closest('table').find('.row-with-select[data-is-last-changed="1"]');
if(lastLastChanged.length>0){
// Add all lines to selection betwin last selected line
if($(this).index() === lastLastChanged.index()) {
return null;
}
if($(this).index() < lastLastChanged.index()) {
$(this).nextUntil(lastLastChanged, ".row-with-select" ).find('.checkforselect').prop('checked', nextCheckStatus).trigger('change');
}else{
lastLastChanged.nextUntil($(this), ".row-with-select" ).find('.checkforselect').prop('checked', nextCheckStatus).trigger('change');
}
lastLastChanged.find('.checkforselect').prop('checked', nextCheckStatus).trigger('change');
checkBox.prop('checked', nextCheckStatus).trigger('change');
setLastClickedRowStatus($(this), 1);
}
}
});
});
// End of lib_head.js.php