Filter Multi Checkboxes - Ajax DataSource returns data in different structure

4 posts, 1 answers
  1. Malcolm
    Malcolm avatar
    6 posts
    Member since:
    Dec 2014

    Posted 17 Jul 2015 Link to this post

    I'm using the "Filter Multi Checkboxes" and I am trying to hook it up to an existing MVC Controller Action which returns JSON data but in a different structure to what is expected.

    Currently my code is

    columns
        .Bound(p => p.Customer)
        .Filterable(ftb => ftb
            .Multi(true)
            .DataSource(ds => ds
                .Read(r => r
                    .Action("Customers_Read", "SalesLedger")
                )
            )
        );

    And this expects the action to return data in this structure

    [{"Customer":"John"},{"Customer":"Bob"},{"Customer":"Mary"}]
    But my action return the data as

    ["John","Bob","Customer"]

    Is this possible?

    Thanks 

     

  2. Answer
    Rosen
    Admin
    Rosen avatar
    3237 posts

    Posted 21 Jul 2015 Link to this post

    Hello Malcolm,

    You can achieve such functionality using ItemTemplate option, similar to the following:

    columns
        .Bound(p => p.Customer)
        .Filterable(ftb => ftb
            .Multi(true)
            .ItemTemplate("template")
            .DataSource(ds => ds
                .Read(r => r
                    .Action("Customers_Read", "SalesLedger")
                )
            )
        );
     
         <script type="text/javascript">
            function template(e) {
                if (e.field == "all") {
                    //handle the check-all checkbox template
                    return "<li><label><strong><input type='checkbox' />#= all#</strong></label></li>";
                } else {
                    //handle the other checkboxes
                    return "<li><label><input type='checkbox' name='" + e.field + "' value='#=data#'/><span>#= data #</span></label></li>";
                }
            }
            </script>

    Note that the template element is bound to the whole item instead of to its field.

    Regards,
    Rosen
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Malcolm
    Malcolm avatar
    6 posts
    Member since:
    Dec 2014

    Posted 21 Jul 2015 in reply to Rosen Link to this post

    Thanks, worked a treat.
  5. Stefan
    Stefan avatar
    1 posts
    Member since:
    Mar 2016

    Posted 15 Jun Link to this post

    In my opinion it's better to use a separate Action method for filling "Filter Multi Checkboxes", for example like this:

    public ActionResult Customers_Read([DataSourceRequest]DataSourceRequest request)
    {
        var values = db.Customers.Select(c => c.Customer).Distinct().Select(value => new {
            Customer = value
        });
        return Json(values);
    }


Back to Top
UI for ASP.NET MVC is VS 2017 Ready