Question

I started working on this from somebody else's code. So the dataset was filled around. My job was to add functionality to expand a node on Load based on a database value. It took me awhile and this Jquery worked when I completely didn't think i would.

 onPostInit: function(isReloading, isError) {
          $("#test-div").dynatree('getRoot').visit(function(node){
             if (node.data.IsExpandable === true) {
                 node.expand(true);               
             }                      
         });

IsExpandable represents a bool db column in my database. I did not think Jquery would be able to pick up on that field but it did. I know the controller code made it possible. Can anyone help explain why? Because right now I am a little iffy as to what AsEnumerable() is doing and what r.Field does. Here's the controller

 //Here Ds equals a filled dataset              //TreeData is model class w/ these properties
  var myData = Ds.Tables[0].AsEnumerable().Select(r => new TreeData
    {
        title = r.Field<string>("Name"),
        key = r.Field<int>("CategoryId"),
        ParentID = r.Field<int?>("ParentId"),
        IsExpandable = r.Field<bool?>("IsExpandable"),
        icon = false,
        addClass = "custom1" 
    });
Was it helpful?

Solution

A dynatree tree can be initialised in many different ways. Some of these require that you supply a tree of javascript objects (see section 4.2 of the online documentation). In your example the title, key, icon and addClass properties are standard properties that the dynatree plugin uses to render the node. The ParentID and IsExpandable properties are custom properties that one of your developers added to the object to add custom behaviour.

The AsEnumerable is a DataTable extension method that lets you use LINQ queries against the rows in a DataTable. Field is a DataRow method that lets you access the fields in a row.

So what's happening in the second snippet is that all a dynatree node object is created for each row in the data table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top