简体   繁体   English

如何在Angular中将表数据转换为Json格式

[英]How to convert table data into Json format in Angular

I am trying to convert data present in an HTML table into JSON link . 我正在尝试将HTML表中存在的数据转换为JSON 链接 I'm getting undefined values in object. 我在对象中得到未定义的值。

HTML code: HTML代码:

<table colspan="2" border="1">
      <tr>
        <th>Source</th>
         <th>Destination</th
      </tr>
      <tr ng-repeat="col in MapCol">
         <td>
      <select ng-model="col.value" ng-init="col.value=MainData.headers[$index].header">
<option ng-selected="{{head.header == col.value}}" ng-repeat="head in MainData.headers">{{head.header}}
</option>
        </select>
           </td>
           <td>{{col.ColName}}</td>

           </tr>

        </table>
        <br/>
        <button ng-click="map();">Map</button>

Controller code: 控制器代码:

var app = angular.module("ShrTest", []);
app.controller("Test1", function ($scope) {
$scope.MainData = { "name": "1-06082015185338.txt", "headers": [{ "header": "Name" }, { "header": "Age" }, { "header": "Address" }], "records": [{ "Name": "Paul", "Age": "23", "Address": "1115 W Franklin" }, { "Name": "Bessy the Cow", "Age": "5", "Address": "Big Farm Way" }, { "Name": "Zeke", "Age": "45", "Address": "W Main St" }] };

$scope.MapCol = [{ "ColName": "Col1" }, { "ColName": "Col2" }, { "ColName": "Col3" }];


$scope.map=function(){
    $scope.fields=[{"source":$scope.value,"destination":$scope.ColName}];
                    console.log(" $scope.fields..", $scope.fields);
}
});

JS JS

$scope.map=function(){
        $scope.fields = [];
        for(i in $scope.MapCol){
          var obj = $scope.MapCol[i]; $scope.fields.push({"source":obj.value,"destination":obj.ColName})
        }

                        console.log($scope.fields);
    }

Here is Modified fiddle: 这是修改过的小提琴:

Demo Here 在这里演示

I hope this will help you. 我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM