Changing the kendo numeric filter format

老子叫甜甜 提交于 2019-12-04 05:58:55

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

}

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>

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

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> 

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

For asp.net core grid: columns.Bound(c => c.Expenditure).Format("{0:#,###.00}").Width(75);

Simply set the column format:

c.Bound(x => x.ColumnName).Format("{0:#}");

On Kendo NumericTextBox, Format is used to format the content of the input when it's not focused. You have tu use Decimals du format the input when it's focused. For example here is a NumericTextbox (asp.net) showing always integer :

@(Html.Kendo().NumericTextBox<decimal>()
        .Name("Total")
        .Format("n0")
        .Decimals(0)
    )

Hope it will help someone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!