Using {{each}} block to iterate through an array backwards in DerbyJS - derbyjs

In a DerbyJS component, I have an array items, which I want to display in the view. I want to display the latest items of the array first, so I am looking for something like each-reverse:
{{each-reverse items as #item}}
<p>{{#item}}</p>
{{/each-reverse}}
. Of course, this snippet of code does not work but I hope it explains what I want to achieve. Is there a way to do this in DerbyJS?
Theoretically I could reverse the items array, but I do not want to do this, because sometimes I update this array by using .push. Using .unshift instead seems to be suboptimal.

I'm pretty sure there is no each-reverse method that you can use in the view. One way to achieve what you want is to first reverse sort the list in the controller.
If you use a Derby sort filter to reverse the items in the array, you could still use push to update the original array like you desire.
#originalList = #model.at 'originalList'
#reversedList = #model.ref 'reversedList', #model.sort 'originalList', (a, b) ->
return a - b
// add new items to the list
#originalList.push 4
// in the view
{{each reversedList as #item}}
<p>{{#item}}</p>
{{/each}}
Alternatively, you could try
// in the view
{{each originalList as #item, #index}}
{{originalList[originalList.length - #index]}}
{{/each}}

Related

Get element position in ZStack

Cheers everyone,
For the past couple of days I have been trying to find a way, to identify elements within a ZStack.
File structure
ZStack {
foreach { element in
SubView(element)
}
}
I always have 3 elements stacked on top of each other & really want to get the top most element, to be able to edit some properties.
Any solution to this? Any help is appreciated.
Thanks!
Found the solution to this specific case
Background:
The View that is being loaded in the loop is in an array, that I previously created. The array also contain an ID.
What I did now was to find out the max ID, reverse loop through my array while passing the current ID and the current max ID to the new view, where I can handle the data accordingly.
So if the max ID == currentElement ID, I can apply my changes, thus also getting the topmost card.

How to make a List that contains another list, in flutter

I was trying to make an app in flutter, I now have an app that adds a new element into a list upon pressing a FAB. Kindly let me know how can I map another ListViewBuilder to each element. I am not sure if I am being clear enough, but I am trying to be clearer. I want an app that lets you create lists inside of lists, in flutter. So for each List element, there lies a corresponding list in which the contents of the list can be added. I am a beginner, and I am struggling to figure it out. plzzz, help me out..thanks in advance...
To create a list in a list you can use:
List<List<int>> numbers = new List<List<int>>();
And then you can add a new list to this by doing:
numbers.add([0,1,2]);
This adds the list [0,1,2] to the list called numbers.
From here you can access an element in this case the second element in the first list:
x = numbers[0][1]

Append does more than wanted

I have a tabbar view controller with three tabs. I have implemented a global array in this tabBar controller in order to make it accessible from all the tabs. It is an array of type [[[Any]]]. I wanted to modify this array from the first tab by appending an element but the problem is that instead of just appending the element, it modifies the existing element too. Can you help me pls ? Thanks.
Here is the definition of the array :
var invoices: [[[Any]]] = []
Here is the appending :
let tabBar = tabBarController as! baseTabBarController
tabBar.invoices.append(cells)
P.S: The append is done in a view controller connected to the first view controller (child of tabbar view controller) by a segue link.
You really need to give more information to get more personalised answers.
However, your Array is a three - dimensional - array, which means that you have an array inside an array inside an array.
When performing the append Method on an array, it always adds a new element to the end of this array.
In your case: The element that is added is of type [[Any]], which is a two - dimensional - array.
NOTE: More - dimensional - arrays are a bit tricky. What your code does is appending a new two - dimensional - array at the end of your outer array, not adding a new Any to your inner array!
However, the append Method will never modify existing Elements, so please check your syntax again, since more - dimensional - arrays are tricky.

Is there a way to highlight a specific item in the search results?

I have been trying to implement a slight change in the behavior of the chosen dropdown search results, but there doesn't seem to be any straightforward way to get at them. I have a list of countries preceded by three-letter ISO country code. I would like to be able to search as usual, but highlight the country if there is an exact match on the country code. The problem is that the matching country code will not always be the first item in the list. A more general request would be able to access the result list, and set the selection to some item other than the first.
By inspection, it looks to me like the search results only exist as a jquery object and within that the list items include a data-option-array-index with the original option list index, so I could find the item that I know I want highlighted in that DOM object and set the class "highlighted". But I also might need to scroll it into view, and messing with the underlying DOM directly is not ideal, so it would really be nice to have access to Chosen functions that would do that.
So, I figured it out, by accessing DOM. I have the index in the initial array that I want to highlight, and the following code will do it, assuming it is in the result list (using lodash _.find):
var highlightChosenResult = function(index) {
var resultList = $('li.active-result');
var result = _.find(resultList, function(el) {
return +$(el).attr('data-option-array-index') === index;
});
if (result) {
this.chosenApi.result_do_highlight($(result));
}
}

How to build an Array or Nested Array to display in UITableView and then sort by certain objectAtIndex?

Sorry for such a confusing title but it's hard to explain in a few words what I'm trying to accomplish. I'll try to explain the best I can. Ok, I'm parsing data from a xml file. It's constructed like so:
<item1>
<subitem1>text</subitem1>
<subitem2>text</subitem2>
<subitem3>text</subitem3>
<subitem4>text</subitem4>
<subitem5>text</subitem5>
</item1>
<item2>
<subitem1>text</subitem1>
<subitem2>text</subitem2>
<subitem3>text</subitem3>
<subitem4>text</subitem4>
<subitem5>text</subitem5>
</item2>
<item3>
<subitem1>text</subitem1>
<subitem2>text</subitem2>
<subitem3>text</subitem3>
<subitem4>text</subitem4>
<subitem5>text</subitem5>
</item3>
so on and so on...
So basically I want to display each item into a separate row in a UITableView and put each subitem into it's parent's row/cell as a label to display the info about it's parent "item".
Also, I need to be able to sort each item by one of it's subitems i.e. let's say subitem4. If subitem4 is equal to some string then it would display that item into the UITableView however if subitem4 isn't equal to that string I compare it to then that item wouldn't get displayed in the UITableView. As of right now I really don't have any working code because I'm not sure how to go about making this work. I don't know how I would do this because I have 1 array right now with all of the subitems together and I'm just separating each subitem and putting them into separate arrays so I can distinguish between each item row, I'm do it with the following code:
int totalNames = [Names count];
id name = [Names objectAtIndex:1];
[listOfItems addObject:name];
I'm pretty sure I'm going about this the wrong way. There must be a better way to do this logically. Any help or advice would be much appreciated. I'm mentally exhausted with this. Thanks.
Use classes.
Make each Item an instance of a class. make each subitem a property of the class. then have one array of the objects. then can sort based on a particular property or whatever.