Kendo Grid : Blank space in the end when you resize(reduce) any column - kendo-ui

I have a grid and when i resize (reduce) any column, there is a white space shows up in the end of the grid. I have checked with the Kendo official samples and it seems like in some samples the shown behavior is in their samples.
I tried setting up width for header, cell content etc.. but its still shows some UI issues and i have multiple grids and i need a generic fix.
If its not an issue and a behavior then somebody please have a look at this and explain how to fix it.
I have added a normal screen shot and resized screenshot.
normal
after resize
For testing it out, i have added a jsfiddle.,
http://jsfiddle.net/49bhz2sk/
html
<div class="panel panel-body">
<div id="fleetInfoGridDisplayDummy" class="" data-bind="autoHeightContainer:true"></div>
</div>
script
$("#fleetInfoGridDisplayDummy").kendoGrid({
dataSource: {
type: "odata",
transport: {
read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Customers"
},
pageSize: 20
},
navigatable: true,
selectable: true,
sortable: true,
reorderable: true,
resizable: true,
scrollable: { virtual: true },
columnMenu: true, // Needed to hide and show columns.
pageable: {
refresh: true,
pageSizes: true,
buttonCount: 5
},
columns: [{
template: "<div class='customer-photo'" +
"style='background-image: url(../content/web/Customers/#:data.CustomerID#.jpg);'></div>" +
"<div class='customer-name'>#: ContactName #</div>",
field: "ContactName",
title: "Contact Name",
width: 240
}, {
field: "ContactTitle",
title: "Contact Title"
}, {
field: "CompanyName",
title: "Company Name"
}, {
field: "Country",
width: 150
}]
});

I have posted this in telerik forum and got a reply from the admin, here is how they suggested to resolve the issue. Posted here so that someone else can benefit from this.
Answer proposed by 'Drew B.' also works, i have seen that too in another post. The the code i posted is less cumbersome with minimal coding.
columnResize: function (e) {
// what is thead and tbody: https://docs.telerik.com/kendo-ui/api/javascript/ui/grid#fields
var grid = e.sender,
gridHeaderTable = grid.thead.parent(),
gridBodyTable = grid.tbody.parent();
// what is wrapper: https://docs.telerik.com/kendo-ui/api/javascript/ui/widget/fields/wrapper
// what is scrollbar(): https://docs.telerik.com/kendo-ui/api/javascript/kendo/fields/support
if (gridBodyTable.width() < grid.wrapper.width() - kendo.support.scrollbar()) {
// remove the width style from the last VISIBLE column's col element
gridHeaderTable.find("> colgroup > col").last().width("");
gridBodyTable.find("> colgroup > col").last().width("");
// remove the width property from the last VISIBLE column's object
// https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/fields/columns
grid.columns[grid.columns.length - 1].width = "";
// remove the Grid tables' pixel width
gridHeaderTable.width("");
gridBodyTable.width("");
}
},

According to various kendo sources, this is a mixture of observed normal behavior (2013), and unexpected behavior (2017). Kendo does provide a workaround for this issue, as I suspect it isn't necessarily kendo related but more an HTML/Table feature.
<style>
.k-grid {
width: 700px;
}
</style>
<div id="grid1"></div>
<script>
function getMasterColumnsWidth(tbl) {
var result = 0;
tbl.children("colgroup").find("col").not(":last").each(function (idx, element) {
result += parseInt($(element).outerWidth() || 0, 10);
});
return result;
}
function adjustLastColumn() {
var grid = $("#grid1").data("kendoGrid");
var contentDiv = grid.wrapper.children(".k-grid-content");
var masterHeaderTable = grid.thead.parent();
var masterBodyTable = contentDiv.children("table");
var gridDivWidth = contentDiv.width() - kendo.support.scrollbar();
masterHeaderTable.width("");
masterBodyTable.width("");
var headerWidth = getMasterColumnsWidth(masterHeaderTable),
lastHeaderColElement = grid.thead.parent().find("col").last(),
lastDataColElement = grid.tbody.parent().children("colgroup").find("col").last(),
delta = parseInt(gridDivWidth, 10) - parseInt(headerWidth, 10);
if (delta > 0) {
delta = Math.abs(delta);
lastHeaderColElement.width(delta);
lastDataColElement.width(delta);
} else {
lastHeaderColElement.width(0);
lastDataColElement.width(0);
}
contentDiv.scrollLeft(contentDiv.scrollLeft() - 1);
contentDiv.scrollLeft(contentDiv.scrollLeft() + 1);
}
$("#grid1").kendoGrid({
dataSource: {
type: "odata",
transport: {
read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Employees"
},
pageSize: 6,
serverPaging: true,
serverSorting: true
},
height: 430,
pageable: true,
resizable: true,
columnResize: adjustLastColumn,
dataBound: adjustLastColumn,
columns: [{
field: "FirstName",
title: "First Name",
width: "100px"
}, {
field: "LastName",
title: "Last Name",
width: "150px"
}, {
field: "Country",
width: "100px"
}, {
field: "City",
width: "100px"
}, {
field: "Title",
width: "200px"
}, {
template: " "
}]
});
</script>

