Kendo网格过滤无法正常工作

时间:2014-07-17 07:20:41

标签: telerik filtering kendo-grid kendo-dropdown

这是我的MVC视图代码: -

 <div id="reportsDb">

         <div id="grid"></div>
     <script type="text/x-kendo-template" id="template">
            <div class="toolbar" id="template" >
                <label class="Status-label" for="Status">Show reports by status:</label>
                <input type="search" id="Status" style="width: 150px"/>
            </div>
        </script>



  <script>
            $(document).ready(function () {
                var path = ""
                dataSource = new kendo.data.DataSource({

                    transport: {
                        read: {
                            url: "@Url.Action("Report_Read", "Report")",
                            dataType: "json",
                            type: "Get",
                            contentType: "application/json"
                        }

                    },

                    serverPaging: true,
                    serverSorting: true,
                    serverFiltering: true,

                    pageSize: 10,
                    schema: {
                        model: {
                            id: "RequestId",
                            fields: {
                                IPAddress: { type: "string" },
                                RequestQuetime: { type: "date" },
                                RequestPicktime: { type: "date" },
                                RequestRespondTime: { type: "date" },                                    
                                StatusType: { type: "string" },
                                RequestTimeDifference: { type: "datetime", format: "{0:hh:mm:ss}" },
                                RequestPickDifference: { type: "datetime", format: "{0:hh:mm:ss}" }
                            }
                        }
                    }
                });



            var grid =  $("#grid").kendoGrid({                       
                    dataSource: dataSource,
                    sortable: true,
                    pageable: true,
                    filterable: {
                        extra: false,
                        operators: {
                            string: {
                                startswith: "Starts with",
                                eq: "Is equal to",
                                neq: "Is not equal to"
                            }
                        }
                        },
                    toolbar: kendo.template($("#template").html()),
                    height: 430,

                    columns: [
                       { field: "IPAddress", title: "IP address", width: 100, filterable: true },
                       { field: "RequestQuetime", title: "Que time", width: 100, filterable: false },
                       { field: "RequestPicktime", title: "Pick time", width: 110, filterable: false },
                       { field: "RequestRespondTime", title: "Respond time", width: 100, filterable: false },
                       { field: "StatusType", title: "status", width: 110, filterable: { ui: statusFilter } },
                       { field: "RequestTimeDifference", title: "Time difference", width: 110, filterable: false },
                       { field: "RequestPickDifference", title: "Pick difference", width: 110, filterable: false }
                    ]

            });

            function statusFilter(element) {                  
                element.kendoDropDownList({                      
                    dataSource: {
                        transport: {
                            read: {
                                url: "@Url.Action("RequestStatus_Read", "Report")",
                                dataType: "json",
                                type: "Get",
                                contentType: "application/json"
                            }
                        }
                    },
                    dataTextField: "Text",
                    dataValueField: "Value",
                    optionLabel: "--Select Value--"
                });
            }


            });

        </script>
    </div>

以下是控制器的动作方法: -

 public ActionResult Report_Read()
 {          
    return Json(_oRepository.GetReports().ToList(), JsonRequestBehavior.AllowGet);
 }
  
    

我想在StatusType上申请过滤。为此,我已将此文件与下拉列表绑定。

  

我的问题是当我尝试通过从下载状态中选择其中一个状态进行过滤时,它什么都不做。

我按照这个例子工作:

http://demos.telerik.com/kendo-ui/grid/filter-menu-customization

3 个答案:

答案 0 :(得分:0)

