How to access Delete Row function in a Multivalued row for a Eureka form - swift

I would like to access the delete row function in the eureka form but I couldn't find how to do it. The reason is that I have an i variable that keeps track of how many rows I have and because of that my form breaks when a row is deleted.
This is how my implementation currently looks in my code:
+++ MultivaluedSection(multivaluedOptions: [.Reorder, .Insert, .Delete], header: "Exercises", footer: "List of Exercises for workout"){
$0.addButtonProvider = {section in
return ButtonRow(){
$0.title = "Add Another Exercise"
self.i+=1
}
}
$0.multivaluedRowToInsertAt = { index in
return TextRow() {
$0.title = "Exercise \(self.i) Name"
$0.tag = "Exercise \(self.i) Name"
self.i+=1
}
}
$0 <<< TextRow(){ //change this to SuggestionRow later
$0.title = "Exercise \(self.i) Name"
$0.tag = "Exercise \(self.i) Name"
$0.add(ruleSet: strictRules)
}
}
What I'm looking for is something like:
$0.multivaluedRowToDeleteAt = { index in
self.i-=1
}
Does something like this exist? If not, is there any other way to get the number of rows for this multivalued section at submission time?

Related

find({}) doesn't return correct result [duplicate]

I imported some sort-of sloppy XML data into a Mongo database. Each Document has nested sub-documents to a depth of around 5-10. I would like to find() documents that have a particular value of a particular field, where the field may appear at any depth in the sub-documents (and may appear multiple times).
I am currently pulling each Document into Python and then searching that dictionary, but it would be nice if I could state a filter prototype where the database would only return documents that have a particular value of the field name somewhere in their contents.
Here is an example document:
{
"foo": 1,
"bar": 2,
"find-this": "Yes!",
"stuff": {
"baz": 3,
"gobble": [
"wibble",
"wobble",
{
"all-fall-down": 4,
"find-this": "please find me"
}
],
"plugh": {
"plove": {
"find-this": "Here too!"
}
}
}
}
So, I'd like to find documents that have a "find-this" field, and (if possible) to be able to find documents that have a particular value of a "find-this" field.
You are right in the certain statement of a BSON document is not an XML document. Since XML is loaded into a tree structure that comprises of "nodes", searching on an arbitary key is quite easy.
A MonoDB document is not so simple to process, and this is a "database" in many respects, so it is generally expected to have a certain "uniformity" of data locations in order to make it easy to both "index" and search.
Nonetheless, it can be done. But of course this does mean a recursive process executing on the server and this means JavaScript processing with $where.
As a basic shell example, but the general function is just a string argument to the $where operator everywhere else:
db.collection.find(
function () {
var findKey = "find-this",
findVal = "please find me";
function inspectObj(doc) {
return Object.keys(doc).some(function(key) {
if ( typeof(doc[key]) == "object" ) {
return inspectObj(doc[key]);
} else {
return ( key == findKey && doc[key] == findVal );
}
});
}
return inspectObj(this);
}
)
So basically, test the keys present in the object to see if they match the desired "field name" and content. If one of those keys happens to be an "object" then recurse into the function and inspect again.
JavaScript .some() makes sure that the "first" match found will return from the search function giving a true result and returning the object where that "key/value" was present at some depth.
Note that $where essentially means traversing your whole collection unless there is some other valid query filter than can be applied to an "index" on the collection.
So use with care, or not at all and just work with re-structring the data into a more workable form.
But this will give you your match.
Here is one example, which I use for recursive search for Key-Value anywhere in document structure:
db.getCollection('myCollection').find({
"$where" : function(){
var searchKey = 'find-this';
var searchValue = 'please find me';
return searchInObj(obj);
function searchInObj(obj){
for(var k in obj){
if(typeof obj[k] == 'object' && obj[k] !== null){
if(searchInObj(obj[k])){
return true;
}
} else {
if(k == searchKey && obj[k] == searchValue){
return true;
}
}
}
return false;
}
}
})

MongoDB - query embedded documents [duplicate]

