Implementing Master and Detail Grids in Wijmo

We recently pushed an update to our WijMarket sample, implementing an example of implementing Master and Detail grids in Wijmo. You can check out the sample here. Also, I recommend doing a View Source on the page to see what’s going on overall. This demo comes apart of the Studio for MVC Wijmo download.

image

Really, this isn’t difficult to do.

First thing to notice is that we use two different ways to manage data in the grids. This is done for no particular reason except to show variety in solutions. Here’s the code snippet we use to implement the top (master) grid.

$.ajax({
            url: "@Url.Action("StockSymbols")",
            data: { symbols: stockApp.symbols },
            dataType: "json",
            success: function (data) {
                $("#stockList").wijgrid({
                    data: data,
                    allowSorting: true,
                    columns: [
                        {},
                        {},
                        { dataType: "number" },
                        { dataType: "number" },
                        { dataType: "number" },
                        { dataType: "number" },
                        { dataType: "number" },
                        { dataType: "number" },
                        { visible: false },
                        { visible: false }
                    ],
                    selectionchanged: function (e, args) {
                            stockApp.currentSymbol = $("#stockList").wijgrid("selection").selectedCells().item(0).value();
                            stockApp.stockChange();
                    }
                });
            }
        });

Let’s walk through what this code is doing. We’re initiating an AJAX call to an action called “StockSymbols”. This action returns all the high level data that we need for our application. With it, we’re passing in a list of symbols we’d like data on. That’s the contents of stockApp.symbols.

When the AJAX call returns, it passes the end result into the success call back. This is where the WijGrid gets created. The data from the callback gets passed in as the data parameter on the WijGrid. We’re also enabled sorting on the grid, and defining properties for the columns we’re going to import. Finally, we have a selectionchanged event for when the user selects a new row (note: this is called for row one on when loaded for the first time.)

The selection changed event is what spawns the magic for our details row. By setting a property for the currentSymbol, we can create a call to our server to pull the latest information for that symbol. That’s what the stockChange method does. Here it is:

stockApp.stockChange = function () {
        stockApp.dataSource = stockApp.createDatasource();

        $("#stockDetailList").wijgrid("destroy").empty();
        $("#stockDetailList").wijgrid({
            allowSorting: true,
            data: stockApp.dataSource,
                columns:[
                    {}, {},
                    {dataType: "number"},
                    {dataType: "number"},
                    {dataType: "number"},
                    {dataType: "number"},
                    {dataType: "number"}
                ]
        });
    };

This process doesn’t use a jQuery AJAX request directly to gather the data. Instead, it uses a WijDataSource to do the AJAX call and JSON reading automatically. Because WijDataSource doesn’t support refreshing data on demand (at least not in the way we want to use it), I abstracted the creation into the createDatasource method. We’ll go into that method here in a second.

Because our WijGrid datasource doesn’t support refreshing data on demand, we need to recreate the grid for each request. In order to recreate the grid, first it needs to be destroyed and the tag needs to be emptied. Then we can proceed to recreate the grid (or create it for the first time). The details grid takes our WijDataSource as a property, and defines properties for the columns we’re going to load. This call will automatically go to fetch data from our data source.

stockApp.createDatasource = function () {
        return new wijdatasource({
                    proxy: new wijhttpproxy({
                        url: "@Url.Action("StockSymbolHistoryByDates")",
                        data: { symbol: stockApp.currentSymbol, startDate: stockApp.minDate, endDate: stockApp.maxDate },
                        dataType: "json",
                        success: function (){
                            stockApp.isDataLoaded = true;
                        }
                    }),
                    reader: new wijarrayreader([
                    { name: "Symbol", mapping: "Symbol" },
                    { name: "Date", mapping: "DateString" },
                     { name: "Open", mapping: "Open" },
                     { name: "Close", mapping: "Close" },
                     { name: "High", mapping: "High" },
                     { name: "Low", mapping: "Low" },
                     { name: "Volume", mapping: "Volume" }
                  ])
                });
    };

In the createDatasource method, we make a call to the HTTP url we want to pull data from. In this case, it’s an action on our controller. We want to pass in the currently selected symbol, as well as the minimum and maximum dates for our data. If you’re feeling like playing around, write a filter for this grid for the dates.

The reader takes the array list returned by the action as JSON, and parses it into the columns we want to present in the grid. The mapping property points to the name of the property in our data, and the name is what we want to call that column on the screen.

That’s all the code required to make the master/detail grids to work. And, of course, this is one way to solve the problem. Feel free to play with different approaches and see if something works better!

I hope that helps you in your applications. If you have any comments, questions, or suggestions, feel free to post them in the comments or shoot me an email.

Kevin Griffin
keving@componentone.com

Follow me on Twitter

A Guide to Wijmo Theming

