Reputation: 41
I'm using vuetify v-data-table. I implement drag column with SortableJs and also column resizable.
When I drag a column, it works properly but after that the resize does not work anymore.
<div id="app">
<v-app id="inspire">
<v-data-table :headers="headers" :items="desserts"
sort-by="calories"
disable-sort
v-sortable-table="{onEnd:sortTheHeadersAndUpdateTheKey}"
:key="anIncreasingNumber" >
</v-data-table>
</v-app>
</div>
Here is my codepen
Thanks in advance.
Upvotes: 2
Views: 4587
Reputation: 1309
As I can see here, the Vuetify team is not interested in doing this feature now.
Thank you for the Feature Request and interest in improving Vuetify. Unfortunately, this is not a functionality we are looking to implement now.
For now, you can check this CodepPen it will do the trick for you. If you don't want to do it manually you could install an npm package called vue-columns-resizable-vuetify
<!-- Credit
Column Resize - Web Dev Tricks https://webdevtrick.com/resizable-table-columns/
Vuetify Datatable - Vuetify Example
https://vuetifyjs.com/en/components/data-tables/ -->
<div id="app">
<v-app id="inspire">
<v-data-table
:headers="headers"
:items="desserts"
class="elevation-1"
>
</v-data-table>
</v-app>
</div>
th, td {
border-right: 1px solid grey;
}
new Vue({
el: '#app',
vuetify: new Vuetify(),
data () {
return {
headers: [
{ text: 'Dessert (100g serving)', sortable: false, value: 'name'},
{ text: 'Calories', sortable: false, value: 'calories' },
{ text: 'Fat (g)', sortable: false, value: 'fat' },
{ text: 'Carbs (g)', sortable: false, value: 'carbs' },
{ text: 'Protein (g)', sortable: false, value: 'protein' },
{ text: 'Iron (%)', sortable: false, value: 'iron' },
],
desserts: [
{
name: 'Frozen Yogurt',
calories: 159,
fat: 6.0,
carbs: 24,
protein: 4.0,
iron: '1%',
},
{
name: 'Ice cream sandwich',
calories: 237,
fat: 9.0,
carbs: 37,
protein: 4.3,
iron: '1%',
},
{
name: 'Eclair',
calories: 262,
fat: 16.0,
carbs: 23,
protein: 6.0,
iron: '7%',
},
{
name: 'Cupcake',
calories: 305,
fat: 3.7,
carbs: 67,
protein: 4.3,
iron: '8%',
},
{
name: 'Gingerbread',
calories: 356,
fat: 16.0,
carbs: 49,
protein: 3.9,
iron: '16%',
},
{
name: 'Jelly bean',
calories: 375,
fat: 0.0,
carbs: 94,
protein: 0.0,
iron: '0%',
},
{
name: 'Lollipop',
calories: 392,
fat: 0.2,
carbs: 98,
protein: 0,
iron: '2%',
},
{
name: 'Honeycomb',
calories: 408,
fat: 3.2,
carbs: 87,
protein: 6.5,
iron: '45%',
},
{
name: 'Donut',
calories: 452,
fat: 25.0,
carbs: 51,
protein: 4.9,
iron: '22%',
},
{
name: 'KitKat',
calories: 518,
fat: 26.0,
carbs: 65,
protein: 7,
iron: '6%',
},
],
}
},
})
var tables = document.getElementsByTagName('table');
for (var i=0; i<tables.length;i++){
resizableGrid(tables[i]);
}
function resizableGrid(table) {
var row = table.getElementsByTagName('tr')[0],
cols = row ? row.children : undefined;
if (!cols) return;
table.style.overflow = 'hidden';
var tableHeight = table.offsetHeight;
for (var i=0;i<cols.length;i++){
var div = createDiv(tableHeight);
cols[i].appendChild(div);
cols[i].style.position = 'relative';
setListeners(div);
}
function setListeners(div){
var pageX,curCol,nxtCol,curColWidth,nxtColWidth;
div.addEventListener('mousedown', function (e) {
curCol = e.target.parentElement;
nxtCol = curCol.nextElementSibling;
pageX = e.pageX;
var padding = paddingDiff(curCol);
curColWidth = curCol.offsetWidth - padding;
if (nxtCol)
nxtColWidth = nxtCol.offsetWidth - padding;
});
div.addEventListener('mouseover', function (e) {
e.target.style.borderRight = '2px solid #0000ff';
})
div.addEventListener('mouseout', function (e) {
e.target.style.borderRight = '';
})
document.addEventListener('mousemove', function (e) {
if (curCol) {
var diffX = e.pageX - pageX;
if (nxtCol)
nxtCol.style.width = (nxtColWidth - (diffX))+'px';
curCol.style.width = (curColWidth + diffX)+'px';
}
});
document.addEventListener('mouseup', function (e) {
curCol = undefined;
nxtCol = undefined;
pageX = undefined;
nxtColWidth = undefined;
curColWidth = undefined
});
}
function createDiv(height){
var div = document.createElement('div');
div.style.top = 0;
div.style.right = 0;
div.style.width = '5px';
div.style.position = 'absolute';
div.style.cursor = 'col-resize';
div.style.userSelect = 'none';
div.style.height = height + 'px';
return div;
}
function paddingDiff(col){
if (getStyleVal(col,'box-sizing') == 'border-box'){
return 0;
}
var padLeft = getStyleVal(col,'padding-left');
var padRight = getStyleVal(col,'padding-right');
return (parseInt(padLeft) + parseInt(padRight));
}
function getStyleVal(elm,css){
return (window.getComputedStyle(elm, null).getPropertyValue(css))
}
};
Upvotes: 2