EasyUI Forum
October 15, 2025, 10:36:55 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: [SOLVED] Datagrid column same width depending on longest title  (Read 13705 times)
jega
Full Member
***
Posts: 225


View Profile
« on: October 12, 2024, 11:47:47 AM »

HI.

Can't find anythig about this.

Have a datagrid with dynamic columns. How can i set the width to alle columns depending on the longest header title.

Jesper
« Last Edit: October 16, 2024, 12:08:19 AM by jega » Logged
jega
Full Member
***
Posts: 225


View Profile
« Reply #1 on: October 14, 2024, 02:00:18 AM »

One solution

var colWidth = 0;
$.each(scrollCols, function(index, element) {
     if ($('#'+element.id).width() > colWidth){colWidth = $('#'+element.id).width()}
});
$.each(scrollCols, function(index, element) {
   $('#'+element.id).css('width',colWidth);
});

BUT, is there a better way to do it ??
Logged
jarry
Administrator
Hero Member
*****
Posts: 2300


View Profile Email
« Reply #2 on: October 15, 2024, 07:15:36 PM »

Here is the extended method to achieve this functionality.
Code:
$.extend($.fn.datagrid.methods, {
setSameColumnWidth: function (jq) {
return jq.each(function () {
const dg = $(this);
let colWidth = 0;
const fields1 = dg.datagrid('getColumnFields',true);
const fields2 = dg.datagrid('getColumnFields',false);
const fields = fields1.concat(fields2);
fields.forEach(field => {
const opt = dg.datagrid('getColumnOption', field);
if (colWidth < opt.width) {
colWidth = opt.width;
}
});
fields.forEach(field => {
const opt = dg.datagrid('getColumnOption', field);
opt.width = colWidth;
opt.boxWidth = colWidth - opt.deltaWidth;
});
dg.datagrid('fixColumnSize').datagrid('fitColumns');
})
}
})
Usage example:
Code:
$('#dg').datagrid('setSameColumnWidth')
Logged
jega
Full Member
***
Posts: 225


View Profile
« Reply #3 on: October 16, 2024, 12:07:35 AM »

Hi jarry


Thanks. Works fine.

Any way to revert it without reload ??
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!