107 lines
3.3 KiB
QML
107 lines
3.3 KiB
QML
/* JSONListModel - a QML ListModel with JSON and JSONPath support
|
|
*
|
|
* Copyright (c) 2012 Romain Pokrzywka (KDAB) (romain@kdab.com)
|
|
* Licensed under the MIT licence (http://opensource.org/licenses/mit-license.php)
|
|
*/
|
|
|
|
import QtQuick 2.0
|
|
import "../js/jsonpath.js" as JSONPath
|
|
|
|
Item {
|
|
property string source: ""
|
|
property string json: ""
|
|
property string query: ""
|
|
property bool jsonready: true
|
|
property bool modelready: true
|
|
property bool prev: false
|
|
property bool next: false
|
|
property string sortby: ""
|
|
//property string filterby: ""
|
|
//property string filterkey: ""
|
|
|
|
property ListModel model : ListModel { id: jsonModel }
|
|
property alias count: jsonModel.count
|
|
//property alias get: jsonModel.get
|
|
|
|
onSourceChanged: {
|
|
if (source !== "") {
|
|
jsonready = false
|
|
modelready = false
|
|
var xhr = new XMLHttpRequest;
|
|
xhr.open("GET", source);
|
|
xhr.onreadystatechange = function() {
|
|
if (xhr.readyState === XMLHttpRequest.DONE)
|
|
json = xhr.responseText;
|
|
}
|
|
xhr.send();
|
|
} else jsonModel.clear();
|
|
}
|
|
|
|
onJsonChanged: updateJSONModel()
|
|
onQueryChanged: updateJSONModel()
|
|
//onFilterbyChanged: updateJSONModel()
|
|
//onFilterkeyChanged: updateJSONModel()
|
|
|
|
function updateJSONModel() {
|
|
jsonModel.clear();
|
|
|
|
if ( json === "" )
|
|
return;
|
|
|
|
var objectArray = parseJSONString(json, query);
|
|
|
|
if (sortby !== "") objectArray = sortByKey(objectArray, sortby);
|
|
//if (filterby !== "" && filterkey !=="") objectArray = filterValuePart(objectArray, filterby, filterkey);
|
|
|
|
for ( var key in objectArray ) {
|
|
var jo = objectArray[key];
|
|
if( jo.toString() === "[object Object]"){
|
|
jsonModel.append( jo );
|
|
} else {
|
|
//this is comma separated strings
|
|
var str = jo.toString().split(",");
|
|
for(var i = 0; i < str.length; i++){
|
|
jsonModel.append({ "item": str[i] } );
|
|
}
|
|
break;
|
|
}
|
|
// var jo = objectArray[key];
|
|
// jsonModel.append( jo );
|
|
// console.log("JSON: "+objectArray[key])
|
|
}
|
|
jsonready = true
|
|
}
|
|
|
|
function parseJSONString(jsonString, jsonPathQuery) {
|
|
var objectArray = JSON.parse(jsonString);
|
|
if ( jsonPathQuery !== "" )
|
|
objectArray = JSONPath.jsonPath(objectArray, jsonPathQuery);
|
|
|
|
return objectArray;
|
|
}
|
|
|
|
function filterValuePart(array, part, key) {
|
|
part = part.toLowerCase();
|
|
return array.filter(function(a) {
|
|
var x = a[key];
|
|
return x.toLowerCase().indexOf(part) !== -1;
|
|
});
|
|
}
|
|
|
|
function sortByKey(array, key) {
|
|
var res = key.split(".")
|
|
//console.log("RES: "+res.length)
|
|
if (res.length === 1) {
|
|
return array.sort(function(a, b) {
|
|
var x = a[res[0]]; var y = b[res[0]];
|
|
return ((x > y) ? -1 : ((x < y) ? 1 : 0));
|
|
});
|
|
} else {
|
|
return array.sort(function(a, b) {
|
|
var x = a[res[0]][res[1]]; var y = b[res[0]][res[1]];
|
|
return ((x > y) ? -1 : ((x < y) ? 1 : 0));
|
|
});
|
|
}
|
|
}
|
|
}
|