thymeleaf设置网页脚本里面的值
thymeleaf修改javascript中的值, 下面示例为修改Fields的值
前端使用layui配置表格字段, 字段不固定, 由后端决定.
<script th:inline="javascript"> var fields = eval([[${Fields}]]); </script> <script> layui.use(["table"], function () { var table = layui.table; table.render({ elem: "#demo", id: "demo", url: "/getdata", cols:[fields] }); }); </script>
后端java代码中设置Fields的值
model.addAttribute("Fields", "[{ type: \"checkbox\", width: 120 },{ field: \"id\", title: \"编号\", align: \"center\", sort: true },{ field: \"itemid\", title: \"物品\", align: \"center\" },{ title: \"操作\", align: \"center\", fixed: \"right\", toolbar: \"#barDemo\", width: 300 }]");
前端页面html中Fields为:
var fields = eval("[{ type: \"checkbox\", width: 120 },{ field: \"id\", title: \"\u7F16\u53F7\", align: \"center\", sort: true },{ field: \"itemid\", title: \"\u7269\u54C1\", align: \"center\" },{ title: \"\u64CD\u4F5C\", align: \"center\", fixed: \"right\", toolbar: \"#barDemo\", width: 300 }]");
thymeleaf中替换脚本值可以使用[[${变量名}]], 但转到前端为字符串, 可以使用eval再转换为js变量