0

私は kendo ui と asp.net mvc4 razor.in を使用して開発されたグリッドを持っています。そこでは、asp.net mvc の代わりに剣道グリッドの html 構文を使用しました。

これは私のグリッドのコードです

    <div id="example" class="k-content">
    <div id="batchgrid">
    </div>
</div>
<script type="text/javascript" lang="javascript">
    $("#batchGrid").click(function () {
        var crudServiceBaseUrl = "http://demos.kendoui.com/service",
                        dataSource = new kendo.data.DataSource({
                            transport: {
                                read: {
                                    url: crudServiceBaseUrl + "/Products",
                                    dataType: "jsonp"
                                },
                                update: {
                                    url: crudServiceBaseUrl + "/Products/Update",
                                    dataType: "jsonp"
                                },
                                destroy: {
                                    url: crudServiceBaseUrl + "/Products/Destroy",
                                    dataType: "jsonp"
                                },
                                create: {
                                    url: crudServiceBaseUrl + "/Products/Create",
                                    dataType: "jsonp"
                                },
                                parameterMap: function (options, operation) {
                                    if (operation !== "read" && options.models) {
                                        return { models: kendo.stringify(options.models) };
                                    }
                                }
                            },
                            batch: true,
                            pageSize: 20,
                            schema: {
                                model: {
                                    id: "ProductID",
                                    fields: {
                                        ProductID: { editable: false, nullable: true },
                                        ProductName: { validation: { required: true} },
                                        UnitPrice: { type: "number", validation: { required: true, min: 1} },
                                        UnitsInStock: { type: "number", validation: { min: 0, required: true} },
                                        Discontinued: { type: "boolean" },
                                        TotalStock: { type: "number" }
                                    }
                                }
                            },
                            //                            group: {
                            //                                field: "UnitPrice", aggregates: [
                            //                                        { field: "UnitPrice", aggregate: "sum" },
                            //                                        { field: "TotalStock", aggregate: "sum" }
                            //                                     ]
                            //                            },
                            aggregate: [{ field: "TotalStock", aggregate: "sum"}]

                        });

        $("#batchgrid").kendoGrid({
            dataSource: dataSource,
            dataBound: onDataBound,

            navigatable: true,

            filterable: {
                messages: {
                    and: "And",
                    or: "Or",
                    filter: "Apply filter",
                    clear: "Clear filter",
                    info: "Filter by"
                },
                extra: false, //do not show extra filters
                operators: { // redefine the string operators
                    string: {
                        contains: "Contains",
                        doesnotcontain: "Doesn't contain",
                        startswith: "Starts With",
                        endswith: "Ends"
                    },
                    number: {
                        eq: "Is Equal To",
                        neq: "Not equal to",
                        gte: "Greater than or equal to",
                        lte: "Less than or equal to",
                        gt: "Greater than",
                        lt: "Less than"
                    }
                }
            },

            reorderable: true, //not working

            selectable: "multiple",

            pageable: {
                refresh: true,
                pageSizes: [5, 10, 20, 50, 100]
            },

            height: 430,
            width: 300,

            toolbar: [
                    {
                        name: "my-create",
                        text: "Add new record"
                    },
                    {
                        name: "save",
                        text: "save changes"
                    },
                    {
                        name: "cancel",
                        text: "cancel changes"
                    },
                    {
                        name: "export",
                        text: "Export To Excel"
                    }
            ],

            columns: [
            //                            { field: "ProductID", title: "No", width: "90px" },
                            {title: "No", template: "#= ++record #", width: 45 },
                            { field: "ProductName", title: "Product Name", width: "350px" },
                            { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "130px" },
                            { field: "UnitsInStock", title: "Units In Stock", width: "150px" },
                            { field: "Discontinued", title: "Purchase", width: "110px" },
                            { field: "TotalStock", title: "Total Stock", width: "150px", footerTemplate: "Total : #= kendo.toString(sum, 'C') #", format: "{0:c2}" }
            //{ command: ["destroy"], title: "&nbsp;", width: "175px" }
            ],


            export: {
                cssClass: "k-grid-export-image",
                title: "people",
                createUrl: "/Home/ExportToExcel",
                downloadUrl: "/Home/GetExcelFile"
            },

            groupable: {
                messages: {
                    empty: "Drop columns here"
                }
            }, //not working

            columnMenu: {
                sortable: true,
                filterable: true,
                messages: {
                    columns: "Hide/Show Columns",
                    filter: "Apply filter",
                    sortAscending: "Sort (asc)",
                    sortDescending: "Sort (desc)"
                }
            },

            resizable: true,

            dataBinding: function () {
                record = (this.dataSource.page() - 1) * this.dataSource.pageSize();
            },

            sortable: {
                mode: "multiple"
            },
            sort: { field: "ProductID", dir: "asc" },

            editable: { mode: "incell", createAt: "bottom" }
        });

        //custom global variables
        newRowAdded = false;
        checkedOnce = false;

        var grid = $("#batchgrid").data("kendoGrid");
        $(".k-grid-my-create", grid.element).on("click", function (e) {

            window.newRowAdded = true;

            var dataSource = grid.dataSource;
            var total = dataSource.data().length;
            dataSource.insert(total, {});
            dataSource.page(dataSource.totalPages());
            grid.editRow(grid.tbody.children().last());
        });

        grid.bind("saveChanges", function () {
            window.newRowAdded = false;
            //                        var grid = $("#batchgrid").data("kendoGrid");
            //                        grid.dataSource.sort({ field: "ProductID", dir: "asc" });
            //            GetValOf();

            //            var grid = $('#batchgrid').data("kendoGrid");
            //            var total = 0;
            //            $.each(grid.dataSource.view(), function () {
            //                total += this.TotalStock;
            //            });
            //            alert(total);
        });

        grid.bind("cancelChanges", function () {
            window.newRowAdded = false;
        });

        $(".k-grid-export", "#batchgrid").bind("click", function (ev) {
            // your code
            //            alert("Hello");
            var grid = $("#batchgrid").data("kendoGrid");
            grid.dataSource.pageSize(parseInt($("#batchgrid").data("kendoGrid").dataSource.data().length));
            excelImport();
        });
    });
</script>

しかし、グリッド データを Excel にインポートするためのサンプル コードを入手し、そこで asp.net mvc 構文を使用しました。

これがコードです。

@(
 Html.Kendo().Grid(Model).Name("Grid")
                .DataSource(ds => ds.Ajax()
                                .Model(m =>
                                {
                                    m.Id(p=>p.ProductID);                              
                                })
                        .Read(r => r.Action("Read", "Home"))
                )
                .ToolBar(toolBar => 
                    toolBar.Custom()
                        .Text("Export To Excel")
                        .HtmlAttributes(new { id = "export" })
                        .Url(Url.Action("Export", "Home", new { page = 1, pageSize = "~", filter = "~", sort = "~" }))
                )
                .Columns(columns =>
                {
                    columns.Bound(p => p.ProductID);
                    columns.Bound(p => p.ProductName);
                    columns.Bound(p => p.UnitPrice).Format("{0:c}");
                    columns.Bound(p => p.QuantityPerUnit);
                })
                .Events(ev => ev.DataBound("onDataBound"))
                .Pageable()
                .Sortable()
                .Filterable()
)

しかし、私の問題は、グリッドに以下のコード行を追加する必要があることです(上記の最初のコードで)。

.ToolBar(toolBar => 
                        toolBar.Custom()
                            .Text("Export To Excel")
                            .HtmlAttributes(new { id = "export" })
                            .Url(Url.Action("Export", "Home", new { page = 1, pageSize = "~", filter = "~", sort = "~" }))
                    )

しかし、私はこの一行にこだわっています

.Url(Url.Action("Export", "Home", new { page = 1, pageSize = "~", filter = "~", sort = "~" }))

誰かが私のhtmlグリッドでこのコードを使用する方法を教えてください..

4

1 に答える 1