0

目标是将外部模板加载到 Kendo UI 网格的工具栏中。我不希望直接在视图本身中有任何脚本(如 Kendo 网站上的示例所示)。我一直在搞乱 $.get 但并没有完全成功。将模板分配给下面的工具栏的最佳方法是什么?

剑道版本 2012.3.1114,Visual Studio 2012

设置网格设置的脚本中的函数调用:

    var configureGrid = function() {
        var grid = $('#allItemsGrid').kendoGrid({
            dataSource: {
                type: 'json',
                transport: {
                    read: {
                        url: "api/GridApi/GetAll",
                        dataType: 'json',
                        type: 'GET',
                        contentType: "application/json; charset=utf-8"
                    }
                },
                schema: {
                    model: {
                        fields { {
                            MusicItemId: { type: "integer" },
                            Artist: { type: "string" },
                            Title: { type: "string" },
                            Genre: { type: "string" },
                            Format: { type: "string" },
                            Notes: { type: "string" },
                            Country: { type: "string" }
                        }
                    }
                },
                pageSize: 5
            },
            toolbar: // load external template????
            sortable: true,
            pageable: { pageSizes: [5, 10, 15] },
            columns: gridCols.GridColumns
        });

这是我在 MVC 项目根目录下的 Tmpl 文件夹中的一个模板:

    <script type="text/x-kendo-template" id="tmplTarget">
        <div class="toolbar">
            <label class="category-label" for="category">Show By Genre:</label>
            <input type="search" id="category"></input>
        </div>
        <div class="toolbar">
            <label class="category-label" for="category">Show By Format:</label>
            <input type="search" id="category2"></input>
        </div>
        <a class="k-button" id="searchCriteria" href="\\\#" onclick="test_fn()">Search</a>
    </script>

根据瑞恩的要求。下面的代码是重做的,只是为了看看我是否可以根据 kendo 文档使事情正常进行。编码:

    <!DOCTYPE html/>
    <html>
        <head>
            <title>The Grid</title>
            <script src="~/Scripts/jquery-1.8.3.min.js"></script>
            <script src="~/Scripts/kendo.web.min.js"></script>
            <script src="~/Scripts/templateLoader.js"></script>
            <link href="~/Content/kendo/2012.3.1114/kendo.common.min.css" rel="stylesheet" />
            <link href="~/Content/kendo/2012.3.1114/kendo.default.min.css" rel="stylesheet" />
        </head>
        <script type="text/javascript">
            templateLoader.loadExtTemplate("@Url.Content("~/Tmpl/Test.tmpl.html")");

            $(document).ready(function () {
                var _itemTemplate;
                $(document).bind("TEMPLATE_LOADED", function (e, path) {
                    console.log('Templates loaded');

                    _itemTemplate = kendo.template($('#template').html(), { useWithBlock: false });

            });
            $('#allItemsGrid').kendoGrid({
                dataSource: {
                    type: 'json',
                    transport: {
                        read: {
                            url: "", // left blank, not concerned with pulling data back for now
                            dataType: 'json',
                            type: 'GET',
                            contentType: "application/json; charset=utf-8"
                        }
                    },
                    schema: {
                        model: {
                            fields: {
                                MusicItemId: { type: "integer" },
                                Artist: { type: "string" },
                                Title: { type: "string" },
                                Genre: { type: "string" },
                                Format: { type: "string" },
                                Notes: { type: "string" },
                                Country: { type: "string" }
                            }
                        }
                    },
                    pageSize: 5
                },
                toolbar: [{ name: 'customTemplate', template: _itemTemplate }],
                sortable: true,
                pageable: { pageSizes: [5, 10, 15] },  // page size using pager
                columns: [
                { field: "Artist", title: "Aritst" },
                { field: "Title", title: "Title" },
                { field: "Genre", title: "Genre" },
                { field: "Format", title: "Format" },
                { field: "Notes", title: "Notes" },
                { field: "Country", title: "Country" }
                ]
            });
        });
    </script>

    <style scoped="scoped">
        #grid .k-toolbar {
            min-height: 27px;
        }
        .category-label {
            vertical-align: middle;
            padding-right: .5em;
            float: left;
        }
        #category {
            vertical-align: middle;
        }
        .toolbar {
            float: right;
            margin-right: .8em;
        }
    </style>
    <body>
        <div id="allContainer">
            <div id="allItemsGrid"></div>
        </div>
    </body>
</html>

这是 templateLoader.js 文件:

    var templateLoader = (function($, host) {
        return {
            loadExtTemplate: function(path) {
                var tmplLoader = $.get(path)
                    .success(function(result) {
                        $('body').append(result);
                    })
                    .error(function(result) {
                        alert("Error loading template...");
                });

                tmplLoader.complete(function() {
                    $(host).trigger("TEMPLATE_LOADED", [path]);
                });
            }
        };
    })(jQuery, document);

模板与上面发布的相同。显然这不起作用,因为它将结果附加到文档的正文中。我需要的是附加到网格工具栏中模板的结果。通过观察网络流量验证,成功拉下模板。

4

0 回答 0