从你的代码中,除了Controller Read Action之外,一切似乎都很好。现在,如果从Grid上的视图应用过滤器时调用控制器,那么您所需的唯一更改如下:

 public JsonResult Report_Read([DataSourceRequest] DataSourceRequest request)
 {          
    return Json(_oRepository.GetReports().ToList().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
 }

修改

如果您不使用Kendo.MVC,那么您有两种过滤选项:

选项1:客户端过滤
- &GT;您需要在读取时获取所有数据,以便在应用过滤时获得所有数据,如果数据源不大,这是最佳选择,因为它可以节省不需要的控制器过滤请求。
- &GT;首先想你需要做的是subscirbe来filterMenuInit()的网格,并添加下面的脚本用于客户端过滤。 代码:

    filterMenuInit: function(e) {
     if (e.field == "name") {
          alert("apply Filter");
          var filter = []
          ... // Generate filters
          grid.dataSource.filter(filters);
      }
    }
  

详细示例:Extact from Kendo Examples

选项2:服务器端过滤
- &GT;我对它没有太多的了解,但是当我在搜索我的过滤选项时,我遇到了下面的问题,这对我的应用来说很好但有点复杂。但我认为你可以使用它。

  

JS Fiddle Sample

请参阅以下链接以获取详细说明。

  

参考:JS Kendo UI Grid Options

答案 1 :(得分:0)

检查渲染的html中td中包含的字符串和要过滤的字符串

  1. 查看您的td是否有除您要过滤的字符串以外的任何其他代码。如果情况是td内还有其他一些html代码(例如span或div),则必须重构代码以确保仅在td中具有内容。
  2. 确保在td中修剪字符串。
  3. 尝试包含而不是等于。如果这样可以解决问题,则应该是文字/ html或修剪。

答案 2 :(得分:0)

function applyFilter() {

    var filters = [], item_filters = [], brand_filters = [], invoice_id = null;
    var item_nested_filter = { logic: 'or', filters: item_filters };
    var brand_nested_filter = { logic: 'or', filters: brand_filters };

    var gridData = $("#invoicelistgrid").data("kendoGrid");
    var invoiceId = $("#invoiceidsearch").data("kendoDropDownList").value();    
    var itemId = $("#itemsearch").data("kendoDropDownList").value();
    var brandId = $("#brandsearch").data("kendoDropDownList").value();    
    var partyId = $("#party-dropdown").data("kendoDropDownList").value();

    if (partyId !== "") {
        filters.push({ field: "party_id", operator: "eq", value: parseInt(partyId) });
    }

    if (invoiceId !== "") {
        filters.push({ field: "invoice_id", operator: "eq", value: parseInt(invoiceId) });
    }

    if (itemId !== "") {
        for (var i = 0; i < gridData.dataSource._data.length; i++) {
            var data = gridData.dataSource._data[i].tb_invoice_lines;
            for (var j = 0; j < data.length; j++) {
                if (parseInt(itemId) === parseInt(data[j].item_id)) {
                    item_filters.push({ field: "invoice_id", operator: "eq", value: parseInt(data[j].invoice_id) });
                } else {
                    invoice_id = data[j].invoice_id;
                }
            }
        }
        if (item_filters.length > 0) {
            filters.push(item_nested_filter);
        } else {
            filters.push({ field: "invoice_id", operator: "eq", value: parseInt(invoice_id) });
        }
    }

    if (brandId !== "") {
        for (var k = 0; k < gridData.dataSource._data.length; k++) {
            var brand_data = gridData.dataSource._data[k].tb_invoice_lines;
            for (var l = 0; l < brand_data.length; l++) {
                console.log("Grid item id = " + brand_data[l].brand_id);
                if (parseInt(brandId) === parseInt(brand_data[l].brand_id)) {
                    brand_filters.push({
                        field: "invoice_id",
                        operator: "eq",
                        value: parseInt(brand_data[l].invoice_id)
                    });
                } else {
                    invoice_id = brand_data[l].invoice_id;
                }
            }
        }
        if (brand_filters.length > 0) {
            filters.push(brand_nested_filter);
        } else {
            filters.push({ field: "invoice_id", operator: "eq", value: parseInt(invoice_id) });
        }
    }

    console.log(filters);

    gridData.dataSource.filter({
        logic: "and",
        filters: filters
    });
}