我有以下代碼加載jstree
控件。數據作爲json
加載到jstree
。我如何從jstree獲得選定節點
<table>
<tr>
<td style="vertical-align:top;">
<div id="Div_jstree"></div>
</td>
<td style="vertical-align:top;">
<div id="Div_txta"></div>
</td>
</tr>
</table>
@section Scripts{
<script src="Scripts/jstree.min.js"></script>
<script>
//load the data from ajax and populate the child nodes
$(document).ready(function() {
$.ajax({
url: 'Home/GetData',
method: 'get',
dataType: 'json',
success: function (data) {
$('#Div_jstree').on('select_node.jstree', function (event, data) {
console.log($("#Div_jstree").jstree("get_selected").text());
var htmlta = '<br><div style="display:inline-block;" ' + 'id=div-' + data.studentid + '>' +
'<textarea style=width:100px;height:100px;visibility:visible;' + 'id=' + data.studentid + '>' + 'name here'
+ ':</textarea>' +
'<input type="button" class="del" value="Del"' + 'id=' + data.id + ' /></div>';
$('#Div_txta').append(htmlta);
$(document).on('click', '.del', function() {
$(this).closest('div').remove();
});
})
.jstree({
"core": {
"data": data,
"themes": {
"url": true,
"icons": true,
"dots": true
},
'check_callback': true
},
"plugins": ["dnd"]
});
},
error: function (x, y, z) {
console.log('error');
}
});
});
}
這裏是前述操作方法:
[HttpGet]
public JsonResult GetData()
{
var tvdata = new List<TVCVM>();
tvdata.Add(new TVCVM { id = "n1", parent = "#", text = "Cardiology", value = "10", @class= "parentNode"});
tvdata.Add(new TVCVM { id = "n2", parent = "#", text = "Physio", value = "11", @class = "parentNode" });
tvdata.Add(new TVCVM { id = "n3", parent = "n2", text = "Bopara", value = "100", @class = "childNode" });
tvdata.Add(new TVCVM { id = "n4", parent = "n2", text = "Wickram", value = "101", @class = "childNode" });
return Json(tvdata, JsonRequestBehavior.AllowGet);
}
這裏是VM
public class TVCVM
{
public String id { get; set; }
public string @class { get; set; }
public string parent { get; set; }
public string text { get; set; }
public string value { get; set; }
}
AJAX接收數據,因爲它顯示了他們的文本樹節點,但它顯示了以下錯誤:
$("#Div_jstree").jstree("get_selected").text();
th行錯誤
Uncaught TypeError: $(...).jstree(...).text is not a function
- 當我試圖訪問htmlta例如JSON數據:
data.id
給出
undefined error and for data[0]
.id
, id is not defined error.
如何修正論文問題? 中,當我試圖訪問
我想擴展這個問題,我將如何將多個值存儲在一個節點,如GteData()返回一個類的集合,我需要將這些屬性值存儲在節點中 –
您可以存儲jsTree將構建的'li'元素的屬性中的任何值。要做到這一點,請在你的json的'li_attr'鍵中傳遞這樣的信息:'{「id」:「...」,「parent」:「...」,「text」:「...」 「li_attr」:{「data-key1」:「value1」,「data-key2」:「value2」}}' –
數據綁定通過ajax發生,所以我需要添加屬性'li_attr'到TVCVM類,以便內部創建該屬性作爲json鍵,這個東西我沒有嘗試過之前 –