Adding items to JS list which is used as a model
-
I have a ListView and its model is a JavaScript list. For example if I declare it as
property var options: [{text: "ttt"}]
and use it in delegate as modelData.text there is no problem in first initialization.But when i want to add an item to that list using a button etc. it doesnt do anything. I call options.push(item) but neither it gives an error nor updates view.
I may use QML item models but this is easier since i get this input from a JSON source.
-
Hi,
this can help for the data persistence : https://stackoverflow.com/questions/48730166/how-to-save-and-restore-the-content-of-a-listmodelyou can parse your json file first, then you create a ListModel with the data (methode in the first link)
then you add data using append() http://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html#append-method
and save on exit
-
Hi,
this can help for the data persistence : https://stackoverflow.com/questions/48730166/how-to-save-and-restore-the-content-of-a-listmodelyou can parse your json file first, then you create a ListModel with the data (methode in the first link)
then you add data using append() http://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html#append-method
and save on exit
@LeLev I know how to do it like in link you gave. He uses for loop to add them to a ListModel:
for (var i = 0; i < datamodel.length; ++i) dataModel.append(datamodel[i])
But I can use a simple JavaScript list as a model and i want to push items to that directly. But model doesnt update when use list.push().
If i cannot find a solution i have to use that ListModel way ofcourse.
-
@maydin said in Adding items to JS list which is used as a model:
property var options:
Declare as
property var options:["ttt"]
To add items use
options.push("Some Text")
-
I have a ListView and its model is a JavaScript list. For example if I declare it as
property var options: [{text: "ttt"}]
and use it in delegate as modelData.text there is no problem in first initialization.But when i want to add an item to that list using a button etc. it doesnt do anything. I call options.push(item) but neither it gives an error nor updates view.
I may use QML item models but this is easier since i get this input from a JSON source.
@maydin Pure Javascript array could not be models easily, instead, use
ListModel
, or use C++QStringList
or other C++ data structure.