在GridMVC中创建自定义时间小部件

时间:2014-10-10 10:14:31

标签: javascript jquery mvcgrid

我正在尝试创建Time小部件来在Grid MVC中按时间过滤记录。我能够看到控件,但它不会过滤任何记录。这是脚本。我添加了脚本,mvc网格的视图代码和索引页面。

脚本

function TimeWidget() {

this.getAssociatedTypes = function () {
    return ["TimeFilterWidget"];
};
this.onShow = function () {
};

this.showClearFilterButton = function () {
    return true;
};

this.onRender = function (container, lang, typeName, values, cb, data) {
    //store parameters:
    this.cb = cb;
    this.container = container;
    this.lang = lang;

    //this filterwidget demo supports only 1 filter value for column column
    this.value = values.length > 0 ? values[0] : { filterType: 1, filterValue: "" };

    this.renderWidget(); //onRender filter widget
    //this.loadCustomers(); //load customer's list from the server
    this.registerEvents(); //handle events
};

this.renderWidget = function () {
    var html = '\<p>Select Time to filter:</p>\
                <p><input class="time" type="time" id="tminput"/></p>\
                ';
    this.container.append(html);
};

this.registerEvents = function () {                
    var x = document.getElementById("tminput");
    x.addEventListener("blur", myBlurFunction, true);        
};

function myBlurFunction() {
    var $context = this;
    var values = [{ filterValue: $(this).val(), filterType: 1 /* Equals */ }];        
    $context.cb(values);
}

}

网格视图

@using GridMvc.Html
@using GridMvc.Sorting
@model IEnumerable<GridMvc.Site.Logging>

@Html.Grid(Model).Named("ordersGrid").Columns(columns =>
{
    /* Adding "OrderID" column: */

    columns.Add(o => o.ID)
           .Titled("Number")
           .SetWidth(100);
    columns.Add(o => o.DateTime, "Date")
            .Titled("Date")
            .SortInitialDirection(GridSortDirection.Descending)
            .Format("{0:dd/MM/yyyy}")
            .SetWidth(110);
    columns.Add(o => o.DateTime, "Time")
            .Titled("Time")
            .SortInitialDirection(GridSortDirection.Descending)
            .Format("{0:hh:mm tt}")                
            .SetWidth(110)
            .SetFilterWidgetType("TimeFilterWidget");
    columns.Add(o => o.Type)
           .Titled("Type")
           .SetWidth(150)
           .ThenSortByDescending(o => o.ID)
           .SetFilterWidgetType("TypeFilterWidget");
    columns.Add(o => o.Description)
           .Titled("Description")
           .SetWidth(250);
    columns.Add(o => o.Reference)
            .Titled("Reference")
            .SetWidth(150)
            .Css("hidden-xs"); //hide on phones
    columns.Add(o => o.Response)
            .Titled("Response")
            .SetWidth(150)
            .Css("hidden-xs"); //hide on phones        


}).WithPaging(15).Sortable().Filterable().WithMultipleFilters()

索引视图

<script>
$(function () {
    pageGrids.ordersGrid.addFilterWidget(new TypeWidget());
    pageGrids.ordersGrid.addFilterWidget(new TimeWidget());
});
</script>

1 个答案:

答案 0 :(得分:2)

如果那时可能有所帮助。 Nead在网页上添加此代码。 的 .Named( “ordersGrid”) 示例代码:

@Html.Grid(Model).Named("ordersGrid").Columns(columns =>
{
    columns.Add(o => o.Customer.CompanyName)
           .Titled("Company")
           .SetWidth(250)
           .ThenSortByDescending(o => o.OrderID)
           .Filterable(true)
           .SetFilterWidgetType("CustomCompanyNameFilterWidget"); })

这个工作中的JavaScrip方法addFilterWidget在文件gridmvc.customwidgets.js

我希望它有所帮助。