Using aggregates with ListView

7 posts, 1 answers
  1. Padmaja
    Padmaja avatar
    6 posts
    Member since:
    Nov 2012

    Posted 03 Jan 2013 Link to this post

    Hello, 

    We would like to use Aggregates with ListView. We have been able to get it working with Grid but not ListView.

    <script type="text/x-kendo-tmpl" id="template">
        <div class="product">
            <img src="@Url.Content("/")${ImagePath}.jpg" alt="${Name} image" />
            <h3>${Name}</h3>
            <dl>
                <dt>Price:</dt>
                <dd>${kendo.toString(Price, "c")}</dd>
                <dt><input type="button" value="Remove"/></dt>
            </dl>
        </div>
        <div>
            <h3>Total price: ?????</h3>
        </div>
    </script>
     
    @(Html.Kendo().ListView<SixthCoffee.Web.ViewModels.CartItem>()
        .Name("CartList")
        .TagName("div")
        .ClientTemplateId("template")
        .DataSource(dataSource => {
            dataSource.Read(read => read.Action("AjaxCartGrid_Read", "MenuSurface"));
            dataSource.Aggregates(aggregates =>
                {
                    aggregates.Add(s => s.TotalPrice).Sum();
                });
        })
                 
    )
    Any help will be greatly appreciated.

    Thanks,
    Padmaja
  2. Answer
    Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 08 Jan 2013 Link to this post

    Hello Padmaja,

    Here is how you can get reference to the ListView datasource and show aggregates in the template:
    #= $("\#CartList").data("kendoListView").dataSource.aggregates().TotalPrice.sum #

    Regards,
    Nikolay Rusev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Padmaja
    Padmaja avatar
    6 posts
    Member since:
    Nov 2012

    Posted 10 Jan 2013 Link to this post

    Thanks that works.
  5. Padmaja
    Padmaja avatar
    6 posts
    Member since:
    Nov 2012

    Posted 11 Jan 2013 Link to this post

    Hello Nikolay,

    We realized that we should not be having the Total Price in the ListView template as it is invoked for each of the list item.
    var aggregates = $("\#CartList").data("kendoListView").dataSource.aggregates().TotalPrice.sum;
     alert(aggregates);
    Can you please help us in identifying whats wrong with the above script?

    Thanks,
    Padmaja
  6. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 14 Jan 2013 Link to this post

    Hello Padmaja,

    I do not understand your question. The item template will be evaluated for every ListView item. It is essential for data bound widgets such as ListView.

    All the best,
    Nikolay Rusev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  7. Padmaja
    Padmaja avatar
    6 posts
    Member since:
    Nov 2012

    Posted 15 Jan 2013 Link to this post

    Hello Nikolay,

    I should have detailed it better. Essentially what we want to do is, being able to set the TotalPrice (sum of all entities in the ListView) to a span tag using javascript function. This javascript function is then called on the DataBound event of the ListView.

    If the problem is not clear, I can add some sample code.

    Thanks,
    Padmaja
  8. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 16 Jan 2013 Link to this post

    Hello Padmaja,

    It is still unclear to me what you are trying to achieve here. You can create a jsfiddle/jsbin sample illustrating your scenario so we can speed up the communication a bit.

    You cannot embed in the ListView template a function that will be called at some point later than when the template is evaluated. If that is what you mean. 

    Regards,
    Nikolay Rusev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top
UI for ASP.NET MVC is VS 2017 Ready