简体   繁体   English

如何在 Ag-grid 中按 ObjectID (_id) 过滤记录

[英]How can I filter records by their ObjectID (_id) in Ag-grid

I'm using MongoDB to store my records and am using Ag-grid react to display them.我正在使用 MongoDB 来存储我的记录,并正在使用 Ag-grid react 来显示它们。 The grid has several columns including the record's ObjectID (_id), name, type, etc. Using the filter agColumnTextFilter works for the name and type fields with the columnDef of:网格有几列,包括记录的 ObjectID (_id)、名称、类型等。使用过滤器 agColumnTextFilter 适用于具有 columnDef 的名称和类型字段:

{
  headerName: 'Column Name',
  field: 'name',
  filter: 'agTextColumnFilter',
},

which then leads to this query setup (using the contains filter option):然后导致此查询设置(使用包含过滤器选项):

case "contains":
  qp[fieldName] = new RegExp(['.*', user input string, '.*'].join(''), 'ig');

this logs the correct query:这记录了正确的查询:

"query db { name: /.*query string.*/gi }"

and the proper rows are displayed.并显示正确的行。 Since the displayed _id is a string as well I tried something similar:由于显示的 _id 也是一个字符串,我尝试了类似的方法:

{
  headerName: 'ID',
  field: '_id',
  valueGetter: (params) => {
    let id = params.data._id;
    return id.slice(id.length-5); //only display last part of ID instead of entire thing
  },
  filter: 'agTextColumnFilter',
},

Using the same contains logic as above the following query is logged to the console:使用与上面相同的包含逻辑,将以下查询记录到控制台:

query db { _id: /.*_id segment.*/gi }

However, no rows are returned in this case (even though there should be rows returned).但是,在这种情况下不会返回任何行(即使应该返回行)。 Do I need to use different logic or is there a problem with this current logic?我需要使用不同的逻辑还是当前的逻辑有问题? Any advice is appreciated.任何建议表示赞赏。

Edit: Turns out even though the ID displays as a String a cast of the string to an ObjectID is needed:编辑:结果即使 ID 显示为字符串,也需要将字符串转换为 ObjectID:

qp[fieldName] = new ObjectID(_id string)

Problem with this is searching for part of a specific ID won't work because it's not a valid ID.问题是搜索特定 ID 的一部分将不起作用,因为它不是有效的 ID。 Searching for a full ID works but isn't ideal either.搜索完整 ID 有效,但也不理想。 If anyone has any ideas on how I can filter just part of the ID I'd appreciate it.如果有人对我如何过滤部分 ID 有任何想法,我将不胜感激。

You can probably set a valueFormatter on the column that displays the partial ObjectId.您可能可以在显示部分 ObjectId 的列上设置 valueFormatter。 The filtering will be done using the full ObjectId.过滤将使用完整的 ObjectId 完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM