Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
RubenM
Posts: 22
Registered: ‎04-15-2013
My Device: Dev Alpha C

Listview: delete item

Hello, i'm having some problems with deleting items in listview.
I'am making an app based on the persistent object example:
httphttp://blackberry.github.io/Cascades-Samples/persistentobjects.html

 

Now i want to delete an "item" with an action in the listview, but the app can't seem to get access to the ID of the object.

ListView {
             dataModel: _app.dataModel
                        
                        listItemComponents: [
                            ListItemComponent {
                                type: "item"  
                                StandardListItem {
                                    imageSource: "asset:///Images/ic_feedback.png"
                                    title: qsTr("%1").arg(ListItemData.firstName)
                                    contextActions: [
                                        ActionSet {
                                            title: "Item"
                                            subtitle: "Options"
                                            DeleteActionItem {title: "Delete"
                                                onTriggered: {
                                                    _app.deleteObject(ListItemData.customerID);
                                                }
                                                imageSource: "asset:///Images/ic_delete.png"
                                            
                                            }
                                          }
                                    ]
                                
                                } 
                                
                            }
                            
                        ]
                        
                    }   

Also i need to access the other variables, eg firstName (which only is accessible for the title of the StandardListItem) from outside the listview.

 Anyone have a solution? Or an alternative?

Please use plain text.
Developer
simon_hain
Posts: 16,212
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Listview: delete item

list items have a different context and cannot access the normal context.

several ways:
1.
give your listitem an id.
then use id.ListItem.view to access the listview.
In the listview you can access _app normally.
2.
use QDeclarativeEngine::rootContext and set the context property _app on that. The root context can be accessed by both listitems and the rest of the app
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
RubenM
Posts: 22
Registered: ‎04-15-2013
My Device: Dev Alpha C

Re: Listview: delete item

How would you delete an object/item in the example above? Because _app is known in the listview (no errors), but even if i do this:

StandardListItem {
                                    id: itRe
                                    imageSource: "asset:///Images/ic_feedback.png"
                                    title: qsTr("%1").arg(ListItemData.firstName)
                                    description: qsTr("%1").arg(ListItemData.customerID)
                                    contextActions: [
                                        ActionSet {
                                            title: "Item"
                                            subtitle: "Options"
                                            DeleteActionItem {title: "Delete"
                                                onTriggered: {
                                                    _app.deleteObject(itRe.description);
                                                }
                                                imageSource: "asset:///Images/ic_delete.png"
                                            
                                            }

 It won't delete the object.... (if i test it in the simulator description says 1 for item 1, so _app.deleteObject(Qstr ID) should delete it, or am i wrong? )

Please use plain text.
Developer
simon_hain
Posts: 16,212
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Listview: delete item

are you sure that deleteObject is called? add some debugging.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
bmorr
Posts: 331
Registered: ‎04-13-2013
My Device: Z10

Re: Listview: delete item

here's what worked for me 

 

First you need to declare a function like so:

 

ListView {
                function getApp() { // 1
                    return _app
                }

 

 

Then you need to use that declaration in your invoke call so that it knows that your invoking an action from the _app:

 

DeleteActionItem {
                                            title: qsTr("Delete") + Retranslate.onLanguageChanged
                                            imageSource: "asset:///images/delete.png"
                                            
                                            onTriggered: {
rootItem.ListItem.view.getApp().deleteObject(ListItemData.customerID) } }

 

 

Please use plain text.
Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: Listview: delete item

[ Edited ]

There's an easier way; pure QML for the deletion.

 

Each item knows its path - and the editable data models have remove functions.

 

The following code works for GroupDataModel (ArrayDataModel has a different remove):

 

import bb.cascades 1.2

Page {
    Container {
        ListView {
            dataModel: GroupDataModel {
            }
            listItemComponents: [
                ListItemComponent {
                    type: "item"
                    Container {
                        id: component
                        Label {
                            text: ListItemData.name
                        }
                        contextActions: ActionSet {
                            DeleteActionItem {
                                title: "Delete"
                                onTriggered: {
                                    component.ListItem.view.dataModel.removeAt(component.ListItem.indexPath);
                                }
                            }
                        }
                    }
                }
            ]
        }
    }
}

 *post editted for shorter QML.

 

--
Rob is no longer associated with BlackBerry.
Please use plain text.