Can I get a field from a list of maps using a variable name instead of a specific field name - flutter

I have a list of maps (bText) and the map of field names include (kjvtext, nasbtext, esvtext) string fields.
I have another preferences variable: String bVersion = "esvtext";
I want to access the map field bText[index].esvtext when the bVersion field contains "esvtext" and field bText[index].nasbtext when the bVersion field contains "nasbtext".
I've tried several combinations of code such as:
Text(bText[index].bVersion)
Text(bText[index].[bVersion])
Text(bText[index]."$bVersion")...
I've finally resorted to:
xText = (bVersion == "kjvtext")
? verselist[index].kjvtext
: (bVersion == "nasbtext")
? verselist[index].nasbtext
: (bVersion == "esvtext")
? verselist[index].esvtext
: "bVersion version error\n",
Seems like there would be a simpler way to code this?
Any thoughts? Thanks in advance!

Related

How to override Ag Grid QuickFilter to receive exact match results?

By default Ag Grid Quick Filter function return rows that contains search string. For example if I type "30 June" in the searchbox, quick filter will also return rows that contains "30 cars were sold by 2 June" text. How can I override default behavior to receive only rows that exactly match my search string?
What I did was the following:
In the search itself, I removed the spaces from the search criteria:
this.gridApi.setQuickFilter(event.toLowerCase().replace(" ", ""));
In each column that I wanted an exact match, I added this code in the column definition:
getQuickFilterText: (params) => { return params.value && params.value.toLowerCase().replace(" ", "");}
(That is the override method for search. See here for more details: https://www.ag-grid.com/angular-data-grid/filter-quick/)
It seems to be working for me.
To achieve the exact match results column wise, You have to do these two things :
Remove cacheQuickFilter property from your default column definition object in gridOptions as caching convert all the columns data into a string separated by backward slash. That's the reason it will not be able to search column by column.
Add getQuickFilterText function in each column definition and add a condition for a exact match else return an empty string.
getQuickFilterText: params => {
return (params.value === <quick filter value>) ? params.value : ''
}
Now the tricky part here is how to access quick filter value inside getQuickFilterText function. You can achieve this in two ways :
Assign an id to quick filter search element and then access it's value using document.getElementById('quick-filter').value
Store the quick filter search value on change and put into a store state or service and then access that inside getQuickFilterText function.

Apply filtering with an array of value instead of one value for a filter in Algolia

I have in my index a list of object, each of them has an objectID value.
On some search, i want to filter OUT a certain number of them, using there objectID.
For the moment it works with one value as a string, i would like to know how to do for multiple value.
filters = 'NOT objectID:' + objectIDToFilter;
This work for one object, what can i do to apply this for an array of ObjectID. because :
filters = 'NOT objectID:' + arrayObjectID;
does not work.
I was thinking of generating a huge string with an arrayId.map with all my 'NOT objectID:1 AND NOT objectID: 2 ...' but i wanted to know if there is a cleaner way to do it.
I unfortunately misunderstood the line in algolia doc :
Array Attributes: Any attribute set up as an array will match the filter as soon as one of the values in the array match.
This apparently refers to the value itself in Algolia and not the filter
So i did not found a solution on algolia doc, i went for the long string, hope there is no limits on how much filter we can add on a query (found nothing about that).
Here is what i did if someone need it :
let filters = `NOT objectID:${userID}`;
blockedByUsers.map((blockedByUser) => {
filters = filters + ` AND NOT objectID:${blockedByUser}`;
});
If you need to add multiple but don't have a starting point like i do, you can't start the query with an AND , a solution i found to bypass that:
let filters = `NOT objectID:${blockedByUsers[0]}`;
blockedByUsers.map((blockedByUser, i) => {
if (i > 0) filters = filters + ` AND NOT objectID:${blockedByUser}`;
});
There is probably a cleaner solution, but this work. If you have found other solution for that problems i'll be happy to see :)

Postgres jsonb_set concatenate current value

I'm trying to use jsonb_set to update a range of json objects within my database. I can get a query working that updates the object with a string value, however I cannot seem to get it to update using the current value.
UPDATE entity
SET properties = jsonb_set(properties, '{c_number}', concat('0', properties->>'c_number'))
WHERE type = 1 and length(properties->>'c_number') = 7
The above doesn't work in its current format, I think the issue is the properties->>'c_number' inside the jsonb_set. Is there a way I can access the current value and simply add a leading 0?
Found a solution:
UPDATE entity
SET properties = jsonb_set(properties, '{c_number}', concat('"0', properties->>'c_number', '"')::jsonb)
WHERE type = 1 and length(properties->>'c_number') = 7
Based on this answer I was able to prepare my solution.
My goal was to create a new property in JSON, with a value that is based on the value of one of the properties which my JSON already has.
For example:
I have:
{
property_root: { property_root_child: { source_property_key: "source_property_value" } }
}
I want:
{
property_root: { property_root_child: { source_property_key: "source_property_value", target_property_key: "source_property_value + my custom ending" } }
}
So my query would look:
UPDATE database.table_with_json
SET json_column=jsonb_set(
json_column,
'{ property_root, property_root_child, target_property_key }',
concat('"', json_column->'property_root'->'property_root_child'->>'source_property_key', ' + my custom ending', '"')::jsonb)
WHERE
json_column->'property_root'->'property_root_child'->'source_property_key' IS NOT NULL
Why concat looks messy? Based on the answer mentioned above:
The third argument of jsonb_set() should be of jsonb type. The problem is in casting a text string to jsonb string, you need a string in double quotes.
That is why we have to wrap concat in double qoutes.

Is it possible to change the field name in user storage with a string?

If you write conv.user.storage.something = "test" I understand that, but can you change the field name "something" in this example. Say you had an array of values named Fruit ["Banana", "Apple"] can you read the array and then assign those values to the field name value. It seems like you can only set the field name manually such as conv.user.storage.Banana but what if I am getting those names from an array?
Yes you can iterate through the array and directly assign the keys.
for (const element of array) {
conv.user.storage[element] = "text"
}

Default value for null fields in a Jasper Report

Background
A ResultSet has many Double value fields (with patterns like "###0.000"). Some values can be null.
Problem
I want to replace null values with "N/A", which is a String and cannot print to a Double field. Printing "0.00" for null values is unacceptable.
Using an PrintWhenExpression value of ($F{value} != null) ? $F{value} : "N/A" does not work; it is not possible to use patterns in that way.
Idea
Add hidden fields that write "N/A". These fields will be printed only if value is null.
Question
Is there a better solution, and if so, what is it?
Thank you.
Solution #1
Your solution:
Use a regular Double field (doubleField) for the column value.
Add a static String text field at the same location.
Change the Double field to Blank When Null.
Set the PrintWhenExpression value for the String text field to: $F{doubleField} == null.
Solution #2
The problem is, as you pointed out, that a Double and a String are two different data types. You can assign a String variable to the value of the Double using an appropriate expression. Then use the String variable as the field. The expression might resemble:
($F{doubleField} == null) ?
"N/A" : new java.text.DecimalFormat("#.##").format($F{doubleField})
(Note: My preference is to use == instead of !=. Think positive.)
Solution #3
Change the SQL statement to pre-format the Double as a text string, and use the "N/A" in the string (by using a CASE or DECODE statement in the query).
Avoid this solution, though, as it is not maintainable.
Recommendation
Do not hard-code the "N/A" string throughout the report(s); put the "N/A" text in a constant, or a parameter with a default value of "N/A".
You can try something like this in your field expression:
("Your static text "+(($F{field}!=null)?$F{field}:""))
Or, if you don't want your static text to be visible when the field is null, try putting $F{field}!=null in your PrintWhenExpression.