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

Datasource not binding to template

10 Answers 776 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Mangesh
Top achievements
Rank 1
Mangesh asked on 10 Dec 2011, 07:08 PM
Hi,

I've created one template and assign datasource to that template but data is not showing on webpage.
Following is my code:

 <script id="template" type="text/x-kendo-template">
            <div class="categoryBox">
                <div>
                    <strong>
                        #= categoryName #
                    </strong>                    
                    <a class="box-link" href="http://www.google.co.in">
                        Read More
                    </a>
                </div>
            </div>
            </script>
            <script type="text/javascript">
                $(document).ready(function () {

                    var template = kendo.template($("#template").html());
                    var category = [
                        { "categoryName": "Category1" },
                        { "categoryName": "Category2" },
                        { "categoryName": "Category3" },
                        { "categoryName": "Category4" },
                        { "categoryName": "Category5" },
                        { "categoryName": "Category6" }
                        ];

                    var dataSource = new kendo.data.DataSource({ data: category });

                    // read data from the remote service
                    dataSource.read();
                });
            </script>

Kind Regards,
Mangesh

10 Answers, 1 is accepted

Sort by
0
Accepted
Petyo
Telerik team
answered on 12 Dec 2011, 10:21 AM
Hello Mangesh,

 Your code initializes the template, but does not output its code anywhere. You need a dom element.

In addition to that, the template should also iterate the data from the datasource - this does not happen automatically. I used kendo.render utility method for that. 

See the attached example:
http://jsfiddle.net/underlog/5J3z5/1/

All the best,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Mangesh
Top achievements
Rank 1
answered on 14 Dec 2011, 11:52 AM
Hi Petyo,

Thank you so much for your assistance. I have successfully created my template now.
I'm moving forward to bind remote data now. I have a webservice. and link of the web service is http://localhost:14987/KendoDemo/WebService.asmx/Categories
which returns json data in string format. and I'm binding that data to the same template but not showing any data on web page.
Here's my code:-
(template is same as we have used before)
<script type="text/javascript">
            $(document).ready(function () {
                var template = kendo.template($("#template").html());
                var dataSource = new kendo.data.DataSource({
                    transport: {
                        read: {
                            url: "http://localhost:14987/KendoDemo/WebService.asmx/Categories"
                        }
                    }
                });

                dataSource.bind("change", function () {
                    $("#content").html(kendo.render(template, dataSource.view()));
                });

                dataSource.read();
                console.log(dataSource.view());

            });
            
        </script>



Please check the attachment for the web service output.
0
Petyo
Telerik team
answered on 14 Dec 2011, 12:45 PM
Hello Mangesh,

The web service does not return json. It returns XML, which contains a json string. 

Best wishes,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Mangesh
Top achievements
Rank 1
answered on 14 Dec 2011, 02:03 PM
Hi Petyo,

OK So I need to set content type in datasource. Am I right?
I have done according to this forum : http://www.kendoui.com/forums/framework/data-source/server-side-filtering-and-paging.aspx
but still not showing the data on webpage?
and my code is :
<script type="text/javascript">
            $(document).ready(function () {
                var template = kendo.template($("#template").html());
                var dataSource = new kendo.data.DataSource({
                    transport: {
                        read: {
                            type: "POST",
                            url: "http://localhost:14987/KendoDemo/WebService.asmx/Categories",
                            contentType: 'application/json; charset=utf-8',
                            datatype: "json"
                        }
                    }
                });

                dataSource.bind("change", function () {
                    $("#content").html(kendo.render(template, dataSource.view()));
                });

                dataSource.read();
                console.log(dataSource.view());

            });
            
        </script>

Thanks,
Mangesh
0
Petyo
Telerik team
answered on 14 Dec 2011, 02:31 PM
Hello Mangesh,

 The dataType transport parameter is case sensitive. 

In addition to that, you can bind to the dataSource error event, so that you can examine what goes wrong with the returned result:

dataSource.bind("error", function () {
console.log(arguments); 
});

Greetings,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Mangesh
Top achievements
Rank 1
answered on 21 Dec 2011, 08:55 AM
Hi Petyo,

Thanks for your help. It works. :)
Now, If I want to change the css class name of div element inside of template How can I do so? for e.g. in our case class name is "categoryBox". how can we change for different data of datasource? Lets assume There is different class for Category1, different class for Category2 and so on...

Kind Regards,
Mangesh
0
Petyo
Telerik team
answered on 21 Dec 2011, 03:00 PM
Hi Mangesh,

 You can perform any kind of logic by embedding JavaScript in the template. You can review the expressions template section in our demos. 

Regards,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Mangesh
Top achievements
Rank 1
answered on 21 Dec 2011, 03:21 PM
Hi Petyo,

Thanks for the help.
I did it by using following code:
<script id="template" type="text/x-kendo-tmpl">
                    # if (CategoryName == "Category 1") { #
                        <div class="GoldBox">
                    # } else { #
                        <div class="BlueBox">
                    # } #                                    
                       <div style="color:black;">
                            <strong>
                                #= CategoryName #
                            </strong>
                            #= Value #                   
                            <a class="box-link" href="http://www.google.co.in">
                                Read More
                            </a>
                       </div>
                      </div>             
 </script>

Kind Regards,
Mangesh
0
Mangesh
Top achievements
Rank 1
answered on 22 Dec 2011, 11:49 AM
Hi Petyo,

Two methods from different web services (.asmx) are giving some results back but first method is binding to template successfully but the other one is not. Please check the attachment for the files which clearly shows that web service1's method giving result as similar to web service2's method. The only difference is that web service 1 return data in IEnumerable<Category> format with the Category class name as data type whereas web service 2 did the same thing but it actually call the other external web service method to get the data, And also data is coming back from external web service and then I'll convert that data into IEnumerable<Category> format but this method's data is not binding to template. Only first web service method is binding to the template. So How can I bind 2nd web service method to template?
0
Mangesh
Top achievements
Rank 1
answered on 22 Dec 2011, 01:17 PM
Hi Petyo,

I managed to solve it.

Kind Regards,
Mangesh
Tags
Data Source
Asked by
Mangesh
Top achievements
Rank 1
Answers by
Petyo
Telerik team
Mangesh
Top achievements
Rank 1
Share this question
or