使用 AngularJs 的 Kendo UI:如何在用户选择网格中的行时绑定文本框字段上的数据 [英] Kendo UI with AngularJs: How to bind data on textbox fields when user select row in a grid

查看:13
本文介绍了使用 AngularJs 的 Kendo UI:如何在用户选择网格中的行时绑定文本框字段上的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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) {返回 {getAll: 函数 (onSuccess, onError) {返回 $http.get('/Scripts/app/data/json/master/masterGridData.js').success(function (data, status, headers, config) {成功(数据);}).error(function (data, status, headers, config) {错误(数据);});}}});

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

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({运输: {阅读:功能(选项){url = "/Scripts/app/data/json/master/masterGridData.js",myService.getAll(函数(数据){options.success(数据);}).错误(函数(数据){选项错误(数据);})}},架构:{模型: {id: "身份证",字段:{id: { type: "number" },帐号:{ 类型:字符串"},PostingDate: { type: "string" },MaturityDate: { type: "string" },描述:{ 类型:字符串"},文档类型:{ 类型:字符串"}}}},页面大小:16});$scope.gridMaster = {列: [{字段:Id",隐藏:真实},{ 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" },{字段:描述",标题:描述",宽度:170px"},{ 字段:DocumentType",隐藏:true }],数据源:$scope.masterDataSource,可选:真,可过滤:真实,可滚动:真实,可分页:{页面大小:16,//刷新:真,页面大小:[50"、100"、200"、全部"]},工具栏:[{名称:创造"}]};});

这是我的 HTML:

<头><!-- css 和 javaScript 文件 --><body ng-app="app" ng-controller="myController"><div class="divH3Style"><h3 class="h3LabelForm">Grid Master</h3>

<div id="tabstrip" class="k-tabstrip-wrapper" data-kendo-tab-strip="tabStrip"><ul><li>概述</li><li>更新</li><div id="tabstrip-1"><div id="gridMaster" kendo-grid k-options="gridMaster" k-data-source="masterDataSource">

<div id="tabstrip-2" style="溢出:隐藏"><div id="tabStrip2Half1"><div class="divHeightStyle"><label for="accountNumber" class="labelTextSize">账号:</label><input id="accountNumber" type="number" class="k-textboxField" name="accountNumber" ng-model="masterDataSource.data.AccountNo" placeholder="账户号"/>

<div class="datepickerStyle"><label for="postingDate" class="labelTextSize">发布日期:</label><input id="postingDate" class="k-datetimepickerMaster" name="postingDate" ng-model="masterDataSource.data.PostingDate"/>

<div class="divHeightStyle"><label for="desccription" class="labelTextSize">说明:</label><input id="desccription" type="text" class="k-textboxField" name="description" placeholder="Description" ng-model="masterDataSource.data.Description"/>

<div class="datepickerStyle"><label for="maturityDate" class="labelTextSize">到期日:</label><input id="maturityDate" class="k-datetimepickerMaster" name="maturityDate" ng-model="masterDataSource.data.MaturityDate"/>

<div id="tabStrip2Half2"><div class="divHeightStyle"><label for="documentType" class="labelTextSize">文档类型:</label><input id="documentType" type="text" class="k-textboxField" name="documentType" placeholder="Document Type" ng-model="masterDataSource.data.DocumentType"/>

<div><button type="button" id="saveDataMasterGrid" class="k-button buttonSaveCancel" onclick="saveDataMasterGrid()">保存</button><button type="button" id="cancelDataMasterGrid" class="k-button buttonSaveCancel" onclick="cancelButtonMasterGrid()">取消</button>

任何帮助都会非常有用.

解决方案

我正在解决那个问题.我在 $scope.gridMaster 中添加了更改事件功能:

$scope.gridMaster = {...改变:函数(){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;}}

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

<div id="tabStrip2Half1"><div class="divHeightStyle"><label for="accountNumber" class="labelTextSize">账号:</label><input id="accountNumber" type="number" class="k-textboxField" name="accountNumber" ng-model="accountNumber" placeholder="账户号"/>

<div class="datepickerStyle"><label for="postingDate" class="labelTextSize">发布日期:</label><input id="postingDate" class="k-datetimepickerMaster" name="postingDate" ng-model="postingDate"/>

<div class="divHeightStyle"><label for="desccription" class="labelTextSize">说明:</label><input id="desccription" type="text" class="k-textboxField" name="description" placeholder="Description" ng-model="description"/>

<div class="datepickerStyle"><label for="maturityDate" class="labelTextSize">到期日:</label><input id="maturityDate" class="k-datetimepickerMaster" name="maturityDate" ng-model="maturityDate"/>

<div id="tabStrip2Half2"><div class="divHeightStyle"><label for="documentType" class="labelTextSize">文档类型:</label><input id="documentType" type="text" class="k-textboxField" name="documentType" placeholder="Document Type" ng-model="documentType"/>

<div><button type="button" id="saveDataMasterGrid" class="k-button buttonSaveCancel" onclick="saveDataMasterGrid()">保存</button><button type="button" id="cancelDataMasterGrid" class="k-button buttonSaveCancel" onclick="cancelButtonMasterGrid()">取消</button>

I am working with Kendo UI and angular grid application. In my application I define Kendo TabStrip. In first tab I have Kendo UI grid with data and second tab contains appropriate textbox fields, which are to be filled when user select some row in a grid. My grid is populated with data when load page and it work perfectly, but data binding to textbox fields not working. How to bind data on textbox fields when user select row in a grid?

This is my JSON data (which is in separate file):

[
  { "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" }


]

This is my angular service (which is in separate file):

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);
          });
      }
  }


});

This is my controller (which is in separate file):

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"
    }]
};


});

This is my 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>

Any help will be very useful.

解决方案

I am solve that problem. I was added change event function in $scope.gridMaster:

$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;
    }
}

And I was change ng-model in my HTML page:

<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="accountNumber" 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="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="description" />
                            </div>
                            <div class="datepickerStyle">
                                <label for="maturityDate" class="labelTextSize">Maturity Date:</label>
                                <input id="maturityDate" class="k-datetimepickerMaster" name="maturityDate" ng-model="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="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>

这篇关于使用 AngularJs 的 Kendo UI:如何在用户选择网格中的行时绑定文本框字段上的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