Related

Kendo grid column lock issue

I have a big problem with a Kendo grid when locking columns. The grid has two static columns and one or more columns created dynamically. Without locking any column, the grid works just fine, when i'm locking the static columns (or just one of them), the grid goes mad.
I know that locking columns will create two different tables (with locked and unlocked columns) but in my case, these tables (with different divs) won't align in my page.
I tried to align them manually using CSS (display:inline-block and/or float: left) but when I resize the window, the grid comes back to the wrong form.
I attached two examples:
In the second example, the rest of the columns are situated somewhere under the page (i need to zoom out to see them).
Inserted simplified code (without locked applied)
Code for columns:
var columns = [
{
field: "Field_Locked1",
title: "F1",
editor: "",
attributes: { style: "text-align: left;" },
sortable: false,
footerTemplate: "T",
width: "30px"
},
{
field: "Field_locked2",
title: "F2",
editable: false,
attributes: { style: "text-align: left;" },
template: "#=test(string)#",
footerTemplate: "<div style='text-align: right;'>1</div><hr/><div style= 'text-align: right;'>2</div>",
width: "370px",
sortable: false
}
];
for (var i = 0; i < customColumns.length; i++) {
var itemTab = customColumns[i];
var dinamicColumn = {
field: itemTab.Name,
title: itemTab.Code,
editable: false,
type: "number",
attributes: { style: "text-align: right;" },
sortable: false,
width: "200px",
template: "#= test(string)#",
footerTemplate: "<div style='text-align: right;'>1</div><hr/><div style= 'text-align: right;'>2</div>"
};
columns.splice(columns.length, 0, dinamicColumn);
}
Code for creating grid:
function createGridGeneric(tabElement,columns) {
var gridModel = tabElement.templateDescription.grid;
var idGrid = gridModel.id;
var gridToolbal = gridModel.toolbar != null ? gridModel.toolbar:null;
var gridFooter = gridModel.footer != null ? gridModel.footer : null;
var hasArea = tabElement.filterArea != null ? tabElement.filterArea.visible : false;
var grid = $("#" + idGrid).data("kendoGrid");
if (grid != null) {
$("#" + idGrid).empty();
}
$("#" + idGrid).kendoGrid({
autoBind: false,
columns: columns,
editable: false,
filterable: false,
groupable: false,
selectable: false,
sortable: false,
scrollable: true,
resizable: true,
save: function(e) {
},
saveChanges: function(e) {
},
columnResize: function(e) {
gridColumnResize(e);
},
databound: function(e) { }
});
return grid;
}

Kendo grid column width doesn't reduce beyond a point

I'm trying to have a kendo grid column extremely thin, basically like a line. Reducing the width property below 10px or 10 doesn't seem to have any effect.
Here's what I'm doing:
let mainGridOptions = {
dataSource: myData,
columns: [{
field: "color",
title: " ",
width: '5px' // tried even without px, just 5
}]
};
Anyone know what's happening?
I am not able to reproduce it I please try with the below code snippet or look in this demo.
<div id="grid"></div>
<script>
$(document).ready(function () {
$("#grid").kendoGrid({
dataSource: {
type: "odata",
transport: {
read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Customers"
},
pageSize: 20
},
height: 550,
groupable: true,
sortable: true,
pageable: {
refresh: true,
pageSizes: true,
buttonCount: 5
},
columns: [{
field: "ContactTitle",
title: "Contact Title"
}, {
field: "CompanyName",
title: "Company Name"
}, {
field: "Country",
width: '5px'
}]
});
});
</script>
If you will set the column width to '5px' then you could not see anything into column because some default spacing(Margin/padding) applied to the columns.

