Changing the kendo numeric filter format

2020-06-16 08:29发布

I am applying custom paging and sorting in the kendo grid. For one of my column I am getting a numeric text box. When I insert the value in numeric text box it is integer, but as the focus is removed from the filter numeric text box it converts into decimal. For eg: if I entered 32, it remains 32,but as the focus is removed the value becomes 32.00. But I want that value to remains 32.

So any one can please help me out with the solution.

8条回答
Ridiculous、
2楼-- · 2020-06-16 09:02

What Palani Kumar has said still inserts separators, that is it converts 12345 to 12,345; so i recommend the following edited code in case you want pure numbers without any formatting (no separators, no decimal points etc.).

columns.Bound(x => x.Property).Filterable(x => x.UI("numericFilter"));

<script type="text/javascript">
    function numericFilter(control) {
        $(control).kendoNumericTextBox({ format: "#", decimals: 0 });

    }
</script> 
查看更多
闹够了就滚
3楼-- · 2020-06-16 09:06

I was having the same issue and columns.Bound(x => x.Property).Filterable(x => x.UI("numericFilter")); was not working for me. Piggy backing off of Xavier John's answer I used columns.filterable.cell.template to fix my problem because telerik's documentation states:

columns.filterable.ui String |Function

The role data attribute of the widget used in the filter menu or a JavaScript function which initializes that widget.

This feature is not supported for columns which have their values option set. If filterable.mode is set to 'row', columns.filterable.cell.template should be used to customize the input.

http://docs.telerik.com/kendo-ui/api/javascript/ui/grid#configuration-columns.filterable.ui

Here's my code

    @(Html.Kendo().Grid<UnsoldStockBO.USS_STOCK>()
        .Name("searchGrid")
        .Columns(columns =>
        {
            columns.Bound(c => c.CyAbbrev);
            columns.Bound(c => c.UssCrop).Filterable(filter => filter.Cell(c => c.Template("IntegerFilter")));
            columns.Bound(c => c.TtAbbrev);
            columns.Bound(c => c.PtAbbrev);
            columns.Bound(c => c.UssInternalGrade);
            columns.Bound(c => c.SuggestedPrice).Format("{0:c2}").Filterable(filter => filter.Cell(c => c.Template("NumericFilter")));
            columns.Bound(c => c.UssCtPricePerKilo).ClientTemplate("#:kendo.toString(UssCtPricePerKilo, 'c', Culture)#").Filterable(filter => filter.Cell(c => c.Template("NumericFilter")));
            columns.Bound(c => c.UssNetKilos).Format("{0:n}").Filterable(filter => filter.Cell(c => c.Template("NumericFilter")));
            columns.Bound(c => c.UssWriteDownCount).Format("{0:n0}").Filterable(filter => filter.Cell(c => c.Template("IntegerFilter")));
            columns.Command(command =>
            {
                command.Edit().HtmlAttributes(new { @title = "Quick Edit" });
                command.Custom("EditStock").HtmlAttributes(new { @title = "Edit" });
                command.Destroy().HtmlAttributes(new { @title = "Delete" });
            }).HtmlAttributes(new { @nowrap = "nowrap" });
        })
        .Mobile()
        .ToolBar(toolbar => toolbar.Custom().Text("Add Stock").Action("Create", "Stock").Name("AddStock"))
            .Editable(editable => editable.Mode(GridEditMode.InLine).DisplayDeleteConfirmation("This record will be permanently deleted and cannot be recovered. Are you sure?"))
            .Filterable(ftb => ftb.Mode(GridFilterMode.Row))
            .Sortable()
            .Pageable()
            .Events(e => e.DataBound("onDataBound").Cancel("onCancel"))
        .NoRecords("No records found.")
        .Resizable(resize => resize.Columns(true))
        .DataSource(dataSource => dataSource
            .WebApi()
            .ServerOperation(true)
            .PageSize(30)
                        .Events(events => events.Error("error_handler").Sync("sync_handler"))
                    .Model(model =>
                    {
                        model.Id(p => p.UssId);
                        model.Field(c => c.CyAbbrev).Editable(false);
                        model.Field(c => c.UssCrop).Editable(false);
                        model.Field(c => c.TtAbbrev).Editable(false);
                        model.Field(c => c.PtAbbrev).Editable(false);
                        model.Field(c => c.UssInternalGrade).Editable(false);
                        model.Field(c => c.SuggestedPrice).Editable(false);
                    })
                    .Read(read => read.Url(Url.HttpRouteUrl("DefaultApi", new { controller = "webapi", action = "UnsoldStock_Get", userId = ((UnsoldStockBO.PSI_USER)Session["USS_User"]).UUid })))
                                .Update(update => update.Url(Url.HttpRouteUrl("DefaultApi", new { controller = "webapi", action = "UnsoldStock_Update", userName = ((UnsoldStockBO.PSI_USER)Session["USS_User"]).UUserName })).Type(HttpVerbs.Post))
                                    .Destroy(destroy => destroy.Url(Url.HttpRouteUrl("DefaultApi", new { controller = "webapi", action = "UnsoldStock_Delete", id = "{0}", userName = ((UnsoldStockBO.PSI_USER)Session["USS_User"]).UUserName })).Type(HttpVerbs.Delete))

                )
    )

