|
function onDataBound() {
if (this.dataSource.length < 20) {
var virtualTable = this.element.querySelector('.e-content .e-virtualtable');
virtualTable.style.minHeight = (Number(virtualTable.style.minHeight.replace("px", "")) - 18).toString() + "px";
}
} |
|
setTimeout(function () {
grid.columns = [];
grid.columnModel = [];
grid.dataSource = data(500000);
}, 300); |
|
// Grid’s dataBound event function
function onDataBound() {
const virtualTable = this.element.querySelector('.e-content .e-virtualtable');
const normaltable = this.element.querySelector('.e-content .e-table');
// Virtualization is disabled based on the virtual table’s height
if (virtualTable && virtualTable.offsetHeight <= virtualTable.offsetParent.offsetHeight) {
this.enableVirtualization = false;
this.enableColumnVirtualization = false;
}
// Virtualization is enabled based on the normal table’s height
else if (normaltable && !virtualTable && normaltable.offsetHeight > normaltable.offsetParent.offsetHeight) {
this.enableVirtualization = true;
this.enableColumnVirtualization = true;
}
} |
|
function onDataBound() {
if (newV && !grid.dataSource.length) {
grid.getColumns().map(m => m.width = 'auto');
const columns = grid.getColumns().map(m => m.field);
console.log(columns);
grid.hideColumns(columns, 'field');
}
} |
|
function onDataBound() {
console.log("databound");
console.log("newV: " + newV);
if (columnChooser) {
columnChooser = false;
setTimeout(function () {
grid.openColumnChooser();
}, 300);
}
}
function actionComplete(args) {
if (args.requestType === "refresh" && newV) {
grid.dataSource = data(10000);
columnChooser = true;
newV = false;
}
} |
Now, on a session/cookie timeout, a 200 OK is returned: (the server is running ASP.NET using MS OWin (3.0.1) for authentication , as documented here , using this configuration: (by now , I changed it to return the status code from the json as the actual HTTP response code too)
app.UseCookieAuthentication(new CookieAuthenticationOptions //// ( This is from Startup.Auth.cs in void ConfigureAuth(IAppBuilder) )
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider(), /// < = this is responsible for returning 200 instead of 401 for AJAX requests (like below) by default
ExpireTimeSpan = TimeSpan.FromMinutes(20),
SlidingExpiration = true,
CookieHttpOnly = true,
});
)
//// ( This is from Startup.Auth.cs in void ConfigureAuth(IAppBuilder) )
var cookieAuthenticationProvider = new CookieAuthenticationProvider();
var defaultRedirect = cookieAuthenticationProvider.OnApplyRedirect;
cookieAuthenticationProvider.OnApplyRedirect = context =>
{
int originalResponseCode = context.Response.StatusCode;
defaultRedirect(context);
// Override default behavior which sets response code for AJAX requests to 200, which caused client code to interpret it as success
if (context.Response.StatusCode == (int) HttpStatusCode.OK)
context.Response.StatusCode = originalResponseCode;
};
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = cookieAuthenticationProvider,
ExpireTimeSpan = TimeSpan.FromMinutes(20),
SlidingExpiration = true,
CookieHttpOnly = true,
});
Response: (this will make the AJAX component call the failure handler and I will get control in the Grid.actionFailure(args) handler with access to the XHR in args.error.error to handle the error with custom code )
|
<div><span>Column chooser</span> <button id='btnapply' style='margin-left: 10%'>Apply</button></div>
<input id="multi-select-listbox" style='width: 200px' /> |
|
// Initialize the ListBox component.
var listObj = new ej.dropdowns.ListBox({
// Grid columns are set as the list data source
dataSource: grid.getColumnFieldNames(),
// Set the selection settings with showCheckbox as enabled.
selectionSettings: { showCheckbox: true },
created: function () {
// All the items are selected by default
this.selectAll();
}
});
listObj.appendTo('#multi-select-listbox');
var button = new ej.buttons.Button({ isPrimary: true });
button.appendTo('#btnapply');
document.getElementById('btnapply').addEventListener('click', function (args) {
// Entire list items
var listItems = listObj.getItems();
// Selected list items
var selectedItems = listObj.getSelectedItems();
var i = 0;
while (i < listItems.length) {
var count = 0;
// Item name(column name) is retrieved from the list element
var itemName = listItems[i].getAttribute("data-value");
// List items are compared with selected item
selectedItems.forEach(item => count = item.getAttribute("data-value") === listItems[i].getAttribute("data-value") ? count + 1 : count)
// Grid column model is retrieved using the item name(column name)
var gridColumn = grid.getColumnByField(itemName);
// If list item is not selected the column visibility is set as false and vice-versa
(count === 0) ? gridColumn.visible = false : gridColumn.visible = true;
i++;
}
grid.refreshColumns();
}) |
|
// Grid’s dataBound event function
function onDataBound() {
if (flag) {
var treeData = [];
Object.assign(treeData, grid.columns);
treeData.forEach(x => x.gridHeader = x.headerText);
treeObj.fields = { dataSource: treeData, id: 'uid', child: 'columns', text: 'gridHeader', expanded: 'visible', isChecked: 'visible', hasChildren: 'columns' };
flag = false;
}
} |
|
document.getElementById('btnapply').addEventListener('click', function(args){
// TreeView’s entire data source
var treeItems = treeObj.fields.dataSource;
// TreeView’s checked nodes
var checkedItems = treeObj.checkedNodes;
var treeFields = [];
// Field data for the checked items
checkedItems.forEach(id => treeFields.push(treeObj.getNodeObject(id)));
var i = 0;
while (i < treeItems.length) {
var count = 0;
var name = treeItems[i].gridHeader;
// Checked field data is compared with each Tree node
treeFields.forEach( item => count = item.gridHeader === name ? count+1 : count )
var gridColumn = grid.getColumnByField(treeItems[i].field);
if (count === 0) {
grid.hideColumns(gridColumn.headerText);
} else if(count !== 0 && !gridColumn.visible) {
grid.showColumns(gridColumn.headerText);
}
i++;
}
}) |
|
document.getElementById('btnapply').addEventListener('click', function(args){
.
.
var gridColumn = grid.getColumnByField(treeItems[i].field);
if (count === 0 && gridColumn.visible) {
grid.hideColumns(gridColumn.headerText);
} else if(count !== 0 && !gridColumn.visible) {
grid.showColumns(gridColumn.headerText);
}
i++;
}
})
|
|
// Web api adaptor is overridden
class CustomAdaptor extends ej.data.WebApiAdaptor {
processQuery() {
// calling base class processQuery function
var original = super.processQuery.apply(this, arguments);
// Here you can modify the queries in the request URL as per your requirement and return it
return original;
}
}
var data = new ej.data.DataManager({
url: hostUrl + 'api/Orders',
adaptor: new CustomAdaptor()
});
var grid = new ej.grids.Grid({
dataSource: data.
.
.
});
grid.appendTo('#Grid'); |