Hi, I am trying to write JSON data of JsonListModel to Storage and display it to AppListView also using JsonListModel, nothing is working for me, what is my mistake? I have little experience, don’t judge my code strictly.
import Felgo
import QtQuick
App {
id:app
Storage
{
id:storage
clearAllAtStartup :true
}
JsonListModel {
id:jsonModel_1
property var jsonData_1: [
{
“name”: 1,
“title”: “apple”,
“text”: “green”
},
{
“name”: 2,
“title”: “tomato”,
“text”: “red”
},
{
“name”: 3,
“title”: “banana”,
“text”: “yellow”
}
]
keyField: “name”
fields: [“title”,”text”,”name”]
source: jsonData_1
}
JsonListModel {
id:jsonModel_2
property var jsonData_2: [
{
“name”: 1,
“title”: “apple_2”,
“text”: “green_2”
},
{
“name”: 2,
“title”: “tomato_2”,
“text”: “red_2”
},
{
“name”: 3,
“title”: “banana_2”,
“text”: “yellow_2”
}
]
keyField: “name”
fields: [“title”,”text”,”name”]
source:jsonData_2
}
AppListView {
id:listView
anchors.fill: parent
model: jsonModel_1
delegate:
Rectangle {
id:rec
// property bool isFavorite: dataModel.isFavorite(model)
//Component.onCompleted: dataModelfareBase.createModelForCategory (“public”)
width: listView.width
height: 70
border.color: “black”
border.width: 1
Text {
id: name
text: model.title || {}
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
font.pixelSize: 30
}
RippleMouseArea {
//Whether the ripple effect is used
//This property determines whether the element accepts mouse events.
anchors.fill: parent
onClicked: {
app.addToFavorites(model)
}
}
}
}
//—————————————————————
function addToFavorites(entry) {
var favorites = storage.getValue(“favorites”)
if (favorites === undefined) {
favorites = []
}
console.log(entry[“title”])
favorites.push(entry[“title”])
//Saves the value with the provided key in storage permanently.
//If an error occurs, a storage Error() message is issued.
storage.setValue(“favorites”, favorites)
}
//————————————————————————–
//show favorites
function showFavorites() {
//Deletes content by index from the model.
//You can specify the number of elements to remove using the count argument.
jsonModel_2.remove(0,jsonModel_2.count) // jsonModel_1.remove(0,jsonModel_1.count)
var favorites = storage.getValue(“favorites”)
if (favorites === undefined) {
console.log(“Favorites not specified”)
return
}
for (const entry of favorites) {
jsonModel_2.append(entry) // jsonModel_1.append(entry)
}
}
AppButton
{
id:button
text: “favorites”
flat: false
backgroundColor: “white”
borderColor: “gainsboro”
textColor: “black”
borderWidth: dp(0.7)
anchors
{
horizontalCenter:parent.horizontalCenter
bottom:parent.bottom
bottomMargin:50
}
onClicked:{
app.showFavorites()
listView.model=jsonModel_2
}
}
}