<script type="text/javascript">

    function IntegerFilter(args) {
        args.element.kendoNumericTextBox({ format: "#", decimals: 0, spinners: false });
    }

    function NumericFilter(args) {
        args.element.kendoNumericTextBox({ spinners: false });
    }

</script>
查看更多
Juvenile、少年°
4楼-- · 2020-06-16 09:07

You can set like below

columns.Bound(x => x.Property).Filterable(x => x.UI("NumericFilter"));

<script type="text/javascript">
function NumericFilter(control) {
    $(control).kendoNumericTextBox({ "format": "n0", "decimals": 0 });

}
</script>

Or use extension method

columns.NumericColumn(x => x.Property);

public static GridBoundColumnBuilder<TModel> NumericColumn<TModel, TValue>(this GridColumnFactory<TModel> Column, Expression<Func<TModel, TValue>> Expression) where TModel : class
{
    return Column.Bound(Expression).Filterable(x => x.UI("NumericFilter"));

}
查看更多
姐就是有狂的资本
5楼-- · 2020-06-16 09:17

Simply set the column format:

c.Bound(x => x.ColumnName).Format("{0:#}");
查看更多
欢心
6楼-- · 2020-06-16 09:19

Set the filter column as

column.filterable = {
    ui: numberFilter,
    cell: {
        template: numberFilter,
    }
};

function numberFilter(args) {
    var element = args instanceof jQuery ? args : args.element;
    element.kendoNumericTextBox({
        format: "n0"
    });
}

查看更多
我欲成王,谁敢阻挡
7楼-- · 2020-06-16 09:24

You can set format on the filterable on the column like this:

            field: "TaskId",
            title: "TaskId",
            width: 80,
            filterable: {
                ui: function (element) {
                    element.kendoNumericTextBox({
                        format: "n0"
                    });
                }
            }

Update This is the javascript version. here my complete grid defintion:

      $("#uxRunningTasksGrid").kendoGrid({
        dataSource: runningTasksDataSource,
        height: $(document).height() - 280,
        autoBind: true,
        filterable: true,
        sortable: true,
        pageable: false,
        reorderable: true,
        resizable: true,
        columns: [{
            command: { text: "Details", click: openDetails }, title: " ", width: 80
        },
            {
                field: "TaskId",
                title: "TaskId",
                width: 80,
                filterable: {
                    ui: function (element) {
                        element.kendoNumericTextBox({
                            format: "n0"
                        });
                    }
                }
            }
        ]
     };
查看更多
登录 后发表回答