How to obtain from within dataviewjs the value of an inline field that is the result of the execution of an inline code?

What I’m trying to do

Hello everyone:
I just entered the forum for the first time.

I have a problem that I am not able to solve and I have not found it resolved on the forum.
If anyone knows how to do it I would appreciate it if you could tell me.

It’s about the following:

I have an inline field

cost::=159.34 (“miscellaneous”) + 35.8 (“clothes”) + … ;

and I have another inline field

cost_c::$=let C = "";C = dv.current().cost;

Things I have tried

I need to be able to compare the values ​​of cost and cost_c within a dataviewjs code
I assume they are the same but when the following code is executed, the result is that they are different.


if (dv.current().cost==dv.current().cost_c)
{
dv.paragraph("lllllddlldldl")
};

It looks like you’re comparing “159.34(“miscellaneous”) + 35.8(“clothing”) +…” with “$=let C = "";C = dv.current().cost” ( and not with its value after the execution of the code)

Thanks in advance.
BR

When you set an inline field to be equal to a query, it’s not equal to the result of the query vjt is actually the query itself. When viewing the note this isn’t obvious sove you see the result, but it is what’s happening behind the scene.

Therefore when you try to set cost_c, you’re setting it to a query (not result) lifting another query (still not a result), so it’s kind of a double query thingy, and doesn’t give you the result you’re aiming for.

If you want to store the result of a dataview query, you’ll need to use some variant of app.fileManager.processFrontMatter(), and it require some extra coding.

In some cases it’s just easier to repeat the query itself in the next context/script… But that depends on what your larger use case is.

Thank you very much for the reply. The only thing I want is to stop the execution of the query that I make in dataviewjs because it is an intense load. I only want it to be done when the cost and cost_c fields are different.
I know I could put a field, called for example exec: 0 or exec:1 to stop the execution or not, but I would prefer to do it another way.
I don’t know if there is a better way to do it

Some alternatives related to not running dataview queries all the time.

Turn off automatic view refreshing

You can turn refreshing off, by disabling at Settings > Dataview > View Settings > Automatic View Refreshing. However, this is a vault wide decision, so now you need to trigger a refresh manually for any dataview query.

This requires manually refreshing, or rebuilds of any view. You could then use commands to rebuild the views. (Which do require some manual configuration, but it’s very doable)

Using a flag variable

As you suggested yourself, you could use a flag variable to indicate whether the query should run or not. This flag variable could be handled manually, or automatically. In the latter case one could envision setting the flag variable to a timestamp, thusly allowing for the query to run if the time since the last run passes some threshold.

The major downside with this approach, is that it’ll not show any result if the flag variable/condition is met. Then it’ll just display whatever alternate text you have specified. (See also last section for the special case of a single result from your query)

Using templates to insert query result

Another option, building on the manual re-run principle is to use a template to insert your query result. This will enable you to insert the query result, and keep it showing until you re-insert the template (after or at the same as deleting the old result).

One downside to this method, is that you need to make template for each query you want to use in this particular fashion. This could possibly be automated through using some name scheme, so that if you’ve got a general template for re-running queries, they could look for your “note name” + some postfix to find the query. Using some specific comments to tag where the query result should go, could also automate what to remove.

Using persisted queries

This is to me, the best option, but sadly it’s not implemented as of Dataview 0.5.64. The gist of the idea here is that you run the query, and get the result inserted into your page, and at the same time keep a record of what the original query was.

The advantage of this (and the previous suggestion using templates), is that since the result is inserted into the note itself, you suddenly get backlinks to work, and when searching it’ll return hits within your result.

The major downside is that you’ll need to implement this persisting yourself. I’ve started dabbling with a version using comment tags to encapsulate the query and result, but I’ve not released it into the public yet as it’s way to volatile.

Store in frontmatter if single result

All of the above suggestions are related to larger queries where the result could be a list or table, and so on. If your result is a single (or a few) value(s), you might use a variant using flag variables, where when you run the query you store the result in the frontmatter using app.fileManager.processFrontMatter().

In this scheme, your output could then be the output of that value, and it wouldn’t depend on the query to actually run as such. Here one could also consider three variants of a flag variable:

  • If your intended value is not present, run the query (and if you want to rerun it, you delete the value before viewing the note)
  • A simple flag variable which you could handle manually
  • A timed flag variable set whenever your query runs, and possibly check for a time threshold of when to rerun the query

That latter variant has the advantage that you’ll get to see when your value was calculated, and you could most likely quickly decide whether the calculated value is usable or you need to re-run the query.

The first variant is the easiest to implement, as your query then simply needs to check if the value is present or not, and if not present run the query. But you do loose the time specification of when the query ran the last time.

1 Like

Thank you very much holroy

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