Unplanned
Last Updated: 11 Mar 2020 12:03 by ADMIN
Datafyer
Created on: 23 Oct 2019 19:11
Category: Button
Type: Feature Request
12
Split button

In some cases I have need of a button with additional options.
These options are typically put in a drop down associated to the button.

A split button makes this natural.
In this mode the user can click on the button itself or expand the dropdown and click on one of the options inside.

1 comment
ADMIN
Marin Bratanov
Posted on: 11 Mar 2020 12:03

Hello,

Here's an example of how you can achieve a split button with the existing components (a button for the main click and a dropdown for choosing options) and how to handle both events (changed command choice from the dropdown, and the click on the main button). It also includes a few bits of CSS to put the components closer together so they look more like one individual item.

<style>
    .split-button-container .k-button {
        border-right: 0;
        border-top-left-radius: 0;
        border-bottom-left-radius: 0;
    }

    .split-button-container .k-dropdown,
    .split-button-container .k-dropdown-wrap {
        border-left: 0;
        margin-left: -3px;
        border-top-left-radius: 0;
        border-bottom-left-radius: 0;
    }
</style>

    <div class="split-button-container">
        <TelerikButton Icon="@IconName.Star" OnClick="@SplitButtonClick">@GetSelectedActionText()</TelerikButton>
        <TelerikDropDownList Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField"
                             Width="40px" PopupWidth="auto"
                             Value="@selectedValue" ValueChanged="@( (int v) => MyValueChangeHandler(v) )">
            <ValueTemplate>
                <span></span>
            </ValueTemplate>
        </TelerikDropDownList>
    </div>

@code {
    int selectedValue { get; set; }

    async Task SplitButtonClick()
    {
        Console.WriteLine($"split button clicked, selected value is: {selectedValue}, the selected text is: {GetSelectedActionText()}");
    }

    async Task MyValueChangeHandler(int theUserChoice)
    {
        Console.WriteLine($"Dropdown Changed: the user chose: {theUserChoice}");
        selectedValue = theUserChoice;
    }

    string GetSelectedActionText()
    {
        return myDdlData.Where(itm => itm.MyValueField == selectedValue).FirstOrDefault().MyTextField;
    }

    public class MyDdlModel
    {
        public int MyValueField { get; set; }
        public string MyTextField { get; set; }
        public string ExtraField { get; set; }
    }

    IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x =>
            new MyDdlModel
            {
                MyTextField = "item " + x,
                MyValueField = x,
                ExtraField = "more item info " + x
            }
        );
}

 

Regards,
Marin Bratanov
Progress Telerik

 UI for Blazor