Showing posts with label kendo menu. Show all posts
Showing posts with label kendo menu. Show all posts

Sunday, March 28, 2021

Kendo Menu and Grid Example

This article is an example of the Kendo Menu combined with the Kendo Grid. The framework used is the Kendo UI for jQuery. In this example, we are tasked to create a menu that disables its items depending on the selected row in the grid. It's is something like, you can't work on a row if you own it. Hence, the owner's name is disabled in the menu item.

Here is our solution. You can edit the code below using any text editor but I prefer Visual Studio Code. Make sure you are connected to the Internet because the code pulls the Kendo library from the Telerik CDN. Take note that this is just a demonstration code and you'll need to purchase a license if you are going to use Kendo UI for jQuery for more than just evaluation purposes.

Open the HTML file in a browser and you should see an output similar to the one shown below.

Kendo Menu and Grid
Kendo Menu and Grid Example

There you have it. A quick and simple example of how to use the Kendo Menu combined with Kendo Grid.

Saturday, March 6, 2021

Kendo Menu With Spring Boot Example

This article is an example of the Kendo Menu Component with a Spring Boot backend. The framework used is the Kendo UI for jQuery. In this example, let us imagine that we need to build a menu with an icon. Sound simple? Let's make it a bit harder. The menu and icon are dynamic. In this example we'll just hardcode the menu data source but in the real world, the data for the menu is provided by a web API. So consider that our menu can have more or less menu items or sub menu items depending on the logged in user. An additional complexity is that our menu items have dynamic icons which we need to pull from the web API. This is the focus of this example, dynamic icons for the menu items.

The IDE used is Spring Tool Suite 4 and it is expected that the reader know a thing or two about Spring Boot. Make sure you are connected to the Internet because the code pulls the Kendo library from the Telerik CDN. Take note that this is just a demonstration code and you'll need to purchase a license if you are going to
use Kendo UI for jQuery for more than just evaluation purposes.

So here we go. One solution could be to pull all the icons when we get the menu data. Possible but might take longer to build the menu. And as we all know users hate to wait. So we only fetch the icon when the menu item is about to be shown and not invoke the web API call again if we already have the icon.

This is our icon web API endpoint.

package com.blogspot.jpllosa.controller;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ImageController {

    @GetMapping(
        value = "/get-image",
        produces = MediaType.IMAGE_JPEG_VALUE)
    public @ResponseBody byte[] getImage() throws IOException {
        InputStream in = getClass().getResourceAsStream("/java.png");
        return IOUtils.toByteArray(in);
    }
}

Here is our frontend code.

<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Kendo Menu with Spring Boot</title>
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2021.1.119/styles/kendo.default-v2.min.css"/>
    
    <script src="http://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2021.1.119/js/kendo.all.min.js"></script>
    
<style>
img.k-image {
    height: 2rem;
}
</style>

</head>
<body>
    <ul id="menu"></ul>

<script>
$(document).ready(function() {
    $("#menu").kendoMenu({
        animation: false,
        dataSource: {
            data: [{
                text: "Menu",
                items: [{
                    text: "Sub Menu",
                    items: [{
                        text: "Java",
                        imageUrl: "#",
                    }],
                }],
            }],
        },
        open: function(e) {
            var $img = $(e.item).find("span img.k-image")
            var src = $img.attr("src");
            
            if (src) {
                if (src === "#") {
                    $.ajax({
                        type: "GET",
                        url: "/get-image",
                        xhrFields: {
                            responseType: "blob",
                        },
                        cache: false,
                    })
                    .done(function(data, textStatus, jqXhr) {
                        var imageUrl = URL.createObjectURL(data);
                        $img.attr("src", imageUrl);
                    });
                }
            }
        },
    });
});
</script>
</body>
</html>

The output looks like this:

Kendo Menu with Spring Boot
Kendo Menu with Spring Boot


There you have it. I hope you found this example educational. The complete project can be cloned from github.com/jpllosa/spring-boot-kendo-menu

Sunday, January 31, 2021

Kendo Menu With DropDownList Example

This article is an example of the Kendo Menu and DropDownList Component. The framework used is the Kendo UI for jQuery. In this example, we are tasked to create a menu that has a drop down list option.
Here are the requirements:

  1. A search menu.
  2. The menu contains two type of searches. Simple and advance.
  3. The advance search has a type option list.

Here is our solution. You can edit the code below using any text editor but I prefer Visual Studio Code. Make sure you are connected to the Internet because the code pulls the Kendo library from the Telerik CDN. Take note that this is just a demonstration code and you'll need to purchase a license if you are going to use Kendo UI for jQuery for more than just evaluation purposes.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Kendo UI Menu with DropDownList Example</title>

    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2020.2.617/styles/kendo.default-v2.min.css"/>

    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2020.2.617/js/kendo.all.min.js"></script>
</head>
<body>
  
  <ul id="menu">
    <li>Search
      <ul>
        <li>
          <div style="display: flex">
            <input placeholder="Simple search" type="text" class="k-textbox">&nbsp;
            <button class="k-primary" title="Execute the search">
              <span class="k-icon k-i-search"></span>
            </button>
          </div>
        </li>
        <li class="k-separator"></li>
        <li>
          <div>
            <input id="searchTypes"></br>
            <div style="display: flex">
              <input placeholder="Advance search" type="text" class="k-textbox">&nbsp;
              <button title="Execute the search">
                <span class="k-icon k-i-search"></span>
              </button>
            </div>
          </div>
        </li>
      </ul>
    </li>
  </ul>
<script>
$(document).ready(function() {
  $("#menu").kendoMenu({
    animation: false,
  });

  $("#searchTypes").kendoDropDownList({
    dataSource: {
      data: ["Type 1", "Type 2", "Type 3", "Type 4"],
    },
    open: function(e) {
      $("#menu").find("ul.k-group.k-menu-group")
        .css("overflow", "visible");
    }
  });
});
</script>
</body>
</html>

Open the HTML file in a browser and you should see an output similar to the one shown below.

Kendo Menu with DropDownList
Kendo Menu with DropDownList


If we don't handle the CSS in the open event then it would look like this. Go ahead try it.

Kendo Menu with DropDownList not fully visible
Kendo Menu with DropDownList not fully visible

 

When we handle the CSS in the open event then it will look the one below.

Kendo Menu with visible DropDownList

There you have it. A quick and simple example of how to use the Kendo Menu with DropDownList component.