How to remove specific element from array with RactiveJs - ractivejs

I need to remove a specific element from the array (either by index or by object itself - doesn't matter). Seems like I'm blind but I don't see appropriate method in RactiveJs documentation. I found a method to remove an element from the beginning of the arrya (shift) or from the end of the array (pop) but don't see a method to remove specific element.

Seems like ractive.splice
ractive.splice('myArray', indexToRemove, 1)
is the way to go.

Define index reference on list (the num in below example) and then use splice:
{{#each students:num}}
<p>{{firstName}}</p>
<button on-click="event.splice('..', event.get('num'), 1)">Remove</button>
{{/each}

Related

Get position of slot item using ISSLOT tag

Actually I'm using isslotiterator and I have to use loop in loop to get the position of each slot item with the attribute counter.
I would like to know if it is possible to get the position with a <isslot>.
Imagine that I have a slot with 3 entries.
So when I'm calling <isslot> in the slot item ISML I could do :
#Pagelet:Position#
Then having as result : 1 / 2 / 3
Unfortunately position is not a characteristic of type Pagelet. If successfully doing this, you need to work with PageletAssignment. So instead of Pagelet:Position you need to use PageletAssignment:Position.
BUT
isslot is preparing a render dictionary for your "slot item ISML" that does not contain the assignment that was leading to this "slot item". It only contains the item itself (aka. the pagelet). Reason for that is slots can define a Pagelet Pipeline that fully dynamically determine pagelets without the need for assignments.
My suggestion: Either use isslotiterator with AssignmentsAlias (easy) or use a pagelet pipeline defined at the slotdefinition of your slot (more complicated).
No i don't think this is possible. <isslot> does not pass the location information down of the pagelet, it just renders it. To have more control of how slot items are rendered, the isslotiterator tag was introduced.
You can get the position info from the SlotPageletAssignment object if u dont want to use a counter.
Take a look at the Carousel component on the demo homepage.
app_sf_responsive_cm/release/templates/default/component/common/Carousel.isml
there u can see they use AssignmentsAlias key to get the SlotPageletAssignment
<isslotiterator Slot="#Pagelet:SubSlot("app_sf_responsive_cm:slot.carousel.items.pagelet2-Slot")#" AssignmentsAlias="CarouselPageletAssignments">
<isloop iterator="CarouselPageletAssignments" alias="CarouselPageletAssignment" counter="i">
#CarouselPageletAssignment:Position# - #CarouselPageletAssignment:getSubPagelet#<br/>
</isloop>

How do I dynamically remove an element from a QML basic type list?

I've successfully added elements using
list.push(element)
But how do I remove them? I've tried the following, but none of them seem to work.
list.pop()
list.pop_front()
list.remove()
list.remove(int)
list.remove(element)
For people who always keep ending up on this page, like I did: I found a very hacky solution to remove an element from a list<QtObject>.
myList = Array.from(myList).filter(r => r !== elementIWant2Remove)
This is not very elegant, but it does the trick, if you need to remove an element.
You can't remove individual items. According to the docs:
Note that objects cannot be individually added to or removed from the list once created; to modify the contents of a list, it must be reassigned to a new list.
This means that the pop method most likely won't exist with the basic list type. (However, there is a push method.)
You'll be much better off using JavaScript arrays.
property var myArray: [1, 2, 3]
Component.onCompleted: {
myArray.push(4);
myArray.push(5);
myArray.splice(1, 1) // simulates a remove
myArray.pop(); // pop last item
console.debug(myArray) // [1, 3, 4]
}
I've used ints here, but you can also store QML objects and other types as well. Using JavaScript arrays exposes your variables to most (if not all) of JavaScript's Array functions.

cannot changed the property of an object inside an array

There is a parent element. One of its property is an array of objects. This array is initialised in the ready function. The display of this array is handed down to a child element. What I want to do is to update the quantity of each element in the array whenever the multiplier property changed. I tried to use the override dirty checking technique described in the docs but couldn't get it to work. I would appreciate some guidance.
[plunker link][1]
[1]: http://plnkr.co/edit/pCZyUC7YtgUU8cpejNpj?p=info
Here is working example: Plunk
Few things are corrected, one is this:
//DO not use 'this.setItems.0.quantity.value', but:
this.set('setItems.0.quantity.value', newQuantity);
console.log("New arr val: ");
console.log(this.setItems[0].quantity.value);
Docs:
https://www.polymer-project.org/1.0/docs/devguide/model-data
https://www.polymer-project.org/1.0/docs/devguide/data-binding#array-binding

AngularJS - stop angular from including element from ng-repeat from array1 if its present in array2's ng-repeat (pointer reference to the object?)

So this is pretty self explanatory:
I have an object in array1 and I want to make a reference of object1 from array1 into array2 so array2 has a reference of that object. Then I do ng-repeat on both arrays but then both of them appear in the dom and I want only one copy to be present in the DOM.
How about if I do the same for more arrays like array3 and array4 ... doing the same thing?
Is there anything such as only keeping a reference of the object and not an actual copy, hence ng-include will not include it?
I can only think of similar thing as working with pointers in C where you literary keep a reference of the object and not a copy.
Array1[ Array2[
Object1{}, Object5{},
Object2{}, <--POIINTS TO-- Object2{},(JUST A REFERENCE)
Object3{}, Object512{},
Object4{}, ...]
...]
I currently have:
PSEUDO:
Array2.push(Array1.object2);
but that simply makes a copy of it so the ng-includes display both objects.
Any suggestions?
I think that I understand what you're asking, but please correct me if I don't. It appears that you have a few arrays with some data that includes duplicates and you want to display that data but filter out the duplicates.
If this is the case, I would suggest simply doing a union on all of the arrays (using something like _.union and then passing the result into ng-repeat.

Insert am document at index in Meteor Collection

i need to replace an elements in an Meteor.Collection, but want to replace it, so it appears in the same position as the old one.
For that i found in the docs in observe, that there is an atIndex parameter in the callbacks.
e.g.
cursor.observe({
addedAt: function(document, atIndex, before){
...
}
...
})
does this mean i can insert objects at a specific index position, if so how?
If you use .update() it will appear in the same position.
Or maybe I misunderstand what you're trying to accomplish?

Resources