This is a migrated thread and some comments may be shown as answers.

Filter Multi Checkboxes - Ajax DataSource returns data in different structure

3 Answers 122 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Malcolm
Top achievements
Rank 1
Malcolm asked on 17 Jul 2015, 09:56 AM

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 

 

3 Answers, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 21 Jul 2015, 09:08 AM

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
0
Malcolm
Top achievements
Rank 1
answered on 21 Jul 2015, 09:24 AM
Thanks, worked a treat.
0
Stefan
Top achievements
Rank 1
answered on 15 Jun 2016, 10:02 AM

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);
}


Tags
Grid
Asked by
Malcolm
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Malcolm
Top achievements
Rank 1
Stefan
Top achievements
Rank 1
Share this question
or