Kendo / JQuery搜索框没有通过`ICollection`搜索?

时间:2015-07-30 06:37:51

标签: c# ajax kendo-ui kendo-grid kendo-asp.net-mvc

我有一个Kendo网格,在.ToolBar我有一个搜索框,根据输入的内容和整体来过滤网格,它运行正常。

问题在于它没有阅读/搜索涉及使用ICollection的特定列(Maker& Associated)。我已经尝试了一些东西,但我无法弄清楚如何正确地为这些列编写搜索。

我会注意到Maker&正确填充了Associated列本身以及每个列的列数。自己的过滤器也可以正常工作

首先,这是一个关于如何在Index.cshtml

中创建列和搜索框的精简示例
@(Html.Kendo().Grid(Model).Name("gridWF")
.Columns(columns =>
    {
        column.Bound (p1 => p1.Id.Filterable(ftb => ftb.Cell(cell => cell.ShowOperators(true).InputWidth(90))).Width(150);
        column.Bound (p1 => p1.Name.Filterable(ftb => ftb.Cell(cell => cell.ShowOperators(true).InputWidth(90))).Width(150);
        column.Bound (p1 => p1.Item.Filterable(ftb => ftb.Cell(cell => cell.ShowOperators(true).InputWidth(90))).Width(150);
        column.Bound (p1 => p1.Maker.Filterable(ftb => ftb.Cell(cell => cell.ShowOperators(true).InputWidth(90))).Width(150);
        column.Bound (p1 => p1.Associated.Filterable(ftb => ftb.Cell(cell => cell.ShowOperators(true).InputWidth(90))).Width(150);
    }
.Pageable()
.ColumnMenu()
.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>)
.DataSource(datasource => datasource.Ajax()
  .PageSize(15)
  .ServerOperation(false)
  .Read(r => r.Action("Read", "Workflows").Data("addSearch"))
  )  

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

控制器的搜索部分

private IEnumerable<WorkflowViewModel> GetData(string searchbox = "")
        {
        var number = 0;
        int.TryParse (searchbox, out number);
        var result = (from w in Repo.GetWF()
                      where w.Id == number ||
                            w.Name.ToLower ().Contains (searchbox.ToLower ()) ||
//Failed attempt            w.UserRoles.Where (ur => ur.Role.Name.ToLower ().Contains (searchbox.ToLower())) != null || 
//Failed attempt            w.UserRoles.Where (ur => ur.Role != null && wr.Role.Name.ToLower().Equals ("maker", StringComparison.InvariantCultureIgnoreCase)).Select (wr => wr.UserName).FirstOrDefault ().Contains (searchbox.ToLower()) ||
                            w.ImportProduct.Item.Name.ToLower ().Contains (searchbox.ToLower ()) ||
                            searchbox == ""
                     select new ViewModel (w, User.Identity.Name)).ToList();
        return result;
        }

以下是ViewModel.cs

中适用的代码
public class ViewModel

  public ViewModel(Workflow workflow, string userName = null)
    {
    Id = workflow.Id;  
    Name = workflow.Name;  
    Item= workflow.ImportProduct.Item.Name  
    Maker= Workflow.GetByRoles(workflow, "maker");  
    Associated= Workflow.GetByRoles(workflow, "associated");  
    }

Workflow.cs

public class Workflow
    {
    public Workflow()
        {
        UserRoles = new List<UserRole>();
        }

        [Key]
        public int Id
            {
            get;
            set;
            }

        [Required(ErrorMessage="*")]
        [Display(Name="Name")]
        public string Name
            {
            get;
            set;
            }

        [Required]
        public int ImportId
            {
            get;
            set;
            }


        [ForeignKey("ImportId")]
        public virtual Product ImportProduct 
            {
            get;
            set;
            }

    public ICollection<UserRole> UserRoles 
        {
        get;
        set;
        }

    public static string GetByRoles (Workflow workflow, string role)
        {
        string userString = "";
        if(workflow.UserRoles != null)
            {
            var query = from ur in workflow.UserRoles
            where ur.Role.Name.ToLower() == role.ToLower()
            select ur.User.DisplayName;

            foreach(string user in query.ToList()
            {
            usersString += user + ",";
            }
            usersString = usersString.Trim(' ');
            usersString = usersString.Trim(',');
            }
        return usersString;
        }

如果我遗漏了某些内容,或者我在任何时候都不清楚,请发表评论,我会尽力澄清。

谢谢

更新
我找到了一个成功搜索列的搜索框的轻微解决方法。

//$('#searchbox').keyup(function () {
//    $('#gridWF').data('kendoGrid').dataSource.read();
//});
$('#searchbox').keyup(function () {
    var q = $("#searchbox").val();
    var int = parseInt(q); 
    var grid = $("#gridWF").data("kendoGrid");
    grid.dataSource.filter({
        logic: "or",
        filters: [
              { field: "Id", operator: "eq", value: int},
              { field: "Name", operator: "contains", value: q},
              { field: "Item", operator: "contains", value: q},
              { field: "Maker", operator: "contains", value: q },
              { field: "Associated", operator: "contains", value: q }
            ]
        }
    });  

但是,此代码并不像我需要的那样运行。这会在每行顶部的所有过滤器框中输入,这是不可接受的;我想要使​​用单个过滤器的任何先前过滤来保持。

1 个答案:

答案 0 :(得分:0)

解决了!
Controller.cs中需要的一行是:

`w.UserRoles.Where (ur => ur.Role.Name.ToLower ().Contains ("maker")).Any (ur => ur.User.Name.ToLower ().Contains (searchbox.ToLower ())) ||` 

我尝试失败的问题是.SelectSystem.Collections.Generic.IEnumerable<bool> ||运营商无法使用的问题。 .Any解决了这个问题,因为它确定是否有任何元素满足条件。