使用AngularJs的Kendo UI:当用户选择网格中的行时,如何绑定文本框字段上的数据

时间:2016-04-12 07:56:26

标签: javascript angularjs kendo-ui kendo-ui-grid

我正在使用Kendo UI和角度网格应用程序。在我的应用程序中,我定义了Kendo TabStrip。在第一个选项卡中,我有带数据的Kendo UI网格,第二个选项卡包含适当的文本框字段,当用户选择网格中的某些行时,这些字段将被填充。我的网格在加载页面时填充了数据并且它可以正常工作,但数据绑定到文本框字段不起作用。当用户选择网格中的行时,如何绑定文本框字段上的数据?

这是我的JSON数据(位于单独的文件中):

[
  { "Id": 1, "AccountNo": "10236", "PostingDate": "20.01.2015", "MaturityDate": "24.01.2015", "Description": "description1", "DocumentType": "doc1" },
  { "Id": 2, "AccountNo": "10648", "PostingDate": "26.01.2015", "MaturityDate": "28.01.2015", "Description": "description2", "DocumentType": "doc2" },
  { "Id": 3, "AccountNo": "10700", "PostingDate": "22.01.2015", "MaturityDate": "25.01.2015", "Description": "description3", "DocumentType": "doc3" },
  { "Id": 4, "AccountNo": "10810", "PostingDate": "24.01.2015", "MaturityDate": "27.01.2015", "Description": "description4", "DocumentType": "doc4" },
  { "Id": 5, "AccountNo": "10101", "PostingDate": "29.01.2015", "MaturityDate": "30.01.2015", "Description": "description5", "DocumentType": "doc5" },
  { "Id": 6, "AccountNo": "10364", "PostingDate": "25.01.2015", "MaturityDate": "31.01.2015", "Description": "description6", "DocumentType": "doc6" }


]

这是我的角度服务(位于单独的文件中):

angular.module("app").factory('myService', function ($http) {

  return {
      getAll: function (onSuccess, onError) {
          return $http.get('/Scripts/app/data/json/master/masterGridData.js').success(function (data, status, headers, config) {
              onSuccess(data);
          }).error(function (data, status, headers, config) {
              onError(data);
          });
      }
  }


});

这是我的控制器(位于单独的文件中):

var app = angular.module("app", ["kendo.directives"]).controller("myController", function ($scope, myService) {

$scope.tabStrip = null;
$scope.$watch('tabStrip', function () {
    $scope.tabStrip.select(0);
});

$scope.masterDataSource = new kendo.data.DataSource({
    transport: {
        read: function (options) {
            url = "/Scripts/app/data/json/master/masterGridData.js",
            myService.getAll(function (data) {
                options.success(data);
            }).error(function (data) {
                options.error(data);
            })
        }
    },
    schema: {
        model: {
            id: "Id",
            fields: {
                Id: { type: "number" },
                AccountNo: { type: "string" },
                PostingDate: { type: "string" },
                MaturityDate: { type: "string" },
                Description: { type: "string" },
                DocumentType: { type: "string" }
            }
        }
    },
    pageSize: 16
});

$scope.gridMaster = {
    columns: [
            { field: "Id", hidden: true },
            { field: "AccountNo", title: "Account No", width: "77px", template: '<div style="text-align:left;">#= kendo.toString(AccountNo) #</div>' },
            { field: "PostingDate", title: "Posting Date", width: "70px" },
            { field: "MaturityDate", title: "Maturity Date" width: "70px" },
            { field: "Description", title: "Description", width: "170px" },
            { field: "DocumentType", hidden: true }
    ],
    dataSource: $scope.masterDataSource,
    selectable: true,
    filterable: true,
    scrollable: true,
    pageable: {
        pageSize: 16,
        //refresh: true,
        pageSizes: ["50", "100", "200", "All"]
    },
    toolbar: [{
        name: "create"
    }]
};


});

这是我的HTML:

<html>
<head>
    <!-- css and javaScript files -->
</head>
<body ng-app="app" ng-controller="myController">
     <div class="divH3Style">
         <h3 class="h3LabelForm">Grid Master</h3>
     </div>
     <div id="tabstrip" class="k-tabstrip-wrapper" data-kendo-tab-strip="tabStrip">
                            <ul>
                                <li>Overview</li>
                                <li>Update</li>
                            </ul>


                        <div id="tabstrip-1">
                            <div id="gridMaster" kendo-grid k-options="gridMaster" k-data-source="masterDataSource">
                            </div>
                        </div>

                        <div id="tabstrip-2" style="overflow: hidden">
                            <div id="tabStrip2Half1">
                                <div class="divHeightStyle">
                                    <label for="accountNumber" class="labelTextSize">Account Number:</label>
                                    <input id="accountNumber" type="number" class="k-textboxField" name="accountNumber" ng-model="masterDataSource.data.AccountNo" placeholder="Account Number" />
                                </div>
                                <div class="datepickerStyle">
                                    <label for="postingDate" class="labelTextSize">Posting Date:</label>
                                    <input id="postingDate" class="k-datetimepickerMaster" name="postingDate" ng-model="masterDataSource.data.PostingDate" />
                                </div>
                                <div class="divHeightStyle">
                                    <label for="desccription" class="labelTextSize">Description:</label>
                                    <input id="desccription" type="text" class="k-textboxField" name="description" placeholder="Description" ng-model="masterDataSource.data.Description" />
                                </div>
                                <div class="datepickerStyle">
                                    <label for="maturityDate" class="labelTextSize">Maturity Date:</label>
                                    <input id="maturityDate" class="k-datetimepickerMaster" name="maturityDate" ng-model="masterDataSource.data.MaturityDate" />
                                </div>
                            </div>
                            <div id="tabStrip2Half2">
                                <div class="divHeightStyle">
                                    <label for="documentType" class="labelTextSize">Document Type:</label>
                                    <input id="documentType" type="text" class="k-textboxField" name="documentType" placeholder="Document Type" ng-model="masterDataSource.data.DocumentType" />
                                </div>

                                <div>
                                    <button type="button" id="saveDataMasterGrid" class="k-button buttonSaveCancel" onclick="saveDataMasterGrid()">Save</button>
                                    <button type="button" id="cancelDataMasterGrid" class="k-button buttonSaveCancel" onclick="cancelButtonMasterGrid()">Cancel</button>
                                </div>
                            </div>
                        </div>
                    </div>
</body>
</html>

任何帮助都非常有用。

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。我在$ scope.gridMaster中添加了更改事件函数:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#808080"/>
    <stroke android:width="3dp" android:color="#B1BCBE" />
    <corners android:radius="20dp"/>
    <padding android:left="0dp" android:top="0dp"
             android:right="0dp" android:bottom="0dp" />
</shape>      

我在HTML页面中更改了ng-model:

$scope.gridMaster = {
    ...
    change: function () {
        var dataItem = this.dataItem(this.select());
        $scope.accountNumber = dataItem.AccountNo;
        $scope.postingDate = dataItem.PostingDate;
        $scope.description = dataItem.Description;
        $scope.maturityDate = dataItem.MaturityDate;
        $scope.documentType = dataItem.DocumentType;
    }
}