Key Pairs in the Data Source

3 posts, 1 answers
  1. Bryan
    Bryan avatar
    28 posts
    Member since:
    Oct 2014

    Posted 15 Jul 2015 Link to this post

    I have a JSON string that I'm trying to convert to a datasource then bind to one of the controls. The string looks like this:

    var TestData =  [ {"Category": "1", "Attributes": [{"Attribute": "A", "Attribute": "B", "Attribute": "C" }] }]

     then the datasource declaration is like this:

        var thisData = new kendo.data.DataSource({
            data: $.parseJSON(TestData),
            schema: {
                model: {
                    fields: {
                        Category: { type: 'string' },
                        Attributes: {
                            Attribute: { type: 'string' }
                        },
                    }
                }
            }
        })​

     When I bind to a listview and try to loop through the values in a template, it always tells me the length of the Attributes list is 1 and displays the last value ("C"). Is there something wrong in the declaration? 

  2. Answer
    T. Tsonev
    Admin
    T. Tsonev avatar
    2772 posts

    Posted 17 Jul 2015 Link to this post

    Hi,

    The Attributes array consists of a single element so that's working pretty much as expected.
    The Attribute key is defined multiple times and consequently only the last value is taken into account.

    Perhaps what you mean to serialize is:
    [ {"Category": "1", "Attributes": [{"Attribute": "A"}, {"Attribute": "B"}, {"Attribute": "C"}] }]
     
    I hope this helps.

    Regards,
    T. Tsonev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  3. Kendo UI is VS 2017 Ready
  4. Bryan
    Bryan avatar
    28 posts
    Member since:
    Oct 2014

    Posted 17 Jul 2015 Link to this post

    That was it. Thanks!
Back to Top