Although I found the answer to this issue on Angular.js more complex conditional loops satisfactory and accepted it, I feel there is more to discuss.
Let me provide further details that were not included in my initial inquiry.
My goal is to transform the following:
<h3>11.4.2013</h3>
<ul>
<li>oofrab | 4 | 11.4.2013 14:55 <button>remove</button></li>
<li>raboof | 3 | 11.4.2013 13:35 <button>remove</button></li>
</ul>
<h3>10.4.2013</h3>
<ul>
<li>barfoo | 2 | 10.4.2013 18:10 <button>remove</button></li>
<li>foobar | 1 | 10.4.2013 12:55 <button>remove</button></li>
</ul>
into this data structure:
[
{
"id": 4,
"name": "oofrab",
"date": "2013-11-04 14:55:00"
},
{
"id": 3,
"name": "raboof",
"date": "2013-11-04 13:55:00"
},
{
"id": 2,
"name": "barfoo",
"date": "2013-10-04 18:10:00"
},
{
"id": 1,
"name": "foobar",
"date": "2013-10-04 12:55:00"
}
]
In addition to standard ng-repeat
, I specifically want to include those headings. However, the process seems unnecessarily complicated despite the provided solution.
The implementation derived from the first question can be viewed here: http://plnkr.co/edit/Zl5EcsiXXV92d3VH9Hqk?p=preview
It's important to note that the system could potentially handle up to 400 entries while allowing for dynamic entry manipulation.
The plunker example achieves the desired outcome by:
Iterating through the original data to create a new structured object as shown below:
{
"2013-10-05": [
{
"id": 4,
"name": "oofrab",
"date": "2013-10-05 14:55:00",
"_orig_index": 0
},
{
"id": 3,
"name": "raboof",
"date": "2013-10-05 13:55:00",
"_orig_index": 1
}
],
"2013-10-04": [
{
"id": 2,
"name": "barfoo",
"date": "2013-10-04 18:10:00",
"_orig_index": 2
},
{
"id": 1,
"name": "foobar",
"date": "2013-10-04 12:55:00",
"_orig_index": 3
}
]
}
This allows for the desired result to be attained through the following approach:
<div ng-repeat="(date,subItems) in itemDateMap">
<h3>{{date}}</h3>
<ul>
<li ng-repeat="item in subItems">
{{item.name}} | {{item.id}} | {{item.date}}
<button type="button" ng-click="removeItem(item._orig_index)">x</button>
</li>
</ul>
</div>
Despite achieving the intended outcome, significant drawbacks persist. The need to rebuild the itemDateMap
each time a new entry is added or removed, date alterations are made, or an item needs to be deleted proves cumbersome. Additionally, performance issues arise when handling a large number of entries.
This convoluted process goes against the simplicity of the task at hand.
How should I proceed?