Creating a basic data grid with Kendo UI and AngularJS

后端 未结 2 1946
后悔当初
后悔当初 2021-02-03 15:25

I\'m experimenting with AngularJS. I want to show a basic Kendo Grid. I\'m trying to do this using pure directives. With that in mind, I\'ve looked at the Kendo UI / Angular JS

2条回答
  •  北荒
    北荒 (楼主)
    2021-02-03 15:48

    You can also setup a Kendo DataSource as an AngularJS Service using the Factory method and inject this into your Controller to conform to AngularJS best practices and patterns.

    Full source code and live demo: http://goo.gl/6Z9jop

    Customer.cshtml

    @{
       ViewBag.Title = "Index";
    }
    
    

    {{title}}

    customerController.js

    'use strict';
    
    northwindApp.controller('customerController',
        function ($scope, $rootScope, $location, customerDataSource)
        {
            customerDataSource.filter({}); // reset filter on dataSource everytime view is loaded
    
            var onClick = function (event, delegate)
            {
                var grid = event.grid;
                var selectedRow = grid.select();
                var dataItem = grid.dataItem(selectedRow);
    
                if (selectedRow.length > 0)
                {
                    delegate(grid, selectedRow, dataItem);
                }
                else
                {
                    alert("Please select a row.");
                }
            };
    
            $scope.toolbarTemplate = kendo.template($("#toolbar").html());
    
            $scope.save = function (e)
            {
                onClick(e, function (grid)
                {
                    grid.saveRow();
                    $(".toolbar").toggle();
                });
            };
    
            $scope.cancel = function (e)
            {
                onClick(e, function (grid)
                {
                    grid.cancelRow();
                    $(".toolbar").toggle();
                });
            },
    
            $scope.details = function (e)
            {
                onClick(e, function (grid, row, dataItem)
                {
                    $location.url('/customer/edit/' + dataItem.CustomerID);
                });
            },
    
            $scope.edit = function (e)
            {
                onClick(e, function (grid, row)
                {
                    grid.editRow(row);
                    $(".toolbar").toggle();
                });
            },
    
            $scope.destroy = function (e)
            {
                onClick(e, function (grid, row, dataItem)
                {
                    grid.dataSource.remove(dataItem);
                    grid.dataSource.sync();
                });
            },
    
            $scope.onChange = function (e)
            {
                var grid = e.sender;
    
                $rootScope.lastSelectedDataItem = grid.dataItem(grid.select());
            },
    
            $scope.dataSource = customerDataSource;
    
            $scope.onDataBound = function (e)
            {
                // check if there was a row that was selected
                if ($rootScope.lastSelectedDataItem == null)
                {
                    return;
                }
    
                var view = this.dataSource.view(); // get all the rows
    
                for (var i = 0; i < view.length; i++)
                {
                    // iterate through rows
                    if (view[i].CustomerID == $rootScope.lastSelectedDataItem.CustomerID)
                    {
                        // find row with the lastSelectedProductd
                        var grid = e.sender; // get the grid
    
                        grid.select(grid.table.find("tr[data-uid='" + view[i].uid + "']")); // set the selected row
                        break;
                    }
                }
            };
        });
    

    customerDataSource.js

    'use strict';
    
    northwindApp.factory('customerDataSource',
        function (customerModel)
        {
            var crudServiceBaseUrl = "/odata/Customer";
    
            return new kendo.data.DataSource({
                type: "odata",
                transport: {
                    read: {
                        async: false,
                        url: crudServiceBaseUrl,
                        dataType: "json"
                    },
                    update: {
                        url: function (data)
                        {
                            return crudServiceBaseUrl + "(" + data.CustomerID + ")";
                        },
                        type: "put",
                        dataType: "json"
                    },
                    destroy: {
                        url: function (data)
                        {
                            return crudServiceBaseUrl + "(" + data.CustomerID + ")";
                        },
                        dataType: "json"
                    }
                },
                batch: false,
                serverPaging: true,
                serverSorting: true,
                serverFiltering: true,
                pageSize: 10,
                schema: {
                    data: function (data) { return data.value; },
                    total: function (data) { return data["odata.count"]; },
                    model: customerModel
                },
                error: function (e)
                {
                    alert(e.xhr.responseText);
                }
            });
        });
    

提交回复
热议问题