New to Telerik UI for ASP.NET CoreStart a free 30-day trial

Selecting MultiSelect Items When Holding Shift Button

Environment

ProductProgress® Telerik® UI MultiSelect for UI for ASP.NET Core

Description

How can I select one item in the MultiSelect and then, if the shift button is held down, select another item and all items between them?

Solution

Razor
.Events(ev=>ev.Select("onSelect"))
  • Initialize a variable, the value of which will indicate that the shift button is down.
Razor
var isShiftDown = false;
        window.onmousemove = function (e) {
            if (e.shiftKey) {
                isShiftDown = true;
            }
            else {
                isShiftDown = false;
            }
        }
  • Initialize two variables that hold as value the indexes of the selected elements. After the first select get the index of the first element. If the shift button is held down get the offset-index for the second element.

  • Iterate through all the elements between those indexes and push their values into an array.

  • Check if the first index is smaller than the second one, if not change their values.

Razor
  if (firstIndex > secondIndex) { 
        var temp = firstIndex;
        firstIndex = secondIndex
        secondIndex = temp;
        // check which index is bigger (first should be smaller)
    }
  • Use the value() method to set the new value of the MultiSelect as you pass the temporary array as a parameter.

Example:

Razor
@(Html.Kendo().MultiSelect()
          .Name("multiselect2")
          .Placeholder("Select attendees...")
          .BindTo(new List<string>() {
                    "Andrew Suyama",
                    "Nige Buchanan",
                    "Laura Fuller",
                    "Laura Fuller2",
                    "Laura Fuller3"
            })
          .Events(ev=>ev.Select("onSelect"))
          .AutoClose(false)
          .Value(new string[] { "Anne King", "Andrew Fuller" }
          )
        )

More ASP.NET Core MultiSelect Resources

See Also