Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kendo UI grid, search box in toolbar in mvc

Kendo-grid search box in toolbar in mvc with razor syntax,

I am facing i need toolbar in which searching box , this searching box search into grid data.

like image 824
Vishal Sen Avatar asked Dec 02 '22 14:12

Vishal Sen


2 Answers

Just copy and paste this code bind with mvc model and custom button(CRUD) and search box in toolbar in kendo grid template

<div>
    @(Html.Kendo().Grid(Model)
    .Name("DiagnosisTestGrid")
    .Columns(columns =>
    {
        columns.Bound(c => c.Description).Title("Description");
        columns.Bound(c => c.Cost).Title("Cost");
        columns.Bound(c => c.CostingRequired).Title("Cost Req.");
        columns.Bound(c => c.DxTestId).ClientTemplate(@"
    <a href='/DiagnosisTest/Details/#=DxTestId#' class = 'dialog-window'>Detail</a> |
    <a href='/DiagnosisTest/Edit/#=DxTestId#' class = 'dialog-window'  >Edit</a> |
    <a href='/DiagnosisTest/Delete/#=DxTestId#' class = 'dialog-window'>Delete</a>
            ").Title("");
    })

       .ToolBar(toolbar =>
        {
            toolbar.Template(@<text>
                <div class="toolbar">

                    <div class="row">
                        <div class="col-md-4">
                            <div class="input-group">
                                <span class="input-group-addon"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></span>
                                <input type="text" class="form-control" id='FieldFilter' placeholder="Search for...">
                                <span class="input-group-btn">
                                    <button class="btn btn-default" type="button"><span class="glyphicon glyphicon-refresh" aria-hidden="true"></span></button>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </text>);
        })

     .Resizable(resizing => resizing.Columns(true))
     .Sortable(sorting => sorting.Enabled(true))
     .Reorderable(reorder => reorder.Columns(true))
     .Pageable()
     .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(5)
        .ServerOperation(false)
        ))
</div>

Script for search box. and filter grid items

<script>
$(document).ready(function () {
    $("#FieldFilter").keyup(function () {

        var value = $("#FieldFilter").val();
        grid = $("#DiagnosisTestGrid").data("kendoGrid");

        if (value) {
            grid.dataSource.filter({ field: "Description", operator: "contains", value: value });
        } else {
            grid.dataSource.filter({});
        }
    });
});

enter image description here

like image 188
Vishal Sen Avatar answered Jan 24 '23 03:01

Vishal Sen


I know this is a bit of an old question now but it seems like the accepted answer is quite limited. This is how I got my searchbox added into the toolbar.

.ToolBar(toolBar => toolBar.Template(@<text><input class='k-textbox' value="Search..." onfocus="if (this.value=='Search...') this.value='';" onblur="this.value = this.value==''?'Search...':this.value;" id='searchbox'/></text>))

Then the script

<script type="text/javascript">
    function addSearch() {
        return { searchbox: $('#searchbox').val() };
    }
    $('#searchbox').keyup(function () {
        $('#gridWorkflows').data('kendoGrid').dataSource.read();
    });
</script>

This seems a bit simpler that what you are using currently.
Hope it helps.

like image 29
Mish Avatar answered Jan 24 '23 02:01

Mish