Reputation: 2022
I am using the following function(which i got from web) to resize the column in kendo ui. this is based on Index, i am looking out to see if there can be an option to select by column title or field/key.
when i reorder the grid column, this function fails.
function resizeColumn(idx, width) {
$("#grid .k-grid-header-wrap") //header
.find("colgroup col")
.eq(idx)
.css({ width: width });
$("#grid .k-grid-content") //content
.find("colgroup col")
.eq(idx)
.css({ width: width });
}
Upvotes: 2
Views: 8619
Reputation: 676
click here to get complete Answer
Loading Column State
function loadColumnState(columnStateKey: string, realGrid): void
{
var colState = JSON.parse($.jStorage.get(columnStateKey));
if(colState && colState.length > 0)
{
var visibleIndex = -1;
for (var i = 0; i < colState.length; i++)
{
var column = colState[i];
// 1. Set correct order first as visibility and width both depend on this.
var existingIndex = -1;
if (typeof column.field !== 'undefined')
{
existingIndex = findFieldIndex(realGrid, column.field);
}
else if (typeof column.commandName !== 'undefined')
{
existingIndex = findCommandIndex(realGrid, column.commandName);
}
if (existingIndex > -1 && existingIndex != i) // Different index
{ // Need to reorder
realGrid.reorderColumn(i, realGrid.columns[existingIndex]);
}
// 2. Set visibility state
var isHidden = (typeof column.hidden === 'undefined') ? false : column.hidden;
if (isHidden)
{
realGrid.hideColumn(i);
}
else
{
realGrid.showColumn(i);
++visibleIndex;
}
// 3. Set width
var width = (typeof column.width === 'undefined') ? null : column.width;
if(width != null)
{
realGrid.columns[i].width = width; // This sets value, whilst rest redraws
realGrid.thead.prev().find('col:eq(' + visibleIndex + ')').width(width);
realGrid.table.find('>colgroup col:eq(' + visibleIndex + ')').width(width);
}
}
}
}
Upvotes: 1
Reputation: 31
Search column by field id to make sure that is the corrected field.
function resizeColumn(fieldId, width) {
var index = $('#grid .k-grid-header-wrap').find('th[data-field="' + fieldId + '"]').index();
$("#grid .k-grid-header-wrap") //header
.find("colgroup col")
.eq(index)
.css({ width: width });
$("#grid .k-grid-content") //content
.find("colgroup col")
.eq(index)
.css({ width: width });
}
Upvotes: 2
Reputation: 18402
To resize by column title, you just need to figure out the correct index, e.g. like this:
function resizeColumn(title, width) {
var index = $("#grid .k-grid-header-wrap").find("th:contains(" + title + ")").index();
$("#grid .k-grid-header-wrap") //header
.find("colgroup col")
.eq(index)
.css({ width: width });
$("#grid .k-grid-content") //content
.find("colgroup col")
.eq(index)
.css({ width: width });
}
Upvotes: 3