Event called when sorting data in kendo grid

I have the sample codes as follows:
$(document).ready(function () {
$("#grid").kendoGrid({
dataSource: getData(),
height: 550,
sortable: true,
pageable: true,
columns: [{
field: "ContactName",
title: "Contact Name",
width: 200
}, {
field: "ContactTitle",
title: "Contact Title"
}, {
field: "CompanyName",
title: "Company Name"
}]
});
function whenSorting(){
//// DO SOMETIME......
}
});
Now what I want is when I do sorting of any field the function "whenSorting" will be called. How to do that?
You have local sorting enabled "sortable: true," , for this you can capture it with databound event
$(document).ready(function () {
$("#grid").kendoGrid({
dataSource: getData(),
height: 550,
sortable: true,
pageable: true,
columns: [{
field: "ContactName",
title: "Contact Name",
width: 200
}, {
field: "ContactTitle",
title: "Contact Title"
}, {
field: "CompanyName",
title: "Company Name"
}],
dataBound: function(e) {
whenSorting();
}
});
function whenSorting(){
//// DO SOMETIME......
}
});
IF you are using server sorting you can handle it in the server read .
Hope this helps
You may bind Change function and check whether sorting exist or not on every grid change
$('#grid').data('kendoGrid').dataSource.bind('change',function(){
// Get the grid object
var grid = $("#grid").data("kendoGrid");
// Get the datasource bound to the grid
var ds = grid.dataSource;
// Get current sorting
var sort = ds.sort();
// Display sorting fields and direction
if (sort) {
for (var i = 0; i < sort.length; i++) {
alert ("Field:" + sort[i].field + " direction:" + sort[i].dir);
}
} else {
alert("no sorting");
}
});
i hope this will help you
You could define a custom sort function for each of your columns and fire your whenSorting event from there, like this:
$(document).ready(function () {
$("#grid").kendoGrid({
dataSource: getData(),
height: 550,
sortable: true,
pageable: true,
columns: [{
field: "ContactName",
title: "Contact Name",
width: 200,
sortable { compare: whenSorting }
}, {
field: "ContactTitle",
title: "Contact Title",
sortable { compare: whenSorting }
}, {
field: "CompanyName",
title: "Company Name",
sortable { compare: whenSorting }
}]
});
function whenSorting(a, b){
//// DO SOMETIME......
return a == b
? 0
: a > b
? 1
: -1;
}
});
I was using jQuery to hide columns, I was not able to use kendo's hideColumn and showColumn functions. When sorting I would end up with a column I wanted hidden showing up after the sort event was fired. I found that using the above mentioned block then writing a function using jQuery to show or hide the column worked like I intended things to.
$(document).ready(function () {
$("#grid").kendoGrid({
dataSource: getData(),
height: 550,
sortable: true,
pageable: true,
columns: [{
field: "ContactName",
title: "Contact Name",
width: 200
}, {
field: "ContactTitle",
title: "Contact Title"
}, {
field: "CompanyName",
title: "Company Name"
}],
dataBound: function(e) {
whenSorting();
}
});
function whenSorting(){
if(_checkBox.is(':checked'){
grid.find("table th").eq(4).show();
grid.children("div:eq(1)").find("table").find("tr").each(function () {
$(this).children("td:eq(4)").show();
});
}
});
Might want to check your formatting with this one too.

Kendo Grid - In Firefox triggering an alert in the save event causes a page reload

I want to know if anyone has seen this. I have a kendo grid and in the save event, I want to do some validation. Basically to make sure that the new values difference between the old is less than or equal to the balance.
So if it is greater than the balance, I trigger an alert and preventDefault. However displaying an alert causes the page to reload in FireFox.
Here is my grid:
$("#gridOpenInvoices").kendoGrid({
autoBind: false,
dataSource: openInvoicesData,
scrollable: true,
sortable: false,
navigatable: true,
toolbar: [{name:"save",
text:strings.ApplyUpdates},
{name: "SplitPayment",
text: strings.SplitPayment},
{name: "PaidOut",
text: strings.PaidOut},
{name: "CreateCredit",
text: strings.CreditBalance},
{name: "UndoAllocation",
text: strings.UndoAllocation}],
columns: [
{
field: "CustomerName",
title: strings.AccountName,
hidden: true
},
{
field: "OpenTransactionId",
title: strings.OpenTransaction,
width:90
},
{
field: "InvoiceNumber" ,
title: strings.InvoiceNumber,
width:65
},
{
field: "GrossSales",
title: strings.OriginalAmount,
width: 75,
format: "{0:c}",
decimals: 2,
min: 1,
value: 0
},
{
field: "Balance",
title: strings.TransBalance,
width: 75,
format: "{0:c}",
decimals: 2,
min: 1,
value: 0
},
{
field: "Date",
title: strings.Date,
width:75,
format: "{0:MM/dd/yy}"
},
{
field: "Age",
title: strings.Age,
width:55
},
{
field: "CheckNumber",
title: strings.CheckNumber,
width:85
},
{
title: strings.ApplyPayment,
width:75,
template: "<input class='k-chk-applied chkbox' type='checkbox' data-bind='source: IsApplied' name='IsApplied' #= IsApplied ? 'checked' : ''#/>",
attributes:{"class":"tdIsApplied"}
},
{
field:"AppliedAmount",
title: strings.AppliedAmount,
format: "{0:c}",
width: 95,
attributes: {"class" : "tdAppliedAmount"}
}],
save: function(e) {
var model = e.model;
if (e.values.AppliedAmount != null) {
var remainingBalance = context.getRemainingBalance();
var difference = e.values.AppliedAmount - model.AppliedAmount;
if (remainingBalance >= difference) {
var balanceCopy = model.BalanceCopy;
model.Balance = model.GrossSales - e.values.AppliedAmount;
model.BalanceCopy = model.GrossSales - e.values.AppliedAmount;
if (model.Balance == 0 && model.IsUncollected) {
model.IsUncollected = false;
context.UpdateBalance(balanceCopy, 0, true);
}
context.UpdateBalance(model.AppliedAmount, e.values.AppliedAmount, true);
this.refresh();
context.showButtons();
}
else {
alert("The applied amount exceeds the remaining balance");
e.preventDefault();
}
}
}, editable:"incell"});
This seems to be only happening when you edit the applied amount and hit enter to commit. Any idea of why this is happening?
The solution is fairly simple: Do not use an alert.
I do have the same problem in some events in kendoUI Widgets.
If you need to display something to the user, use kendoWindow with it's modal property set to true.
Alerts should only be used for debugging purposes, like console.log.

How to get selected row data of kendo detail grid

I am able to get the selected row in kendo grid ( master grid), But I am unable to get the selected row data in detail grid. Please provide me a code sample.
Thanks,
abhi
It is like for the main grid. Being childgrid the grid corresponding to details, do:
var row = childgrid.select();
var data = childgrid.dataItem(row);
console.log("row", row);
console.log("data", data);
Where I defined master grid as:
$("#grid").kendoGrid({
...
detailInit: detailInit,
...
});
And details grid is created using the following function when a row in master grid is expanded:
function detailInit(e) {
childgrid = $("<div/>").appendTo(e.detailCell).kendoGrid({
dataSource: {
type: "odata",
transport: {
read: "http://demos.kendoui.com/service/Northwind.svc/Orders"
},
serverPaging: true,
serverSorting: true,
serverFiltering: true,
pageSize: 5,
filter: { field: "EmployeeID", operator: "eq", value: e.data.EmployeeID }
},
scrollable: false,
sortable: false,
selectable: true,
pageable: true,
columns:
[
{ field: "OrderID", width: "70px" },
{ field: "ShipCountry", title: "Ship Country", width: "110px" },
{ field: "ShipAddress", title: "Ship Address" },
{ field: "ShipName", title: "Ship Name", width: "200px" }
]
}).data("kendoGrid");
}
Running example here : http://jsfiddle.net/OnaBai/2M86L/ (When you click on Show button, its displays in the console of your browser the selected row and its data).
Here a somewhat simpler example on how to get to the data of the clicked row: http://jsfiddle.net/Corne/AQqMH/5/
This is the code where the magic happens:
change: function (arg) {
var selectedData = this.dataItem(this.select());
// selectedData now points to the selected dataSource item!
alert("Clicked id: " + selectedData.id);
}

Resources