javascript - Spring + jsp + selectivity.js - 如何从 java 站点上构建的文件中读取选择性配置

标签 javascript java spring

我正在我的 spring 应用程序中使用 selectivity.js这是一个具有漂亮“标签外观”的选择库。现在我有一个 js 文件,我可以在其中配置选择性数据:

/**
 * 
 */
$(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'
		});
	}
);

但我需要这样的配置:

{
			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'
		}

在一些文件中,例如:“myData.txt”。在分析了一些数据之后,我将从 java 站点以编程方式构建这个 txt 文件。在下一步中,我需要从 js 函数中读取这个结构:

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

你知道如何从另一个文本文件中读取这种数据结构以使其工作吗?

最佳答案

为什么不将配置放在一个可以使用 $.ajax() 加载的 JSON 文件中?然后你可以这样做:

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

关于javascript - Spring + jsp + selectivity.js - 如何从 java 站点上构建的文件中读取选择性配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34765086/

相关文章:

javascript - jquery - 子菜单在主菜单的鼠标移开时隐藏

横幅中的 JavaScript 文本按时间间隔更改 - 无点击

java - Hibernate 教程不起作用

java - 在运行时获取 Maven 工件版本

java - Spring AOP 使用方法和参数注解

Spring Security 5.2 密码流程

java - 通过 ModelMapper 映射时将 LocalDateTime.now() 的值设置为两个字段

JavaScript 快速页面滚动

javascript - 颜色谷歌虚拟化 bool 列

java - 在 C++ 中的模板参数上调用静态函数