Unplanned
Last Updated: 31 Jul 2024 23:50 by Leland
Doug
Created on: 16 Aug 2022 14:15
Category: Form
Type: Feature Request
14
Add the ability to define form columns with different widths

Currently, you can split the TelerikForm into multiple columns via the Columns parameter. These columns will have equal widths. I would like to have the ability to define custom width per column. 

<AdminEdit>

You can use the following code snippet as a workaround with some considerations:

  • The k-grid-cols-2 class is a dynamic one. The "2" segment maps to the number of the columns defined via the Columns parameter exposed for the <TelerikForm>
  • If there are more/less than two columns, you should change the values for the grid-template-columns accordingly.

<style>
    .non-equal-columns .k-form-layout.k-grid-cols-2 {
        grid-template-columns: 71% 28.5%;
    }
</style>

@using System.ComponentModel.DataAnnotations

<TelerikForm Model="@person" ColumnSpacing="25px" Columns="2" Class="non-equal-columns">
    <FormValidation>
        <DataAnnotationsValidator></DataAnnotationsValidator>
    </FormValidation>
    <FormItems>
        <FormGroup LabelText="Personal Information" ColumnSpacing="15px">
            <FormItem LabelText="First Name" Field="@nameof(Person.FirstName)"></FormItem>
            <FormItem LabelText="Last Name" Field="@nameof(Person.LastName)"></FormItem>
            <FormItem LabelText="Age" Field="@nameof(Person.Age)"></FormItem>
            <FormItem LabelText="Email" Field="@nameof(Person.Email)"></FormItem>
        </FormGroup>
        <FormGroup LabelText="Employee Information" Columns="2" ColumnSpacing="15px">
            <FormItem LabelText="Company Name" Field="@nameof(Person.CompanyName)"></FormItem>
            <FormItem LabelText="Position" Field="@nameof(Person.Position)"></FormItem>
        </FormGroup>
    </FormItems>
</TelerikForm>

@code {
    public Person person { get; set; } = new Person();

    public class Person
    {
        [Required(ErrorMessage = "The First name is required")]
        public string FirstName { get; set; }
        [Required(ErrorMessage = "The Last name is required")]
        public string LastName { get; set; }
        [Range(18, 120, ErrorMessage = "The age should be between 18 and 120")]
        public int Age { get; set; }
        [Required]
        [EmailAddress(ErrorMessage = "Enter a valid email")]
        public string Email { get; set; }
        [Required]
        public string CompanyName { get; set; }
        [MaxLength(25, ErrorMessage = "The position can be maximum 25 characters long")]
        public string Position { get; set; }
    }
}

</AdminEdit>

2 comments
Leland
Posted on: 31 Jul 2024 23:50
My Form had more FormGroups and columns and I was running into issues where FormItems in the last column of the Form were going out of the Form's bounds.  If you changed the width of the window, that would alter how far off that last column would go.  After trying things out for a while, I realized that the `ColumnSpacing` takes up part of the total available width, so the final percentage was over 100%.  Maybe that's why the example didn't add up to 100% without any explanation as to why.

What this means is that you need to stick with % units throughout.  Otherwise, depending on the width of your window, the form may be trying to take up more or less than the width of its container.  As the form layout gets more complicated, the math for handling this workaround gets more complex, so a built in way to define varying column widths would simplify things greatly.

@Alex you might try changing the units in `non-equal-columns` to px instead of % and see if that works for you.  I haven't explored how that works, as in my use case I actually do want the Form content to span the entire width of it's parent container.
Alex
Posted on: 20 Jun 2024 17:23
I would prefer that my input fields not span the entire width of the page, or a percentage of a page in some scenarios. It would be nice to be able to set pixel widths so that the input box can be sized to the appropriate length for the data field to which it is bound.