This is a migrated thread and some comments may be shown as answers.

[Solved] Changing the data loaded in the node when ExpandMode=WebService (WCF)

2 Answers 148 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
relper
Top achievements
Rank 1
relper asked on 21 Feb 2008, 05:37 PM

I’m using a ReadTreeView with ExpandMode set to WebService. When I expand the node, I use NodePopulating to pass the proper parameters. Then the web service method is called and returns the data to be loaded on the tree. The problem is that the data returned contains more information than just Text and Value which seem to be mapped automatically from members with the same name. But if this data doesn’t contain properties Text/Value or I want to load the tree node custom attributes collection with other values, how do I do that?

By the way, I’m using WCF and the return type is defined in a DataContract class, as in your example of the KB article at http://www.telerik.com/community/code-library/submission/b311D-bcaeek.aspx.


Thanks!

2 Answers, 1 is accepted

Sort by
0
Accepted
T. Tsonev
Telerik team
answered on 22 Feb 2008, 08:57 AM
Hello Ricardo,

We have updated the KB article to include custom attributes. The changes on the server-side are trivial - all we need to do is to add a dictionary property to the RadTreeNodeInfo class:

[DataMember] 
public IDictionary<stringobject> Attributes 
    get { return _attributes; } 
    set { _attributes = value; } 

On the client-side things are a bit more complicated. As this MSDN article explains, the DataContractJsonSerializer serializes dictionaries as arrays of key/value pairs, instead of JSON objects. Therefore we need a helper routine on the client to help us get the attribute values:

function nodePopulated(sender, eventArgs) 
    var childNodes = eventArgs.get_node().get_nodes(); 
    for (var i = 0; i < childNodes.get_count(); i++) 
    { 
        var node = childNodes.getNode(i); 
        var attributeValue = getAttributeValue(node.get_attributes(), "TestAttribute"); 
        node.set_text(node.get_text() + "[" + attributeValue + "]"); 
    } 
     
function getAttributeValue(attributeCollection, key) 
    // The DataContractJsonSerializer serializes the dictionary as an 
    // array of key/value pairs and we cannot use the attributeCollection 
    // methods directly. 
    for (var i = 0; i < attributeCollection.get_count(); i++) 
    { 
        var keyValuePair = attributeCollection.getAttribute(i); 
        if (keyValuePair.Key == key) 
        { 
            return keyValuePair.Value; 
        } 
    } 
     
    return null

I hope this helps.

Best wishes,
Tsvetomir Tsonev
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
relper
Top achievements
Rank 1
answered on 22 Feb 2008, 03:38 PM
Thanks Tsvetomir! This will definitely help.
Tags
TreeView
Asked by
relper
Top achievements
Rank 1
Answers by
T. Tsonev
Telerik team
relper
Top achievements
Rank 1
Share this question
or