Is it possible to add a Sparkline inside a ClientRowTemplate in a Grid?

2 posts, 0 answers
  1. Nicklas
    Nicklas avatar
    45 posts
    Member since:
    Oct 2012

    Posted 05 Jun 2013 Link to this post

    I'm trying to add a column of sparklines inside a grid, using a ClientRowTemplate that is based on a partial view. So far, this is what I've got;
    Index view (holding grid):
              .DataSource(ds => ds
                  .Read(read => read.Action("Index", "Home"))
              .HtmlAttributes(new { style = "height:430px;" })
              .Columns(columns =>
                      columns.Bound(p => p.UnitContract.Name).Title("Unit");
                      columns.Bound(p => p.UnitContract.CurrentRun.Operation.WellContract.Location).Title("Site");
                      columns.Bound(p => p.UnitContract.CurrentRun.Operation.WellContract.Name).Title("Well");
                      columns.Bound(p => p.UnitContract.CurrentRun.Id).Title("Run");
                      columns.Bound(p => p.UnitContract.CurrentRun.RunTask).Title("Task");
                      columns.Bound(p => p.UnitContract.CurrentRun.Operation.Description).Title("Operation");
                      columns.Bound(p => p.UnitContract.Status.StatusText).Title("Status");
                      columns.Bound(p => p.UnitContract.CurrentRun.LatestWellLogEntry.Depth).Title("Depth (m)");
                      columns.Bound(p => p.UnitContract.CurrentRun.LatestWellLogEntry.Speed).Title("Speed (m/min)");
                      columns.Bound(p => p.UnitContract.CurrentRun.LatestWellLogEntry.Tension).Title("Weight (kg)");
                      columns.Bound(p => p.UnitContract.CurrentRun.Name);
                      columns.Template(p => { }).ClientTemplate(" ").Width(75);
              .ClientRowTemplate(Html.Partial("_ClientRowTemplate", Model).ToHtmlString())
              .Scrollable(builder => builder.Height(250))

    Next, I added a partial view, looking like this:
        <td>#: UnitContract.Name#
        <td>#: UnitContract.CurrentRun.Operation.WellContract.Location#
        <td>#: UnitContract.CurrentRun.Operation.WellContract.Name#
        <td>#: UnitContract.CurrentRun.Id#
        <td>#: UnitContract.CurrentRun.RunTask#
        <td>#: StatusMessage#
        <td class='text-center'>
            <span class='label label-#:StatusColor #' style='width: 25px;'> </span>
                      .Data(#: UnitContract.CurrentRun.LatestWellLogEntry.Speed#)
                      .ValueAxis(axis => axis.Numeric().Max(5000).Min(-5000).Visible(false))
                      .CategoryAxis(c =>
                                    c.MajorTicks(ticks => ticks.Visible(false))
                      .ChartArea(ca => ca.Background("transparent"))
                      .HtmlAttributes(new { style = "width: 75px;" }))
                <!-- DataWiz Sparkline -->
                <!-- DataWiz Sparkline -->
        <td>#: UnitContract.CurrentRun.Name#
            <form method='POST' action='@Url.Content("~/UnitDetails/Index/")#: UnitContract.Id#'>
                <input type='submit' class='k-button' value='Details' />
    I've tried several different approaches as far as the part with .Data(source) goes.
     Is there another way to go that lets me utilize the Sparkline inside a Grid, or is this combination not possible?
  2. Vladimir Iliev
    Vladimir Iliev avatar
    2196 posts

    Posted 07 Jun 2013 Link to this post

    Hi Nicklas,

    Basically current approach to render the data using KendoUI template syntax is not possible as the data will be rendered as string. Possible solutions are:

    • Load the SparkLine data from remote source - for example you can pass the current record ID as additional parameter to the controller. In this scenario you should also manually execute the SparkLine scripts on the client side using the DataBound event of the Grid.  Please check the example below:
          //generate unique ID for each SparkLine
          //Send the model ID as additional parameter (OrderID in current case)
          .DataSource(d => d.Read(a => a.Action("ReadSparkLine", "Home").Data("{OrderID: #=OrderID#}")))
      //use the Grid DataBound event to execute the generated SparkLine scripts on the client side
      function onDataBound(e) {
          this.tbody.find("script").each(function () {
    • Another option is to initialize the SparkLine using JavaScript and use the data from the model - please check the example below:  
              <!-- generate unique id for each SparkLine (render only span)-->
              <span id="spark#=OrderID#"></span>
      //use DataBound event to initialize the SparkLIne
      //from each rendered span element from the row template
      //using the data from the current model
      function onDataBound(e) {
          var data = this.dataSource.view();
          for (var i = 0; i < data.length; i++) {
              var element = $("#spark" + data[i].OrderID);
                  type: "area",
                  //OrderPriceChange field in the model contains array with data for the SparkLine
                  data: data[i].OrderPriceChange
    Kind Regards,
    Vladimir Iliev
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top