Monday, September 8, 2014

Kendo UI's ListView for AngularJS doesn't work

File this under "what a waste of time".

So I was trying to get Kendo UI's ListView to work with AngularJS. But it doesn't render. So I assume its my fault and tried for hours to get it to work. Stepping through Javascript (not the most pleasant of tasks), searching Stackoverflow (not the most pleasant of tasks).

Guess what was the problem?

In the Kendo UI AngularJS example above, the Kendo UI directive was kendo-listview. That doesn't work with my version of Kendo UI.

It should be kendo-list-view.

Wow, what a waste of my time.

Friday, September 5, 2014

How to upload file data using Spring MVC, Angular JS and Kendo UI

Uploading a file in Spring is one of those super important things to know about, which, for some reason, none of my past projects required me to do. And suddenly I have to asynchronously upload a file using Kendo UI, AngularJS and Spring MVC. The widget I use in Kendo UI is "Upload" Here are the steps we need to do.

  1. Enable Spring’s built-in multipart handling.
  2. Create a method in the Spring controller that will handle the multipart POST.
  3. Create the Kendo UI Upload widget.

1. Enable Spring’s built-in multipart handling
By default, Spring does no multipart handling. This is done by adding a "multipart resolver" bean. I chose to use a multipart resolver that uses Apache Commons FileUpload package (which is mature and robust). First include the package. I use Maven. So do it like this:

Then I add the multipart resolver into the Spring configuration:
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
(the id attribute was not needed in my case)

2. Create a method in the Spring controller that will handle the multipart POST.
This method will only accept POST calls. It will have a MultiPart parameter, which contains (or contains information about) the file data. All the temporary file data will be cleared at the end of request processing.
@RequestMapping(value = "uploadFile", method = RequestMethod.POST)
public Map<String, String> uploadImage( @RequestParam("file") MultipartFile file, 
                                                     @RequestParam("username") String userName {

    InputStream inputStream = null;
    try {
        inputStream = file.getInputStream();
        // do something with inputStream here....
        inputStream = null;

    } finally {
        if (inputStream != null)
        inputStream = null;

    String extension = FilenameUtils.getExtension( file.getOriginalFilename() );

    final HashMap<String, String> ret = new HashMap<String, String>();
    ret.put( "someInfo", "foobar from uploadImage" );
    return ret;
Some interesting things happen here:

  • I also take in another @RequestParam. This is pretty typical of a file upload: to pass more information about the file to the server. Kendo UI's Upload widget's async upload will require a little tweaking to pass in additional information. More on that later.
  • I used MultipartFile::getInputStream() and MultipartFile::getOriginalFile(). These are typical usages of MultipartFile. The entire API is here
  • I also use Apache Commons awesome FilenameUtils and other Commons IO classes. In Java 6 and below, it's still the best way to do IO. When using Commons IO, remember to check if the method closes the stream/file. If not, you have to do it yourself. There are sometimes 2 similar methods to do something: 1 that closes the stream/file, and 1 that doesn't. Do yourself a favor. Use the first one.
  • Finally, I also return a Map. This will translate to an Object when returned back to AngularJS. Not all async uploads require return data. But if you do, here's how you do it. More on that later.

3. Create the Kendo UI Upload widget
Let's take a look at the widget:
<input kendo-upload
       k-async="{ saveUrl: 'uploadFile', autoUpload: true }"
In my case:

  • I set k-multiple to false.
  • I also don't want to show the file list
  • k-async contains the URL, and the setting to auto upload the file once the user selects it.
Check out all the configuration on Kendo UI's API page.

How do we pass in the username? That's where k-upload comes in. The API states that the k-upload event "fires when one or more files are about to be uploaded". Let's take a look at the AngularJS code:
$scope.addMorePostParameters = function (e) { = {username: "gerard"};
The API also says that there is a data object "that will be sent to the save handler in the form of key/value pairs". So we use that to pass in the username String together with the file upload.

How do we access the return value (if any) which the server returned? k-success will do that for you.
$scope.onSuccess = function (e) {
    var someInfo = e.response.someInfo;
e.response.someInfo will contain the string "foobar from uploadImage".

I hope that helps.

Programmatically scroll to selected row in Kendo UI Grid widget (using AngularJS)

I had successfully selected a row programmatically in Kendo UI - AngularJS. But because I usually refreshed the data for the Kendo UI Grid, occasionally the selected row would be "scrolled out". So yes, the row was selected, but the user still needed to scroll down to see it.

This was done by using jQuery's scrollTop() method.

  1. First thing we need to do is somehow to get a reference to the Kendo's Grid widget. 
  2. Then we need to get the vertical distance between the Grid "content area" and the selected row.
  3. Finally we call scrollTop() to scroll the grid the above distance.

1. Get a reference to the Kendo's Grid widget
This is very simple.
<div kendo-grid="theGrid"

Simply specify the variable name in the kendo-grid attribute. Then a simple:
will reference the Kendo widget. Simply simple.

2. get the vertical distance between the Grid "content area" and the selected row
This is done using the offset() jQuery method of the Grid and the selected row. Obtaining the jQuery object of the selected row is outside the scope of this article. I talked about it in a previous post, so you can go take a look. It looks like this:
var row = $('[data-uid=' + item.uid + ']');

Getting the vertical offset of the row from the top of the document then looks like this:

Now for the vertical offset of the Grid's content area. Looking at the Kendo Grid API, I see that there is a content property "which represents the grid content element, which holds the scrollable content". That's what we want. The vertical offset of the "Grid content element" is then:

And the vertical difference is just the difference:
row.offset().top - $scope.theGrid.content.offset().top

3. call scrollTop()
Nothing special here. Except if you do a lot of async calls to populate the Grid content. If so, you might find that the Grid content has already been scrolled down. So to be safe, we scroll the Grid content all the way up, then we scroll it back down to the selected item.
var row = $('[data-uid=' + item.uid + ']');

$scope.theGrid.content.scrollTop(0); // we HAVE to reset the scroll first.
$scope.theGrid.content.scrollTop( row.offset().top - $scope.theGrid.content.offset().top );

Surely someday Kendo - AngularJS will have a method to do this without resorting to "behind the back" jQuery code. Sooner or later, we'll be free, to leave this all behind. Sooner or later, this is gonna be, the last thing on my mind.

The jQuery docs for scrollTop() and offset() are here.