I’ve been working on this for days and can’t see to crack it! Deadlines are very close and I am full of covid. Please please help me crack it asap. I feel like i’ve tried every option of square brackets, curly brackets but I just can’t get JSTree to interpret it correctly from my Array. When I return a 1 line string using the exact same format, JSTree correctly renders it as a folder with the correct name but when I return the Array, JSTree shows a list of folders but they have the whole string as the name:
https://i.imgur.com/6ElhCFW.png
C# Code:
[WebMethod]
public static Array getChildren(string libraryID)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con.Open();
//Get the library name
string getUserInfo = "select LibraryName from Document_Library_Names where Id =" + libraryID;
SqlCommand getUserInfocmd = new SqlCommand(getUserInfo, con);
string libraryName = Convert.ToString(getUserInfocmd.ExecuteScalar()).Trim();
//Getting documents
string strGetDocuments = "Select * from Documents where LID =" + libraryID + " order by DisplayOrder ASC";
SqlCommand getDocumentsCMD = new SqlCommand(strGetDocuments, con);
getDocumentsCMD.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataReader sqlDR = getDocumentsCMD.ExecuteReader();
dt.Load(sqlDR);
sqlDR.Close();
if (dt != null)
{
if (dt.Rows.Count > 0)
{
int arrayCount = dt.Rows.Count;
int rowCount = 1;
string arrayEnd = "";
string[] documentArray = new string[arrayCount+1];
//List<string> documentList = new List<string>();
documentArray[0] = "[{"id":"" + libraryName + "","parent":"#","text":"" + libraryName + "","data":"0"}]";
//Build the string
foreach (DataRow row in dt.Rows)
{
//Getting the data
string ID = row[0].ToString().Trim();
string type = row[2].ToString().Trim();
string name = row[3].ToString().Trim();
string parentFolder = row[4].ToString().Trim();
string displayOrder = row[5].ToString().Trim();
string extension = row[8].ToString().Trim();
//Get parentFolder ID
string parentFolderID = "0";
if (parentFolder != "")
{
string strParentFolderID = "Select ID from Documents where Name=@Name and Type=0";
SqlCommand strParentFolderIDcmd = new SqlCommand(strParentFolderID, con);
strParentFolderIDcmd.Parameters.AddWithValue("@Name", parentFolder);
parentFolderID = Convert.ToString(strParentFolderIDcmd.ExecuteScalar());
}
else
{
parentFolderID = libraryName;
}
documentArray[rowCount] = "[{"id":"" + ID + "","parent":"" + parentFolderID + "","text":"" + name + "","data":"" + displayOrder + ""}]";
rowCount = rowCount + 1;
}
con.Close();
return documentArray;
}
else
{
con.Close();
return null;
}
}
else
{
con.Close();
return null;
}
}
Javascript:
<script type="text/javascript">
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const libraryID = urlParams.get('ID');
$('#libraryTreeview').jstree({
"core": {
"data": {
type: "POST",
url: "LibraryFiles.aspx/getChildren",
data: '{"libraryID":"' + libraryID + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.d);
data.d;
$(data).each(function () {
return { "id": this.id };
});
}
},
"themes": {
dots: false,
//"variant": "large",
//"responsive": true
},
"types": {
"FDR": {
"icon": "WebsiteImages/LibraryIcons/Folder48.png"
},
"IMG": {
"icon": "WebsiteImages/LibraryIcons/Image48.png"
}
},
"plugins": ["types", "themes"]
}
});
</script>