Need to pass a parameter based on dropdown selection to refresh grid

1 Answer 52 Views
Grid
JOHNY
Top achievements
Rank 1
JOHNY asked on 27 Feb 2024, 05:13 AM | edited on 27 Feb 2024, 05:14 AM
Telerik UI for asp.net core 2024.1.130

I need to pass a parameter from dropdownlist selection to the ajax function that refreshes the grid. Using the following code to accomplish this using taghelper syntax, however the parameter is not getting passed to ajax api. Could you please help?

<form id="frmEnableFeed">
    <div class="mb-3 form-group">
        <label asp-for="SelectedAirline" class="form-label">Select the Airline to configure:</label>
        <kendo-dropdownlist class="form-select" name="SelectedAirline"
                                    datatextfield="Text"
                                    datavaluefield="Value"
                                    bind-to="Model.CompanySelectList"
                                    option-label="Please select an Airline..."
                                    filter="FilterType.Contains">
        </kendo-dropdownlist>
    <div class="mb-3 form-group">
        <kendo-grid name="grid" height="430">
            <datasource type="DataSourceTagHelperType.Ajax" page-size="20"
                        on-error="error_handler">
                <schema data="Data" total="Total">
                    <model id="AirlineID">
                        <fields>
                            <field name="AirlineID" type="string" editable="false"></field>
                            <field name="AirlineName" type="string"></field>
                            <field name="Enabled" type="boolean"></field>
                        </fields>
                    </model>
                </schema>
                <transport>
                    <read url="@Url.Action("GetAirlines", "Feed")" />
                </transport>
            </datasource>
            <columns>
                <column field="AirlineName" />
                <column field="ITP Services" width="100" />
                <column field="Feed Enabled" width="100" />
            </columns>
            <toolbar>
                <toolbar-button name="create"></toolbar-button>
            </toolbar>
            <editable mode="popup" />
            <pageable enabled="true" />
            <sortable enabled="true" />
            <scrollable enabled="true" />
        </kendo-grid>
    </div>
    <div class="mb-3 form-group">
        <button type="submit" class="btn btn-primary" id="btnSaveChange">Save Changes</button>
    </div>
</form>
@section Scripts {
    <partial name="_ValidationScriptsPartial" />
    <script>
        $(function () {
            $("#SelectedItp").change(function () {
                var cid = $(this).val();
                var grid = $("#grid").data("kendoGrid");
                grid.dataSource.read({ companyId: cid });
            })
        });
    </script>
}

 

 

1 Answer, 1 is accepted

Sort by
0
Mihaela
Telerik team
answered on 29 Feb 2024, 03:59 PM

Hello Johny,

To  pass a parameter through the Read request of the Grid based on the selected DropDownList option:

  • Handle the "change" event of the DropDownList control.
  • Get its value through the client-side value() method.
  • Call the read() method of the DataSource.
<kendo-dropdownlist class="form-select" name="SelectedAirline" on-change="onAirlineChange"
                                    datatextfield="Text"
                                    datavaluefield="Value"
                                    bind-to="Model.CompanySelectList"
                                    option-label="Please select an Airline..."
                                    filter="FilterType.Contains">
        </kendo-dropdownlist>


<script>
function onAirlineChange(e) {
    var cid = e.sender.value(); //get the selected DropDownList value
    var grid = $("#grid").data("kendoGrid");
    grid.dataSource.read({ companyId: cid });
}
</script>

For your convenience, I have prepared a REPL sample based on this example:

https://netcorerepl.telerik.com/GeawwNPp54lfCEy329

 

Regards,
Mihaela
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
JOHNY
Top achievements
Rank 1
commented on 01 Mar 2024, 08:23 PM

Thanks Mihaela for your response.

I have a question on this example,   the grid rows are not changing based on the dropdown list selection https://netcorerepl.telerik.com/GeawwNPp54lfCEy329.

That is what i am trying to accomplish.

Also can we change this example to disable to "Add a new record" button if nothing selected in the dropdown list?

Thanks again,

Johny

JOHNY
Top achievements
Rank 1
commented on 05 Mar 2024, 03:30 AM

To enable / disable the "Add new record" toolbar button I added the following code in dropdownlist change event, still no luck!

                var toolbar = grid.element.find(".k-grid-toolbar");
                var addNewButton = toolbar.find(".k-grid-add");

                if (cid && cid.length > 0) {
                    addNewButton.removeClass("k-state-disabled");
                } else {
                    addNewButton.addClass("k-state-disabled");
                }

Mihaela
Telerik team
commented on 05 Mar 2024, 05:08 PM

Hi Johny,

The Grid's data in the REPL sample is not filtered/updated based on the selected DropDownList value, because the "Orders_Read" Action is not updated to intercept the received "companyId" parameter. However, you can filter the data on the server and return it back to the Grid as follows:

 public IActionResult Orders_Read([DataSourceRequest]DataSourceRequest request, int companyId)
{
  var gridData = new List<AirlineViewModel>(); // populate the "gridData" with the data set
  if(companyId > 0) {
    gridData = gridData.Where(x => x.AirlineID == companyId).ToList(); //filter the data based on your requirements
  }
   return Json(gridData.ToDataSourceResult(request)); //returned the filtered data back to the Grid
}

Regarding the disabling of the "Add new record" command, use the "k-disabled" class:

$(document).ready(function(){
    var ddl = $("#SelectedAirline").data("kendoDropDownList");
    if(!ddl.value()) {
        $("#grid .k-grid-toolbar [data-role='create']").addClass("k-disabled"); //Disable the command initially
    }
    
})


function onAirlineChange(e) {
    var cid = e.sender.value(); //get the selected DropDownList value
    if(cid) {
        $("#grid .k-grid-toolbar [data-role='create']").removeClass("k-disabled");
    }
    var grid = $("#grid").data("kendoGrid");
    grid.dataSource.read({ companyId: cid });
}

Here is the updated REPL sample based on this example:

https://netcorerepl.telerik.com/QoYdOJlL07aiiORy37

Best,

Mihaela

JOHNY
Top achievements
Rank 1
commented on 12 Mar 2024, 06:20 PM

Thank you Mihaela, Appreciate your help.
Tags
Grid
Asked by
JOHNY
Top achievements
Rank 1
Answers by
Mihaela
Telerik team
Share this question
or