This is a migrated thread and some comments may be shown as answers.

Dynamically setting PageSize based on Grid Height

7 Answers 1787 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Veteran
Michael asked on 16 Apr 2020, 05:50 AM

Marin:

I'm able to size the grid based on the Viewport height, but having done that I want to set the Grid PageSize property so that the number of rows per page roughly consumes the available vertical space-- in other words I'm trying to avoid the vertical scroll bars appearing on the grid because it's displaying more rows than will fit in visible grid height.  Presumably something like GridHeight / GridRowHeight would equal the optimal PageSize, but my experiments trying to get the RowHeight didn't work.  Can you provide an example of how to do it? 

BONUS Question:  How can I embed line returns in a string that will be displayed as a line break in the grid? ('\n' doesn't work for me)

 

<div style="margin: 0;padding: 0;border-width: 0; height: 95vh;">
 
    <TelerikGrid Data="@MyData" Height="100%"
                 Pageable="true"
                 Resizable="true">
        <GridColumns>
            <GridColumn Field="@(nameof(SampleData.Id))" Width="120px" />
            <GridColumn Field="@(nameof(SampleData.Name))" Title="Employee Name" Groupable="false" />
            <GridColumn Field="@(nameof(SampleData.Team))" Title="Team" />
            <GridColumn Field="@(nameof(SampleData.HireDate))" Title="Hire Date" />
        </GridColumns>
    </TelerikGrid>
</div>
 
@code {
 
    public IEnumerable<SampleData> MyData = Enumerable.Range(1, 200).Select(x => new SampleData
    {
        Id = x,
        Name = "Name " + x + "\nNext Line",
        Team = "team " + x % 5,
        HireDate = DateTime.Now.AddDays(-x).Date
    });
 
    public class SampleData
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Team { get; set; }
        public DateTime HireDate { get; set; }
    }
}

 

7 Answers, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 16 Apr 2020, 07:58 AM

Hello Michael,

You will need JS Interop to hook to the window.resize event and consume that handler in the component where the grid is declared. There are libraries that can help you with that (like this one). Then, when you have the actual size of the viewport, and the actual size of the grid, you can perform the desired calculation in the event handler, and set the PageSize parameter of the grid (you will, of course, have to use a field in the view-model for that).

If you create such a sample, I'd encourage you to open a pull request in this repo and post it, we award such contributions with Telerik points: https://github.com/telerik/blazor-ui

On line breaks - you need to initialize a MarkupString and include and HTML tag that can produce the desired result (such as a <br /> tag). The \n symbol applies to C#, but the browser renders HTML.

 

Regards,
Marin Bratanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Michael
Top achievements
Rank 1
Veteran
answered on 16 Apr 2020, 04:15 PM

Let me rephrase my question... is there a way to determine the current Row Height of the grid?  I get that you can supply a RowHeight and it will use that value, but if I'm not defining RowHeight how can I find out what it is?

 

0
Marin Bratanov
Telerik team
answered on 17 Apr 2020, 06:51 AM

Hi Michael,

You need JS Interop for that too. The grid is an HTML <table> and so the browsers determine a lot in its rendering - for example, when you set the RowHeight parameter, we set it as an inline style attribute to the row, but if the contents of a certain cell (say, long text, or a large image) are taller than that, the browser will ignore the explicit height we set and make the row taller. If you define a RowHeight that can accommodate all your content without stretching the rows, you can rely on using that in your calculations. Otherwise, you will need to use JS Interop to get the actual rendered height of the HTML elements.

 

Regards,
Marin Bratanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
1
Michael
Top achievements
Rank 1
Veteran
answered on 17 Apr 2020, 06:59 AM

Thanks-- I was hoping the grid might expose an easier way to get it (maybe a future feature?).  From looking at the javascript examples it looks like more trouble than it's worth. 

I am using Ed's BlazorSize package by the way.   When I have a chance I'll try to put together an example of all the tips and tricks I've learned-- mostly thanks to you-- in case it can help others getting started with the grid.

0
Marin Bratanov
Telerik team
answered on 17 Apr 2020, 07:05 AM

Hello Michael,

I do not expect the grid to expose such a feature. It provides a lot of settings so you can customize its behavior, and it expects that the developer sets them according to the application logic. Using JS Interop to get the DOM elements size will be the way until Microsoft provide a built-in way to do that in Blazor (If that happens). JS Interop is a valid thing in Blazor, even if the framework aims at enabling a lot of C# for many tasks that required JS.

On examples of what you did and learned, feel free to post them. Maybe some of them would be interesting scenarios that can go into this repo (for example, if you implement the page size change with the viewport change approach).

Regards,
Marin Bratanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
2
Michael
Top achievements
Rank 1
Veteran
answered on 26 Apr 2020, 04:48 AM

Marin:

I have created a sample project to demonstrate a number of things I have learned about in working with the Telerik Blazor Grid including how to dynamically set the grid height, responsively add or subtract columns based on media breakpoints, selecting a page and row and how to handle some formatting issues.

Telerik Blazor Grid Demo

None of this is rocket science, but it might be useful for others getting started.

I know you suggested adding it to the existing telerik/blazor-ui repository, and I would if I knew how, but I'm afraid that doing pull requests is not in my toolkit.  You are welcome to add it to the Telerik Blazor repository if you think it's worthy.  

Thanks for your continued help.

Twain
Top achievements
Rank 2
Iron
Iron
Iron
commented on 04 Jan 2023, 08:10 PM

Thanks Michael. 
What you are sharing is very helpful. I will use it as a guide for my own implementation.

Twain.
0
Marin Bratanov
Telerik team
answered on 27 Apr 2020, 02:55 PM

Hello Michael,

I added the project to the following URL: https://github.com/telerik/blazor-ui/tree/master/grid/adjust-height-with-browser. You will find your Telerik points updated.

 

Regards,
Marin Bratanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Veteran
Answers by
Marin Bratanov
Telerik team
Michael
Top achievements
Rank 1
Veteran
Share this question
or