Kendo / JQuery搜索框不搜索`ICollection`?(Kendo/JQuery searchbox doesn't search through `ICollection`?

编程入门 行业动态 更新时间:2024-10-25 02:29:58
Kendo / JQuery搜索框不搜索`ICollection`?(Kendo/JQuery searchbox doesn't search through `ICollection`?)

我有一个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 } ] } });

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

I have a Kendo grid, and in the .ToolBar I have a search box that filters the grid based on what is typed in and overall, it is working fine.

The issue is that it is not reading/searching through particular columns (Maker & Associated) which involve the use of ICollection. I have tried a few things but I can not figure out how to write the search correctly for these columns.

I'll note that the Maker & Associated columns themselves are being populated correctly and that each of the columns' own filters also work correctly

First, this is a stripped down example of how the columns and the search box are created in the 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>

Search section of controller

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

Here is the applicable code in 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; }

If I left something out, or if I was unclear at any point, please comment and I will do my best to clarify.

Thank you

Update: I found a slight workaround for the search box which successfully searches through the columns.

//$('#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 } ] } });

However, this code doesn't behave as I need it to. This types into all of the filter boxes at the top of each row which is not acceptable; I want any previous filtering using the individual filters to persist.

最满意答案

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

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

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

Solved it! The line I needed in my Controller.cs was:

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

The problem with my failed attempts was that .Select is a System.Collections.Generic.IEnumerable<bool> which the || operator doesn't work with. The .Any solves this because it determines if any element satisfies the condition.

更多推荐

Name,搜索,Maker,电脑培训,计算机培训,IT培训"/> <meta name="description&quo

本文发布于:2023-07-24 01:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JQuery   Kendo   ICollection   search   searchbox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!