Get task type in dataviewjs

Good day!
I am trying to use dataviewjs for filtering tasks.
I wrote workaround for ignoring not TODO tasks with status symbol:

dv.taskList(dv.pages().file.tasks
    .where(t => {
    if(t.completed) return false;
    if(t.status != " ") return false; //not a task workaround
    // How to write something like this if(t.type != "TODO") return false;
    // handle task
    }),
false)

Could you help me write more readable (and easier to maintain) code where the task type is explicitly compared to the TODO type? (something like if(t.type != “TODO”)…)

How are you writing your tasks? What does that “TODO” refer to?

In order to just list tasks which are not done and have an open status, the following should suffice:

```dataviewjs
dv.taskList(dv.pages().file.tasks
  .where(t => !t.completed && t.status = " ")
)
```

But this doesn’t do anything related to that mysterious “TODO” thingy…

Bonus tip: How to present code properly in a forum post

If you want to showcase either markdown, or code blocks, or dataview queries properly in a forum post, be sure to add one line before and one life after what you want to present with four backticks, ````. This will ensure that any other backticks (like for code blocks) is properly shown.

1 Like

I want to use this status type link in dataviewjs

I’ll try to ask the question differently.
Through tasks request, we can get TODO tasks.

```tasks
status.type is TODO
```

How to do the same with dataviewjs?

The actual status.type is something only available within a tasks query. However, you can decipher what they are by looking in Settings > Tasks (Task Statuses). Here you’ll see the Core Statuses and potentially any Custom Statuses. An example of a setup is shown below:

So the answer to you question is both yes and no. No, you can’t check for status.type = TODO or similar, but you can check what the status of a given task is using dataview.

Given the setup above, we can see the following groups of statuses:

  • TODO: , /
  • IN_PROGRESS: /
  • CANCELLED: -
  • DONE: x

To check for the last three you could simply do t.status = "x" or similar. For the groups, you need to do it slightly different. Lets give some examples:

### DQL - task
```dataview
TASK
WHERE file.name = this.file.name
WHERE contains(list(" ", "/"), status)
```

### DQL - table
```dataview
TABLE WITHOUT ID T.text, T.type, T.status
FLATTEN file.tasks as T
WHERE file.name = this.file.name
WHERE contains(list(" ", "/"), T.status)
```

### Dataviewjs variant
```dataviewjs

const tasks = dv.current()
  .file.tasks
  .where(t => [' ', '/'].includes(t.status))
  
dv.taskList(tasks)
```

All these three variant will pull tasks from the current file, having a status of either or /. If you feel like it, you could of course extend these lists further and include other statuses. For example some like to include the BuJo markers < & > into the TODO list. And depending on personal preferences the -, aka cancelled, could be considered both DONE and NOT DONE…

Suggestion on using literals in dataview

The literals used by tasks are not readily available within dataview, and the bare lists could possibly not look as good, so here are two alternatives to define these lists:

## Within dataviewjs

```dataviewjs

const TODO = [' ', '/', '<', '>']
const DONE = ['x', '-']

const tasks = dv.current()
  .file.tasks
  .where(t => TODO.includes(t.status))
  
dv.taskList(tasks)
```

## Within dataview

```dataview
TASK
FLATTEN list(" ", "/", "<", ">") as TODO
WHERE file.name = this.file.name
WHERE contains(TODO, status)
```

and if you wanted to check for DONE, you could exchange the  query to
```dataview
TASK
FLATTEN list(" ", "/", "<", ">") as DONE
WHERE file.name = this.file.name
WHERE contains(DONE, status)
```

In summary, the groups defined by tasks can’t be used directly within dataview queries, as far as I know, but they’re defined by the content of the status field, and this field you can check from both DQL and dataviewjs queries.

PS! In all of my queries I use variant of dv.current() or WHERE file.name = this.file.name to limit the query to the current test file. You will of course need to change this to match your base task lists.

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.