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

Filterrow column input formatting

2 Answers 114 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 06 Dec 2019, 04:20 PM

I cannot find any examples / documentation on how to format the column filtering input for Core ASP.net.

For example I have a column which is called ID. It is filterable but I want it formatted so that :
1 - It does not have the spinner

2 - After entering a value it does not reformat it from Integer to Decimal with a thousands separator. For example 1234 become 1,234.00 The current code I have is:

@(Html.Kendo().Grid<CIPHRChecks.Models.Order>()
            .Name("Grid")
            .HtmlAttributes(new { @class = "ChecksGrid" })
            .Columns(columns =>
            {
                columns.Bound(o => o.Id).Title("ID").Format("{0}").Width(200).Filterable(f => f.Cell(cell => cell.ShowOperators(false)));
                columns.Bound(o => o.OrderDate).Title("Order Date").Format("{0:dd/MM/yyyy}").Width(80);
                columns.Bound(o => o.Source.Name).Title("Source").Width(80).Filterable(ftb => ftb.Multi(true).Search(true));

                columns.Bound(o => o.OrderStatus.Description).Title("Status").Width(200);
                columns.Bound(o => o.Service.Name).Title("Service").Width(200);
                columns.Bound(o => o.SourcePersonReference).Title("Reference").Width(120).Filterable(f => f.Multi(false).Cell(cell => cell.ShowOperators(false)));
                columns.Bound(o => o.SourcePersonDescriptor).Title("Descriptor").Width(200).Filterable(f => f.Multi(false).Cell(cell => cell.ShowOperators(false)));
            })
                .Pageable(p =>
            {
                p.PageSizes(new[] { 15, 25, 50 });
            })

            .Sortable(sortable => sortable
            .SortMode(GridSortMode.MultipleColumn))
            .Filterable(ftb => ftb.Mode(GridFilterMode.Row))
            .Scrollable()
            .DataSource(dataSource => dataSource
                .Ajax()
                .Read(read => read.Action("Read", "Home")
            )
                .PageSize(15)
            )
)

2 Answers, 1 is accepted

Sort by
1
Accepted
Alex Hajigeorgieva
Telerik team
answered on 11 Dec 2019, 10:24 AM

Hello, Simon,

To render the desired custom Filterable UI, we need to declare a template for the filterable cell and provide the required configurations to it.

https://docs.telerik.com/kendo-ui/api/javascript/ui/numerictextbox/configuration/decimals

https://docs.telerik.com/kendo-ui/api/javascript/ui/numerictextbox/configuration/format

The below snippet should provide you with both of the desired outcomes. Give this a try and let me know in case you have further questions:

columns.Bound(o => o.Id).Title("ID").Format("{0}").Width(200).Filterable(f => f.Cell(cell => cell.ShowOperators(false).Template("myFilterCell")));

<script>
    function myFilterCell(args) {
         args.element.kendoNumericTextBox({
             spinners: false,
             decimals:0,
             format: "{0:0}"
         });
    }
</script>

Kind Regards,
Alex Hajigeorgieva
Progress Telerik

Get quickly onboarded and successful with Telerik UI for ASP.NET Core with the dedicated Virtual Classroom technical training, available to all active customers.
Robert
Top achievements
Rank 1
commented on 11 Jul 2023, 04:42 PM

Is there any way to get this to continue to work if you use state persistence?

https://demos.telerik.com/aspnet-core/grid/persist-state

The accepted answer works fine if you don't click away, but if you come back and setOptions is called, it seems to go back to displaying the value with the default format.

e.g.
search for 14892

after setOptions it becomes 14,892.00

Alexander
Telerik team
commented on 14 Jul 2023, 01:59 PM

Hi Robert,

Generally, "JSON.stringify()" cannot serialize function references (e.g. event handlers), so if stringification is used for the retrieved Grid state, all configuration fields, which represent function references, will be lost.

You have two options to avoid this limitation: use a custom implementation to serialize JavaScript functions, or add the function references back to the deserialized configuration object before passing it to the setOptions method.

In this regard, a possible recommendation would be to explicitly supplement the desired UI cell handler back to the Grid.

For the purposes of the demonstration, I have created a proof of concept example that is incorporated from the demo you linked.

Where I have added an additional template handler for one of the columns:

.Columns(columns =>
{
    columns.Bound(c => c.ContactName).Filterable(f => f.Cell(cell => cell.ShowOperators(false).Template("myFilterCell"))).Width(400);
})


<script>
    function myFilterCell(args) {
         args.element.kendoTextBox({
             enable: false
         });
    }
</script>

And the handler is passed back within the following layer of logic:

<script>
    function myFilterCell(args) {
         args.element.kendoTextBox({
             enable: false
         });
    }
    $(document).ready( function () {
        var grid = $("#grid").data("kendoGrid");

        $("#load").click(function (e) {
            e.preventDefault();
            var columns = grid.columns; // 1. Gather the columns.
            var options = localStorage["kendo-grid-options"];
            if (options) {
                var parsedOptions = JSON.parse(options); // 2. Parse the Grid options.
                columns.find(column => column.field == "ContactName").filterable.cell.template = myFilterCell; // 3. Reattach the template handler.

                parsedOptions.columns = columns; // 4. Add the altered columns to the parsed options.
                
                grid.setOptions(parsedOptions); // 5. Set the Grid's options.
            }
        });
    });
</script>

For your convenience, here is a Telerik REPL example that tackles the aforementioned approach:

I hope this helps during your endeavors.

0
Simon
Top achievements
Rank 1
answered on 17 Dec 2019, 09:45 AM
Perfect Thankyou
Tags
Grid
Asked by
Simon
Top achievements
Rank 1
Answers by
Alex Hajigeorgieva
Telerik team
Simon
Top achievements
Rank 1
Share this question
or