2017-05-26 15:32:01 +08:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>JSONEditor | Auto Complete</title>
|
|
|
|
|
|
|
|
<link href="../dist/jsoneditor.css" rel="stylesheet" type="text/css">
|
|
|
|
<script src="../dist/jsoneditor.js"></script>
|
2017-06-17 08:19:56 +08:00
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
|
2017-05-26 15:32:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
<style type="text/css">
|
|
|
|
#jsoneditor {
|
|
|
|
width: 500px;
|
|
|
|
height: 500px;
|
|
|
|
}
|
|
|
|
|
|
|
|
p {
|
|
|
|
width: 500px;
|
|
|
|
font-family: "DejaVu Sans", sans-serif;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<!--<link href="./css/darktheme.css" rel="stylesheet" type="text/css">-->
|
|
|
|
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<p>
|
2017-05-26 15:45:20 +08:00
|
|
|
This example demonstrates how to autocomplete works, options available are dynamics and consist in all the strings found in the json
|
2017-05-26 15:32:01 +08:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<div id="jsoneditor"></div>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
|
|
// create the editor
|
|
|
|
var container = document.getElementById('jsoneditor');
|
|
|
|
var options = {
|
|
|
|
modes: ['text', 'tree'],
|
|
|
|
autocomplete: {
|
2017-06-01 05:53:24 +08:00
|
|
|
applyTo:['value'],
|
2017-06-29 13:57:54 +08:00
|
|
|
getOptions: function (text, path) {
|
2017-06-17 08:19:56 +08:00
|
|
|
return new Promise(function (resolve, reject) {
|
2017-06-29 13:57:54 +08:00
|
|
|
var options = extractUniqueWords(path);
|
2017-06-17 08:19:56 +08:00
|
|
|
if (options.length > 0) resolve(options); else reject();
|
|
|
|
});
|
2017-05-26 15:32:01 +08:00
|
|
|
}
|
|
|
|
}
|
2017-06-29 13:57:54 +08:00
|
|
|
};
|
2017-06-17 08:19:56 +08:00
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
function extractUniqueWords (json) {
|
|
|
|
return _.uniq(_.flatMapDeep(json, function (value, key) {
|
|
|
|
return _.isObject(value)
|
|
|
|
? [key]
|
|
|
|
: [key, String(value)]
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2017-05-26 15:32:01 +08:00
|
|
|
var json = {
|
|
|
|
'array': [{'field1':'v1', 'field2':'v2'}, 2, 3],
|
|
|
|
'boolean': true,
|
|
|
|
'null': null,
|
|
|
|
'number': 123,
|
|
|
|
'object': {'a': 'b', 'c': 'd'},
|
|
|
|
'string': 'Hello World'
|
|
|
|
};
|
|
|
|
var editor = new JSONEditor(container, options, json);
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|