I imported some sort-of sloppy XML data into a Mongo database. Each Document has nested sub-documents to a depth of around 5-10. I would like to find() documents that have a particular value of a particular field, where the field may appear at any depth in the sub-documents (and may appear multiple times).
I am currently pulling each Document into Python and then searching that dictionary, but it would be nice if I could state a filter prototype where the database would only return documents that have a particular value of the field name somewhere in their contents.
Here is an example document:
{
"foo": 1,
"bar": 2,
"find-this": "Yes!",
"stuff": {
"baz": 3,
"gobble": [
"wibble",
"wobble",
{
"all-fall-down": 4,
"find-this": "please find me"
}
],
"plugh": {
"plove": {
"find-this": "Here too!"
}
}
}
}
So, I'd like to find documents that have a "find-this" field, and (if possible) to be able to find documents that have a particular value of a "find-this" field.
You are right in the certain statement of a BSON document is not an XML document. Since XML is loaded into a tree structure that comprises of "nodes", searching on an arbitary key is quite easy.
A MonoDB document is not so simple to process, and this is a "database" in many respects, so it is generally expected to have a certain "uniformity" of data locations in order to make it easy to both "index" and search.
Nonetheless, it can be done. But of course this does mean a recursive process executing on the server and this means JavaScript processing with $where.
As a basic shell example, but the general function is just a string argument to the $where operator everywhere else:
db.collection.find(
function () {
var findKey = "find-this",
findVal = "please find me";
function inspectObj(doc) {
return Object.keys(doc).some(function(key) {
if ( typeof(doc[key]) == "object" ) {
return inspectObj(doc[key]);
} else {
return ( key == findKey && doc[key] == findVal );
}
});
}
return inspectObj(this);
}
)
So basically, test the keys present in the object to see if they match the desired "field name" and content. If one of those keys happens to be an "object" then recurse into the function and inspect again.
JavaScript .some() makes sure that the "first" match found will return from the search function giving a true result and returning the object where that "key/value" was present at some depth.
Note that $where essentially means traversing your whole collection unless there is some other valid query filter than can be applied to an "index" on the collection.
So use with care, or not at all and just work with re-structring the data into a more workable form.
But this will give you your match.
Here is one example, which I use for recursive search for Key-Value anywhere in document structure:
db.getCollection('myCollection').find({
"$where" : function(){
var searchKey = 'find-this';
var searchValue = 'please find me';
return searchInObj(obj);
function searchInObj(obj){
for(var k in obj){
if(typeof obj[k] == 'object' && obj[k] !== null){
if(searchInObj(obj[k])){
return true;
}
} else {
if(k == searchKey && obj[k] == searchValue){
return true;
}
}
}
return false;
}
}
})

Mongo Shell print is not displaying?

So I am trying to compare a simple comma delimited list to the documents in my collection. This is my code:
var file = cat("Price Level V.csv");
var skus = file.split("\n");
for(var i = 0; i < skus.length; i++) {
var vasku = skus[i].split(',');
db.getCollection('skus').findOne({sku:vasku[0]}, function(err, mydoc) {
if(err)
print(err);
if(mydoc == null) {
print('NF');
} else if(mydoc.VA == vasku[1]) {
print('Correct');
} else {
print('Incorrect');
}
});
}
For some reason, I am not seeing anything pop up in the shell for all my print statements. It should at least print 'Incorrect', right?
If the loop is entered and the skus collection is not empty, then this can happen if you misspell the collection name of the model that You try to query (I see that from time to time when someone writes the collection name in camelCase).
It's a long shot but maybe the model name in the db is actually skuss (second 's' added for plural form)?

Eureka Dynamically Hide/Show Rows

I have a option field that I created using Eureka. I want to create new fields according of the answer of my first option and show new row accordingly.
form +++ Section("First Section")
<<< ActionSheetRow<String>() {
$0.title = "Select Option"
$0.selectorTitle = "1 or 2"
$0.options = ["1","2"]
}
For example, if I want to show new row according to the answer of the first question; so different form field row for 1 and 2, which was initially hidden. How can it be achieved?
So I want the flow to be: If 1 selected, show second question which is option select as well. If 2 selected show option with different set of options. But don't show that row before first option value is selected.
I had been working in your questions, this are my results, you need to setup a tag for your section and then search in the form with tag get the section and add another cell according your needs, this code can help you
<<< ActionSheetRow<String>() {
$0.title = "Select Option"
$0.selectorTitle = "1 or 2"
$0.options = ["1","2"]
$0.onChange({ (row) in
debugPrint("\(row.value) was selected")
if let section = self.form.sectionBy(tag: "first") {
let actionSheetRow = ActionSheetRow<String>()
actionSheetRow.title = "Select Option2"
actionSheetRow.title = "2 or 3"
actionSheetRow.options = ["2","3"]
actionSheetRow.onChange({ (row2) in
debugPrint("\(row2.value) was selected")
})
section.append(actionSheetRow)
section.reload(with: .none)
}
})
}
I hope this helps you, best regards

Detect empty pick() from hash in KRL

I have a bunch of data in a hash and I am picking from it. Sometimes there will be data there to pick and sometimes there won't. What is the best way to know when there was something found by the pick operator and when there wasn't so I can react to that in my code?
The pick operator will take a second optional parameter that will make it so that it always returns the results in an array. This means that if something is picked, the length of the array will be greater than 0, otherwise it will be 0. You can then use that to do what you are wanting to do.
Example code/app taken from http://kynetxappaday.wordpress.com/2011/01/04/day-30-detecting-empty-pick/
ruleset a60x526 {
meta {
name "hash-pick-detect"
description <<
hash-pick-detect
>>
author "Mike Grace"
logging on
}
global {
dataHash = {
"one": {
"name": "Mike"
}, // number
"two": {
"random": 8
}, // number
"three": {
"name": "Alex"
} // number
}; // dataHash
} // global
rule detect_the_pick {
select when pageview ".*"
foreach dataHash setting (key, value)
pre {
userName = value.pick("$.name", true);
length = userName.length();
}
if (length > 0) then {
notify("Key: #{key}","Name: #{userName}<br/>Length: #{length}") with sticky = true;
}
notfired {
raise explicit event empty_pick
with pickedKey = key;
}
}
rule empty_pick_found {
select when explicit empty_pick
pre {
pickedKey = event:param("pickedKey");
results =<<
Key: #{pickedKey}<br/>
doesn't have a name associated with it to pick from
>>; //' fixing syntax highlighting
}
{
notify("An empty pick was detected",results) with sticky = true;
}
}
}