Spring + jsp + selectivity.js - how to read the selectivity configuration from a file created on a java site

I use selectivity.js in my spring application , which is a select library with a nice "tag viewer". Right now I have a js file where I set up selectivity data:

/**
 * 
 */
$(document).ready(
	function() {
		$('#single-input-with-labels').selectivity(
		{
			multiple : true,
			items : 
			[ 
				{
					text : 'City',
					children : [{
						id : 1,
						text : 'cityName1'
					}, {
						id : 2,
						text : 'cityName2'
					}, {
						id : 3,
						text : 'cityName3'
					}]
				}, {
					text : 'Type',
					children : [{
						id : 4,
						text : '2.1.typeName1'
					}, {
						id : 5,
						text : '2.2.typeName2'
					}, {
						id : 6,
						text : '2.3.typeName3'
					}]
				}
			],
			placeholder : 'ie. City, Type, ...',
			searchInputPlaceholder : 'Type to search'
		});
	}
);
Run codeHide result

But I need to have this configuration:

{
			multiple : true,
			items : 
			[ 
				{
					text : 'City',
					children : [{
						id : 1,
						text : 'cityName1'
					}, {
						id : 2,
						text : 'cityName2'
					}, {
						id : 3,
						text : 'cityName3'
					}]
				}, {
					text : 'Type',
					children : [{
						id : 4,
						text : '2.1.typeName1'
					}, {
						id : 5,
						text : '2.2.typeName2'
					}, {
						id : 6,
						text : '2.3.typeName3'
					}]
				}
			],
			placeholder : 'ie. City, Type, ...',
			searchInputPlaceholder : 'Type to search'
		}
Run codeHide result

in some file, for example: "myData.txt". I will create this txt file programmatically from the java site after analyzing some data. In the next step, I need to read this structure from the js function:

/**
 * 
 */
$(document).ready(
	function() {
		$('#single-input-with-labels').selectivity(
          /* here I want to read this structure from the generated txt file
		);
	}
);
Run codeHide result

Do you know how to read this data structure from another text file to make it work?

+4
1

JSON, $.ajax()? - :

$(document).ready(function() {
    $.ajax('/url/to/my/config.json').done(function(config) {
        $('#single-input-with-labels').selectivity(config);
    });
});
0

Source: https://habr.com/ru/post/1624091/


All Articles