I am trying to get a cell in my row/column grid to display as an empty cell or blank content given a certain case and as a CellRenderer type in another case.
Currently I am doing:
cellRendererSelector: function (params) {
const exampleCheckBox = {
component: 'exampleCheckboxCellRenderer',
params: {values: ['true', 'false']}
};
if (params.data.exampleField) {
return exampleCheckBox;
} else {
return null; // HERE: null sets the string value of that field as a default cell
}
},
In my else case above I want to return or show an empty cell or a cell with an empty string. Returning null only returns a default basic cell with the field value as a string.
The cellRendererSelector is a function that returns the name of the component to be used as a renderer, or null.
You could try either of these two approaches:
Creating a Cell Renderer that returns an empty string, e.g. exampleEmptyRenderer:
if (params.data.exampleField) {
return exampleCheckBox;
} else {
return exampleEmptyRenderer;
}
Inside the exampleCheckBox renderer, add logic to do the checking inside the init method to conditionally return what you want, as you have access to the node data:
ExampleCheckBoxCellRenderer.prototype.init = function (params) {
this.eGui = document.createElement('div');
if (params.data.exampleField) {
// here return your renderer
this.eGui.innerHTML = params.value;
} else {
// here return empty string
this.eGui.innerHTML = ''
}
}
Related
var input = [readLine() ?? ""]
If I just entered, input has [""]
If I do not input anything, I want to make the input an empty list.
How can I do it?
This is because I want the count of the input to be zero when the input is empty.
You can use an IF statement to check if the input was an empty string and if so, then set the input to an empty array. There may be a better way to do this but I think this will work.
if input == [""] {
input = []
}
I hope this helped.
Another way to do this is to define your own function that reads the line or returns an empty array:
private func myReadLine() -> [String] {
let line = readLine()
if line == [""] {
return []
} else {
return line
}
}
And then at the call site you can write:
var input = myReadLine()
Which keeps the logic separated from the calling code and is easier to read. It also has the added advantage of being a lot easier to change if you want to amend your input handling conditions later on.
Simply filter out empty values:
input = input.filter { !$0.isEmpty }
or even:
let input = [readLine()]
.compactMap { $0 } // remove nil
.filter { !$0.isEmpty } // remove empty strings
I have an if statement that checks to see if an array element matches a local variable.
if pinArray.contains(where: {$0.title == restaurantName})
How would I create a variable of this element?
I attempted
let thePin = pinArray.contains(where: {$0.title == restaurantName})
but this comes with "could not cast boolean to MKAnnotation".
I also tried variations of
let pins = [pinArray.indexPath.row]
let pinn = pins(where: pin.title == restaurantName) (or close to it)
mapp.selectAnnotation(thePin as! MKAnnotation, animated: true)
to no avail. What basic step am I missing?
contains(where:) returns a Bool indicating whether a match was found or not. It does not return the matched value.
So thePin is a Bool which you then attempt to force-cast to a MKAnnotation which of course crashes.
If you want the matching value, change your code to:
if let thePin = pinArray.first(where: { $0.title == restaurantName }) {
do {
mapp.selectionAnnotation(thePin, animated: true)
} catch {
}
} else {
// no match in the array
}
No need for contains at all. No need to cast (assuming pinArray is an array of MKAnnotation).
I am downloading information from a Firebase database and it is being inputted via a for loop into:
static var Reports = [String:[String:String]]()
I need to figure out a way to search the inside values for a certain string
I have messed around with this but can't seem to get it inside the inside dictionary (If that makes sense)
for values in Reports.count {
if let item = Reports["favorite drink"] {
print(item)
}
}
I need to have a search string then a number of times the value appears like so:
func findString(dict Dictionary) -> Int {
var ReportsLevel1 = 0
(for loop I'm guessing)
search here for string
return ReportsLevel1
}
Tip: the outside dictionary keys are not set in stone, they depend on what time and date the report was submitted
To find out the numberOfTimes in which "yourSearchString" appears you can do as follows
var numberOfTimes = 0
for internalDictionary in reports.values
{
for value in internalDictionary.values
{
if (value == "yourSearchString") { numberOfTimes += 1 }
}
}
or
let numberOfTimes = reports.flatMap { internalDictsArray in internalDictsArray.value.filter { $0.value == "yourSearchString" } }.count
Goal:
I want to collect the return value of a function.
Question:
How can I call the 'test' function to collect the return variable 'name' without passing through a parameter?
Is there a way to collect a variable(values) from functions with agruments(parameters) without passing through a parameter?
I have provided an example:
let userName = "Jake"
let userInfo = test(name: userName)
func test(name: String) -> String {
return name
}
// function call
// Goal: I want to retrieve the function return value without passing a parameter
let newUser = test()
Does the function 'test' return value have to be stored to retrieve it?
I want to retrieve the 'userName' Jake
You can return like below,
let userName = "Jake" //Global variable of the class
let userInfo = test() //Jake
func test() -> String { //single - element tuple will don't have label for return.
return self.userName
}
If you like to return with labels then you need tow or more return values like below,
func test() -> (name:String,age:Int) {
return (self.userName,125)
}
And access specific values by test().name
I'm trying to change a tuple in an array , however ,when I try
emo = (type:emo.type,strength:increaseStrength(emo.strength))
it gives me error
"cannot assign to 'let' value 'emo'
here is my code :
var emotions : [(type : String, strength: Int)] = [("happy",0),("scared",0),("tender",0),("excited",0),("sad",0)]
func increaseStrength(i:Int)->Int {
switch i {
case 0: return 1
case 1: return 2
case 2: return 3
case 3: return 0
default :return 0
}
}
#IBAction func HappyBA(sender: AnyObject) {
for emo in emotions {
if (emo.type == "happy" ){
emo = (type:emo.type,strength:increaseStrength(emo.strength))
}
}
println(emotions)
}
If there are better way to do the assignment please tell me I am so appreciated ! Thanks..
There is no point assigning to emo even if you could do it. This is not the same as replacing the corresponding object in the array - which is what you do want to do. emo is a copy; even if you were to set a property of it, it wouldn't affect the one back in the array. And certainly setting the variable would not magically read back into the array!
Here's one solution. Instead of cycling thru emotions in your for-loop, cycle thru enumerate(emotions). Now you have a tuple of an index number along with an emotion. If this is the right emotion type, write into the array via the index number.
for (ix,emo) in enumerate(emotions) {
if emo.type == "happy" {
emotions[ix] = (type:emo.type,strength:increaseStrength(emo.strength))
}
}
Or you could use map.
emotions = emotions.map {
emo in
if emo.type == "happy" {
return (type:emo.type,strength:increaseStrength(emo.strength))
} else {
return emo
}
}