Responsive Grid help

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?



10 Replies 1 reply marked as answer

PS Pavithra Subramaniyam Syncfusion Team February 7, 2022 12:08 PM UTC

Hi Eddie, 
 
Thanks for contacting Syncfusion support. 
 
You can show the Grid rows vertically based on the device by using the “rowRenderingMode” property. The vertical row rendering mode supports only the Dialog editing. Show while rendering in desktop you can set the editSettings.mode as “Normal” otherwise it should be “Dialog”. Please refer to the below code example and API link for more information. 
 
// 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'; 
        } 
    } 
 
 
Please get back to us if you need further assistance on this 
 
Regards, 
Pavithra S 



EW Eddie Willcox replied to Pavithra Subramaniyam February 8, 2022 07:34 AM UTC

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>



JC Joseph Christ Nithin Issack Syncfusion Team February 10, 2022 05:51 AM UTC

Hi Eddie, 

  Thanks for your update. 

   Based on your requirement, you want to render the grid rows vertically. Your requirement can be achieved by enabling the `enableAdaptiveUI` property of the EJ2 grid and then set the `rowRenderingMode` property of the EJ2 grid as `Vertical` and add the `e-bigger` class to the parent container in the `load` event of the EJ2 grid.  

Please refer the below code example. 


 
 
<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> 

Please get back to us for further details. 

Regards, 
Joseph I. 



EW Eddie Willcox replied to Joseph Christ Nithin Issack February 10, 2022 07:23 AM UTC

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.



PS Pavithra Subramaniyam Syncfusion Team February 11, 2022 08:51 AM UTC

Hi Eddie, 
 
You can set the column alignment for mobile browser by setting the column.textAlign Property inside the load event of Grid component. Please refer to the blow code example for more information. 
 
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"; 
                    } 
                }) 
        }  
    }  
 
 
Please get back to us if you need further assistance on this. 
 
Regards, 
Pavithra S 



EW Eddie Willcox replied to Pavithra Subramaniyam February 11, 2022 09:43 AM UTC

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



PS Pavithra Subramaniyam Syncfusion Team February 14, 2022 08:02 AM UTC

Hi Eddie, 
 
The “Total” displayed in summary cell with mobile mode might be the header text of the “LineTotal” column. You can remove this text inside the “dataBound’ event of Grid component as below. 
 
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'''); 
    } 
  } 
} 
 
 
 
Regards, 
Pavithra S 


Marked as answer

EW Eddie Willcox February 14, 2022 08:23 AM UTC

Perfect. Thank you so much



EW Eddie Willcox replied to Pavithra Subramaniyam February 14, 2022 08:32 AM UTC

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.



PS Pavithra Subramaniyam Syncfusion Team February 15, 2022 01:16 PM UTC

Hi Eddie, 
 
We have tried to reproduce the issue at our side, but the summary cell text is working fine as expected. Since we customize the cells inside the dataBound event it will work for grid refresh too. So could you please share the issue reproducible sample or hosted link for validate the scenario and provide you a solution as early as possible. 
 
Regards, 
Pavithra S 


Loader.
Up arrow icon