从使用Ajax调用检索的数据填充引导树视图时出错
我正在使用引导treeview插件()。我不确定这是treeview插件的问题还是我实现代码的方式的问题。我正在尝试使用ajax回调中的数据加载treeview。以下是我正在使用的代码示例:从使用Ajax调用检索的数据填充引导树视图时出错,ajax,treeview,bootstrap-treeview,Ajax,Treeview,Bootstrap Treeview,我正在使用引导treeview插件()。我不确定这是treeview插件的问题还是我实现代码的方式的问题。我正在尝试使用ajax回调中的数据加载treeview。以下是我正在使用的代码示例: <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content=
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<!-- Required Javascript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="~/Content/BootStrap/bootstrap-treeview.js"></script>
</head>
<body>
<div class="clear-fix"> </div>
<div class="row">
<div class="col-sm-2"><div id="lstCollections">This will display my list of all collections</div></div>
</div>
</body>
</html>
<script type="text/javascript">
function getTree(handleData) {
$.ajax({
type: "GET",
url: '@Url.Action("Get","DatabaseSchema")',
dataType: 'json',
success: function (response) {
handleData(response);
}
});
}
$(document).ready(function () {
getTree(function (output) {
var collectionTree = [];
output.forEach(function (a) {
collectionTree.push({ text: a });
});
//var tree2 = [{ text: "Buyer" }, {text: "text1" }, { text: "text2" }, { text: "text3" }];
$('#lstCollections').treeview({ data: collectionTree });
});
})
</script>
一切正常!!
当调用在ajax回调函数中时,我不确定为什么要获取Tig treeview is not function()。传递到treeview的正确格式不是从ajax调用中得到的格式。您应该解析json对象,如以下示例所示:
let invalid = '{ "bank": "CityBank", "sum": "500" }, { "bank": "WPBank", "sum": "700" }';
let valid = JSON.parse("[" + invalid + "]");
现在,您可以将有效变量毫无问题地传递到treeView
这一切都是关于向JSON对象添加方括号符号 传递到treeview的正确格式不是从Ajax调用中得到的格式。您应该像下面的示例那样解析json对象:
let invalid = '{ "bank": "CityBank", "sum": "500" }, { "bank": "WPBank", "sum": "700" }';
let valid = JSON.parse("[" + invalid + "]");
现在,您可以将有效变量毫无问题地传递到treeView
这一切都是关于向JSON对象添加方括号符号 好问题。我的树视图也不工作了。这是我第四次尝试了,真是个好问题。我的树视图也不工作了。这是我第四次尝试记录在案。