DHTMLX Docs & Samples Explorer

Adjust column size

Double clicking on any header (or using dropdown below the grid) allows you to adjust column size according to cell value size.


Source
<link rel="STYLESHEET" type="text/css" href="../../codebase/dhtmlxgrid.css">
<link rel="stylesheet" type="text/css" href="../../codebase/skins/dhtmlxgrid_dhx_skyblue.css">
<script  src="../../codebase/dhtmlxcommon.js"></script>
<script  src="../../codebase/dhtmlxgrid.js"></script>
<script  src="../../codebase/dhtmlxgridcell.js"></script>
 
 
<table width="600">
    <tr>
        <td width="50%" valign="top">
            <div id="gridbox" style="width:100%;height:270px;background-color:white;"></div>
        </td>
    </tr>
    <tr>
        <td>
            <div><input id='z1' type="button"  onclick='mygrid.adjustColumnSize(this.nextSibling.value)' value="Adjust column" /><select><option value="0">Sales
                <option value="1">Book title
                <option value="2">Author
                <option value="3">Price
                <option value="4">In store
                <option value="5">Shipping
                <option value="6">Bestseller
                <option value="7">Date
            </select></div>
        </td>
    </tr>
</table>
<script>
function m_f2(id) {
    document.getElementById("a_1").innerHTML = "Selected row: " + id;
}
function doIt() {
    mygrid.enableEditEvents(document.getElementById('z1').checked, document.getElementById('z2').checked, document.getElementById('z3').checked);
}
mygrid = new dhtmlXGridObject('gridbox');
mygrid.setImagePath("../../codebase/imgs/");
mygrid.setHeader("Sales,Book Title,Author,Price,In Store,Shipping,Bestseller,Date of Publication");
mygrid.setInitWidths("50,50,50,50,50,50,50,50");
mygrid.setColAlign("right,left,left,right,center,left,center,center");
mygrid.setColTypes("dyn,ed,ed,price,ch,co,ra,ro");
mygrid.getCombo(5).put(2, 2);
mygrid.setColSorting("na,na,na,na,na,na,na,na");
mygrid.enableColumnAutoSize(true);
mygrid.init();
mygrid.setSkin("dhx_skyblue");
mygrid.loadXML("../common/grid_16_rows_columns_manipulations.xml");
</script>