Hi,
I've been trying to get my grid to be responsive, e.g. if a mobile device views the grid, the grid must render rows vertically and if a desktop browser views the grid it must render horizontally.
I've been trying to follow the examples here: https://ej2.syncfusion.com/aspnetmvc/Grid/Adaptive#/tailwind
and here: https://ej2.syncfusion.com/aspnetmvc/documentation/grid/adaptive/
But I am unable to understand exactly how to switch between rendering modes based on mobile or desktop.
My grid is also set to normal editing mode. Could this be causing an issue with the adaptive UI display? If that's the case, can I then also switch between edit modes based on display device type?
|
// grid load event triggeres while grid initial loading
function load() {
var grid = document.getElementById('Grid').ej2_instances[0];
if (ej.base.Browser.isDevice) { // true for mobile device
grid.rowRenderingMode = 'Vertical';
grid.editSettings.mode = 'Dialog';
} else {
// for Desktop
grid.rowRenderingMode = 'Horizontal';
grid.editSettings.mode = 'Normal';
}
} |
Thanks for this, but my grid still renders as if it's a desktop browser on mobile. Please can you point out where I'm going wrong?
@Html.EJS().Grid("products").DataSource((IEnumerable<object>)ViewBag.products).EnableStickyHeader(true).AllowTextWrap(true).TextWrapSettings(text => { text.WrapMode(Syncfusion.EJ2.Grids.WrapMode.Header); }).Width("auto").Columns(col =>
{
col.Field("ProductCode").HeaderText("Code").HeaderText("Code").IsPrimaryKey(true).AutoFit(true).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Center).AllowSorting(true).Add();
col.Field("Description").HeaderText("Item").Width("400px").HeaderText("Item").HeaderTextAlign(Syncfusion.EJ2.Grids.TextAlign.Center).AllowEditing(false).AllowSorting(true).Add();
col.Field("Colour").HeaderText("Colour").AllowEditing(false).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Center).AllowSorting(false).Add();
col.Field("Price").HeaderText("UNIT PRICE EX VAT").Format("C2").Width("150px").AllowEditing(false).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Center).AllowSorting(false).Add();
col.Field("Pack").HeaderText("ORDER IN MULTIPLES OF").AllowEditing(false).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Center).AllowSorting(false).Add();
//col.Field("Barcode").HeaderText("Barcode").AllowEditing(false).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Center).Add();
col.HeaderText("Image").Template("#template").AllowEditing(false).Width("90px").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Center).Edit(new { create = "create", read = "read", destroy = "destroy", write = "write" }).AllowSorting(false).Add();
col.Field("QtyToOrder").HeaderText("Order").EditType("numericedit").ValidationRules(new { required = true }).Edit(new { @params = new Syncfusion.EJ2.Inputs.NumericTextBox() { Change = "changeFn", ShowSpinButton = false, Min = 0, ValidateDecimalOnType = true, Decimals = 0, Format = "N" } }).Width("100px").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Center).AllowSorting(false).Add();
col.Field("LineTotal").HeaderText("Total").AllowEditing(false).Format("C2").Width("220px").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).AllowSorting(false).Add();
}).Locale("en-ZA").GridLines(Syncfusion.EJ2.Grids.GridLine.Both).EnableAltRow(true).Height("400px").EditSettings(edit => { edit.AllowEditing(true).AllowAdding(false).AllowDeleting(false).ShowConfirmDialog(false); }).Toolbar(new List<string>() { "Search" }).Aggregates(agg =>
{
agg.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "LineTotal", Format = "C2", Type = "Sum", FooterTemplate = "Excl Total: ${Sum}" } }).Add();
agg.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "LineTotal", Format = "C2", Type = "Custom", FooterTemplate = "VAT: ${Custom}", CustomAggregate = "vatFn" } }).Add();
agg.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "LineTotal", Format = "C2", Type = "Custom", FooterTemplate = "Total: ${Custom}", CustomAggregate = "totalFn" } }).Add();
}).EnableVirtualization().ActionBegin("calcTotal").RecordClick("click").Height("400").AllowSorting(true).EnableAdaptiveUI(true).Load("mainLoad").Render()
<script>
function mainLoad() {
var grid = document.getElementById("products").ej2_instances[0];
if (ej.base.Browser.isDevice) {
grid.rowRenderingMode = "Vertical";
grid.editSettings.mode = "Dialog";
} else {
grid.rowRenderingMode = "Horizontal";
grid.editSettings.mode = "Normal";
}
}
</script>
|
<div class="e-adaptive-demo">
@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.datasource).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Filter(new { type = "CheckBox" }).Add();
col.Field("CustomerID").HeaderText("Customer Name").Add();
col.Field("OrderDate").HeaderText("Order Date").Format("M/d/y hh:mm a").EditType("datepickeredit").Add();
col.Field("Freight").HeaderText("Freight").Format("C2").Add();
col.Field("Verified").HeaderText("Verified").Add();
}).Height("400").AllowPaging().Toolbar(new List<string>
() { "Add", "Edit", "Delete", "Update", "Cancel" }
).AllowFiltering().FilterSettings(filter => filter.Type(Syncfusion.EJ2.Grids.FilterType.Menu)).EditSettings(edit => { edit.AllowEditing(true).AllowAdding(true).AllowDeleting(true); }
).EnableAdaptiveUI(true).Load("onLoad").Render()
</div>
<script type="text/javascript">
function onLoad() {
var gridObj = document.getElementById('Grid').ej2_instances[0]
if (ej.base.Browser.isDevice) {
document.querySelector('.e-adaptive-demo').classList.add('e-bigger');
gridObj.rowRenderingMode = 'Vertical';
gridObj.adaptiveDlgTarget = document.getElementsByClassName('e-mobile-content')[0];
gridObj.editSettings.mode = "Dialog";
}
else {
gridObj.rowRenderingMode = "Horizontal";
gridObj.editSettings.mode = "Normal";
}
}
</script> |
Thank you so much. This did the trick.
One last question:
Is it possible to set the text alignment in the grid for both headers and data rows to be left aligned instead of center aligned when it is a mobile browser?
Then center alignment is causing everything to render weirdly and it does not look so good.
|
function onLoad() {
var gridObj = document.getElementById('Grid').ej2_instances[0]
if (ej.base.Browser.isDevice) {
grid.columns.map((e)=>{
if(e.textAlign == "Center"){
e.textAlign ="Left";
}
})
}
else {
grid.columns.map((e)=>{
if(e.textAlign == "Left"){
e.textAlign ="Center";
}
})
}
}
|
Thank you. One last question. Is it possible to change text in the aggregate columns? This is what my aggregates look like in the code:
agg.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "LineTotal", Format = "C2", Type = "Sum", FooterTemplate = "Excl Total: ${Sum}" } }).Add();
agg.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "LineTotal", Format = "C2", Type = "Custom", FooterTemplate = "VAT: ${Custom}", CustomAggregate = "vatFn" } }).Add();
agg.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "LineTotal", Format = "C2", Type = "Custom", FooterTemplate = "Total: ${Custom}", CustomAggregate = "totalFn" } }).Add();
In desktop browser, the aggregates are displayed correctly as
"Excl Total: value"
"VAT: value"
"Total: value"
but in mobile mode it is displayed as:
"Total Excl Total: value"
"Total VAT: value"
"Total Total: value"
I need the first "Total " in the above to not display and only display the aggregates as I have set them
|
function dataBound() {
var grid = document.getElementById('Grid').ej2_instances[0];
if (ej.base.Browser.isDevice) {
let summaryCell = grid.element.querySelectorAll('.e-summarycell');
for (var i = 0; i < summaryCell.length; i++) {
summaryCell[i].setAttribute('data-cell', '');
}
}
}
|
Perfect. Thank you so much
I spoke too soon. If I do not edit any data, your solution works fine. As soon as I edit a row in mobile browser, the "TOTAL" text comes back exactly the way it was.