Populate Grid Records Based upon Toggle button

2 Answers 105 Views
Switch
froger
Top achievements
Rank 1
Iron
froger asked on 02 Nov 2021, 06:07 AM

Hi,

I have one table

Emplpyee

..................

Id   Name    Active

1     a                 1

2     b                0

 

my requirement is that

i have one toggle button (by default select Yes(1)) 

based upon that values load grid records

once i change that value to No then display records based upon that

can you please provide me sample code for this one

2 Answers, 1 is accepted

Sort by
0
Yanislav
Telerik team
answered on 02 Nov 2021, 02:13 PM

Hello, 

You can reach this behavior by passing a parameter to your action, after that filter the data on the server.

You can pass parameter this way : 

 

.Read(r=>r.Action("People", "Home").Data("functionName")))

 

Where 'functionName' is the name of the function which will return your parameter. In your case this function is simple you just return true or false depends on that, is the switch is on or of:

 

    function functionName() {
        return {
            paramName: value
        }
    }

 

You have to setup the switch to force the Grid to make a request. Here is the whole snippet:

 

var isChecked = true;
    $(document).ready(function () {
        $('#switch').kendoSwitch({
            change: function (e) {
                isChecked = e.checked; //true/false depends on that is the switch is turned on
                $("#grid").data("kendoGrid").dataSource.read(); // make request
            }
        });
    })
    
    function isActive() {
        return {
            isActive: isChecked
        }
    }

 

You action declaration should be something like this:

 public ActionResult People([DataSourceRequest] DataSourceRequest request, bool isActive)
        {

            var people = Enumerable.Range(1, 50).Select(x => new PersonViewModel
            {
                FirstName = "MyName" + " " + x,
                IsActive = x % 2 == 0
            }).Where(x=>x.IsActive == isActive);
            return this.Json(people.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

You can use the parameter to filter the data in the action, or you can make different queries conditionally, based on the value you get in the action.

Regards,
Yanislav
Progress Telerik

Remote troubleshooting is now easier with Telerik Fiddler Jam. Get the full context to end-users' issues in just three steps! Start your trial here - https://www.telerik.com/fiddler-jam.
froger
Top achievements
Rank 1
Iron
commented on 09 Nov 2021, 10:27 AM

Please provide me sample code example for that above requirement so that it can be helpful to me 

 

since i am new to kendo mvc

 

0
Yanislav
Telerik team
answered on 09 Nov 2021, 03:19 PM

Hello, Mahesh

I attached a sample project, so you can see in detail how to reach the desired behavior.

Regards,
Yanislav
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

froger
Top achievements
Rank 1
Iron
commented on 17 Nov 2021, 07:31 AM

Tags
Switch
Asked by
froger
Top achievements
Rank 1
Iron
Answers by
Yanislav
Telerik team
Share this question
or