2

How have the sum of a column in a list view?

By example the total cost :

                        ListModel {
                        id: fruitModel
                        ListElement {
                            name: "Apple"
                            cost: 2.45
                        }
                        ListElement {
                            name: "Orange"
                            cost: 3.25
                        }
                        ListElement {
                            name: "Banana"
                            cost: 2.45
                        }
                    }

                    UbuntuListView {
                        objectName: "ubuntuListView"
                        width: parent.width
                        height: main.height
                        model: sortedFruitModel
                        spacing: units.gu(1)
                        interactive: false

                        delegate: ListItem.Subtitled {
                            showDivider: true
                            anchors.leftMargin: units.gu(2)

                            Text {
                                text: name
                            }

                            Label {
                                anchors { right: parent.right; verticalCenter: parent.verticalCenter}
                                anchors.rightMargin: 15;
                                text: cost
                            }
                        }
                    } // ListView

Thank you for your help.

1 Answer 1

3

i think something like this will help you.

function sum(){
  var result = 0;
  for(var i = 0; i < fruitModel.count; i++){
    result += fruitModel.get(i).cost;
  }
  return result;
}

Not the answer you're looking for? Browse other questions tagged .