One of the major features of Wijmo is that it’s built on top of the jQuery plugin model. The benefit of this is that the Wijmo team took great care into making sure that you got the benefits of working with jQueryUI widgets. The major benefit being ThemeRoller support.

image

I always find it amazing when I talk to people and people haven’t heard about ThemeRoller. It’s just the greatest thing since sliced bread.

ThemeRoller

When jQueryUI was released, it provided a great array of user interface widgets for you to use in your applications. However, if you’ve used other component packs you know that theming can be really really hard or really really easy. Developers don’t really do theming or design well, so that’s where the ThemeRoller tool was born.

ThemeRoller lets you or another person design themes for your sites. It’s developer friendly, and awesome if you have a designer on your team. Build your own custom themes or tweak one of the many existing ones.

image

When I used to do consulting, I used <COMPETITOR NAME REDACTED>’s toolset. But it was so frustrating to do a lot of stuff that should be similar in terms of theming. Instead, I rewrote the majority of the site with jQueryUI widgets. I provided the ThemeRoller tool to my designer and within the day, she provided me the CSS file I needed to add to my project. It’s just that easy!

How Wijmo Uses ThemeRoller

Out of the box, Wijmo uses ThemeRoller themes. You don’t have to do ANYTHING. Imagine you have a grid:

image

It’s a nice grid, but you like the “Redmond” style better. That’s cool, just replace the CSS file.

image

BOOM! You’re done.

Editing Themes

I’m bringing this note up since someone asked a question in the forums. How do you edit a theme after you’ve downloaded it? That’s easy!

First, open up the CSS file. Scroll down a couple lines until you find this comment:

image

Note: if CSS is minified, you won’t see this.

See that link at the bottom, paste it into your browser and go. ThemeRoller will automatically load all the settings for that theme. Make your changes and download the new style. This is handy on jQueryUI updates. If they add new widgets, you can just load the file into ThemeRoller and it’ll create an updated style sheet for the theme.

So go have fun! Let me know if you have questions or comments! Go have fun with ThemeRoller, and make Wijmo your own!

Kevin Griffin
keving@componentone.com

Follow me on Twitter @1kevgriff

Update Wijmo Charts in Real Time

Wijmo 1.3.0 introduced a very cool feature called Series Transitions. What this feature allow you to do is define a chart (pie/line/bar), and have it fluently update based on new data. Before this feature, the chart would have to redraw itself every time.

Check out the Wijmo Explore demo to see this in action.

This opens up a lot of possibilities for your applications, and is even the topic for an upcoming MSDN webcast I’m presenting. You should go register for it. I’ll wait here!

The basis for the webcast is that you can automate the updating of the charts by using AJAX calls back to the server.

First step we take is to create a basic, empty chart.

<div id="popularGenres">
</div>

dashboard.init = function () {
        $("#popularGenres").wijbarchart({
            width: 700
        });

        dashboard.populatePopularGenres();
        dashboard.startTimer();
    };

Next, we need to write some code that’ll get the initial data from the server and kick off a process for pinging the server.

dashboard.startTimer = function (){
        dashboard._updateTimer = setInterval("dashboard.populatePopularGenres()", dashboard.updateInterval);
};

dashboard.populatePopularGenres = function () {
        $.ajax("@Url.Action("GetTotalOrdersByGenre", "Dashboard")", {
            success: function (data){
                $("#popularGenres").wijbarchart("option", "seriesList", data);
            },
            error: function () {
                alert("Something terrible went wrong.");
                dashboard.clearTimer();
            }
        });
};

The startTimer method is used call the populatePopularGenres method. It uses the JavaScript method setInterval to make a function call every X milliseconds.

The populatePopularGenres method is where the magic happens. First, you’ll see we make an AJAX call to an Action in our MVC controller “dashboard”. This call returns a custom object that mimics what is required for the seriesList property on the chart.

public class BarChartSeriesList
{
        public string label { get; set; }
        public bool legendEntry { get; set; }

        public BarChartData data { get; set; }
}

public class BarChartData
{
        public List<object> x { get; set; }
        public List<int> y { get; set; }
}

While this isn’t all the options available, it’s enough for our needs in the demo. If we return a JSON-ified version of a BarChartSeriesList object, we can set it as the value of the seriesList property for our bar chart. Wijmo will detect the data change and automatically transition itself to reflect the new data.

What will happen, if you have a real time system, is that the chart will update with new data on your defined interval. In our demo, I’m going to be using the MVC Music Studio example to show off how this approach can be used with real data.

If you’d like to see more, or ask me questions live in person, feel free to register for my webcast “Real-Time Dashboards with ASP.NET MVC, jQuery, and Wijmo” on August 23rd.

Kevin Griffin
keving@componentone.com

Follow me on twitter @1kevgriff