简体   繁体   English

Dexie JS - 结合条件过滤器和文本搜索

[英]Dexie JS - Combining Condition Filters and Text Search

I have read a variety of posts and examples on combining and and or statements in a Dexie query, and I'm still confused.我已经阅读了各种关于在 Dexie 查询中组合andor语句的帖子和示例,但我仍然感到困惑。

My tasks query begins by filtering out certain attributes based on the app's UI state.我的tasks查询首先根据应用程序的 UI state 过滤掉某些属性。 For example, I want to view tasks by project, and by whether or not they are completed or deleted.例如,我想按项目查看任务,以及它们是完成还是删除。 These conditions are all indexed when my database is initialized.这些条件在我的数据库初始化时都会被索引。

var where = null

switch(selectedProject){
  case 'inbox': 
    where = { project: '', completed: filterCompleted, deleted: 0 }
    break
  case 'my-tasks':
    where = { assignee: sessionStorage.userId, completed: filterCompleted, deleted: 0 }
    break
  default:
    where = { project: selectedProject, completed: filterCompleted, deleted: 0 }
}

//Ready to go...
tasks = await db.tasks.where(where).sortBy('order')

In addition to these ways of viewing different task states, I also have a text search field for filtering down the remaining tasks in a list.除了这些查看不同任务状态的方法之外,我还有一个文本搜索字段,用于过滤列表中的剩余任务。 I can get it to work great with the following if I omit the above where conditions:如果我省略了上面的where条件,我可以让它在以下情况下很好地工作:

//searchText is the user's input text in the search field
let searchTerms = searchText.split(' ')

tasks = await db.tasks
  .where('name').anyOfIgnoreCase(searchTerms)
  .or('name').startsWithIgnoreCase(searchText)
  .or('notesPlain').anyOfIgnoreCase(searchTerms)
  .sortBy('order')

Where I'm struggling is how to combine the initial UI state's where clause and the above text search.我苦苦挣扎的地方是如何结合初始 UI 状态的where子句和上述文本搜索。 I've tried append this to the above code, but it doesn't work:我已经尝试过 append 到上面的代码,但它不起作用:

...
.sortBy('order')
.filter(function(task){ 
  return task.project == selectedProject && task.completed == filterCompleted && task.deleted == 0
})

Any ideas on how I can solve this?关于如何解决这个问题的任何想法?

Hey you can do it like this:嘿,你可以这样做:

 tasks = (await db.tasks
  .where('name').anyOfIgnoreCase(searchTerms)
  .or('name').startsWithIgnoreCase(searchText)
  .or('notesPlain').anyOfIgnoreCase(searchTerms)
  .sortBy('order'))
.filter(function(task){ 
  return task.project == selectedProject && task.completed == filterCompleted && task.deleted == 0
})

The sortBy returns a Promise so you need to wrap with the await to get the Collection object. sortBy 返回 Promise,因此您需要使用 await 进行包装以获取 Collection object。

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

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