mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
[ADD] web_tree_resize_column
[FIX] Initial resizing
This commit is contained in:
21
web_tree_resize_column/static/lib/resizableColumns/LICENSE
Normal file
21
web_tree_resize_column/static/lib/resizableColumns/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Jones Vinoth Joseph
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,47 @@
|
||||
(function($) {
|
||||
$.fn.resizableColumns = function() {
|
||||
var isColResizing = false;
|
||||
var resizingPosX = 0;
|
||||
var _table = $(this);
|
||||
var _thead = $(this).find('thead');
|
||||
|
||||
_thead.find('th').each(function() {
|
||||
$(this).css('position', 'relative');
|
||||
if ($(this).is(':not(:last-child)')) $(this).append("<div class='resizer' style='position:absolute;top:0px;right:-3px;bottom:0px;width:6px;z-index:999;background:transparent;cursor:col-resize'></div>");
|
||||
})
|
||||
|
||||
$(document).mouseup(function(e) {
|
||||
_thead.find('th').removeClass('resizing');
|
||||
isColResizing = false;
|
||||
e.stopPropagation();
|
||||
})
|
||||
|
||||
_table.find('.resizer').mousedown(function(e) {
|
||||
_thead.find('th').removeClass('resizing');
|
||||
$(_thead).find('tr:first-child th:nth-child(' + ($(this).closest('th').index() + 1) + ') .resizer').closest('th').addClass('resizing');
|
||||
resizingPosX = e.pageX;
|
||||
isColResizing = true;
|
||||
e.stopPropagation();
|
||||
})
|
||||
|
||||
_table.mousemove(function(e) {
|
||||
if (isColResizing) {
|
||||
|
||||
var _resizing = _thead.find('th.resizing .resizer');
|
||||
if (_resizing.length == 1) {
|
||||
var _nextRow = _thead.find('th.resizing + th');
|
||||
var _pageX = e.pageX || 0;
|
||||
var _widthDiff = _pageX - resizingPosX;
|
||||
var _setWidth = _resizing.closest('th').innerWidth() + _widthDiff;
|
||||
var _nextRowWidth = _nextRow.innerWidth() - _widthDiff;
|
||||
if (resizingPosX != 0 && _widthDiff != 0 && _setWidth > 50 && _nextRowWidth > 50) {
|
||||
_resizing.closest('th').innerWidth(_setWidth);
|
||||
resizingPosX = e.pageX;
|
||||
_nextRow.innerWidth(_nextRowWidth);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
}
|
||||
(jQuery));
|
||||
47
web_tree_resize_column/static/src/js/backend.js
Normal file
47
web_tree_resize_column/static/src/js/backend.js
Normal file
@@ -0,0 +1,47 @@
|
||||
/* Copyright 2019 Onestein
|
||||
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
|
||||
|
||||
odoo.define('web_tree_resize_column.backend', function (require) {
|
||||
"use strict";
|
||||
|
||||
var ListRenderer = require('web.ListRenderer');
|
||||
|
||||
ListRenderer.include({
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
_renderView: function() {
|
||||
// Preserve width of columns
|
||||
var styles = [];
|
||||
this.$el.find('thead th').each(function () {
|
||||
styles.push($(this).attr('style'));
|
||||
});
|
||||
|
||||
var res = this._super.apply(this, arguments);
|
||||
|
||||
// Initialize jQuery plugin
|
||||
this.$el.find('table').resizableColumns();
|
||||
|
||||
// Restore width of columns
|
||||
this.$el.find('thead th').each(function (index, th) {
|
||||
$(th).attr('style', styles[index]);
|
||||
});
|
||||
|
||||
return res;
|
||||
},
|
||||
|
||||
/**
|
||||
* Prevent sorting when the user is resizing a column.
|
||||
*
|
||||
* @override
|
||||
*/
|
||||
_onSortColumn: function (event) {
|
||||
if ($(event.target).is('.resizer')) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._super.apply(this, arguments);
|
||||
},
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user