Hi Mahesh,
We validated your requirement that dynamically change the
datasource of TreeView component using ajax request. We have prepared a sample
for your requirement. In that sample, we have initially render the TreeView
component. During button click, we have to set the ajax request to fetch the
Json data and assign the new datasource to the TreeView component.
Refer the below code snippet.
|
<script> document.getElementById("element").addEventListener('click', function () { var treeInstance =
document.getElementById("listdata").ej2_instances[0];
$.ajax({
url: '/Home/updateData',
type: 'POST',
dataType: 'json',
success: function (result) {
//
Setting datasource to the TreeView component.
treeInstance.fields.dataSource
= result.data;
// When
calling dataBind() method, changes are immediately reflected in TreeView
compoennt.
treeInstance.dataBind();
},
error: function (xhr, status) {
console.log(status);
},
}) }); script> |
Refer the below code snippet for controller side.
|
public ActionResult updateData()
{
var
data1 = ajaxdata.GetTreeData();
return
Json(new
{ data = data1 });
} public class ajaxdata
{
public static
List
public static
List
{
List
load.Add(new ajaxdata { id = 1,
name = "User", hasChild = true });
load.Add(new ajaxdata { id = 2,
name = "Documents", hasChild = true, expanded = true });
load.Add(new ajaxdata { id = 3,
name = "Programs", hasChild = true, expanded = true });
load.Add(new ajaxdata { id = 4,
pid = 1, name = "Smith" }); });
return load;
}
public int id { get; set; }
public int? pid { get; set; }
public string name { get; set; }
public bool? hasChild { get; set; }
public bool? expanded { get; set; }
public bool? ischecked { get; set; }
public bool? selected { get; set; }
public string spriteCss { get; set; } } |
Refer the sample link below.
https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication11304156024.zip
Refer the below link to know about the TreeView component.
UG Documentation-https://ej2.syncfusion.com/aspnetmvc/documentation/treeview/getting-started/
Demo link-https://ej2.syncfusion.com/aspnetmvc/TreeView/DefaultFunctionalities#/material
API Reference-https://help.syncfusion.com/cr/cref_files/aspnetcore-js2/Syncfusion.EJ2~Syncfusion.EJ2.Navigations.TreeView~NodeChecking.html
Please let us know, if you need any further assistance.
Regards,
Sowmiya.P
|
For initial rendered data source of TreeView component |
When click the button, ajax request is sent to the controller side, it fetches the new data source. Result contains the new data source fetched from controller side. |
|
|
|
|
|
|
|
|
@Html.EJS().TreeView("listdata").ShowCheckBox(true).AutoCheck(false).Fields(ViewBag.TreeViewFields).Render() |