从角形表数据构建表

时间:2015-10-13 22:30:14

标签: javascript html angularjs forms

所以我正在尝试构建一个表,该表获取输入的表单数据并存储在客户端上,并将每个输入属性推送到一个组中以创建一个对象。从那里开始,使用ng-repeat构建表。代码开始在下面,但没有任何事情发生。有人可以帮忙吗?

                <form class="addClaim" ng-submit="submitClaim(claimInfo)">
                    <div class="form-group">
                        <label for="beneSelect">Select your benefit</label>
                        <select class="form-control" id="beneSelect" >
                            <option ng-repeat="descr in claim.claimBenes" data-ng-model="claimInfo.providerName">{{ descr.descr }}</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="start">Date of Service</label>
                        <div>
                            <input type="text" class="form-control" id="start" placeholder="--/--/----" data-ng-model="claimInfo.fromDate" style="width: 240px;">
                            <span>To</span>
                            <input type="text" class="form-control" id="end" placeholder="--/--/---- (optional)" data-ng-model="claimInfo.toDate" style="width: 240px;">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="providerName">Provider Name</label>
                        <input type="text" class="form-control" id="providerName" data-ng-model="claimInfo.provider">
                    </div>
                    <div class="form-group">
                        <label for="forWhom">For Whom</label>
                        <input type="text" class="form-control" id="forWhom" data-ng-model="claimInfo.who">
                    </div>
                    <div class="form-group" ng-show="claimInfo.benefCode == 'HCFSA'">
                        <label for="age">Age</label>
                        <input type="text" class="form-control" id="age" data-ng-model="claimInfo.who">
                    </div>                    

                    <div class="form-group">
                        <label for="claimAmount">Amount</label>
                        <input type="text" class="form-control" id="claimAmount" data-ng-model="claimInfo.amount">
                    </div>
                    <div class="form-group">
                        <label for="claimComment">Comments</label>
                        <textarea class="form-control" rows="5" id="claimComment" data-ng-model="claimInfo.comments"></textarea>                        
                    </div>
                    <div class="form-group">
                        <label class="control-label"></label>
                        <div>
                            <input type="button" class="btn btn-primary" ng-click="saveClaim()" value="add claim" style="float: right;">
                        </div>
                    </div>
                </form>

表格:

<div class="claimedTable" style="background-color: #ffffff; color: black;">
    <table class="table">
        <thead>
            <tr>
                <th>service date</th>
                <th>provider</th>
                <th>amount</th>
                <th>edit</th>
                <th>delete</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="item in claimSubmit">
                <td>{{ claimInfo.fromDate }}</td>
                <td>{{ claimInfo.provider }}</td>
                <td>{{ claimInfo.amount }}</td>
                <td><a href="#"><i class="fa fa-pencil-square-o"></i></a></td>
                <td><a href="#"><i class="fa fa-trash-o"></i></a></td>            
            </tr>
        </tbody>
    </table>
</div>

和控制器:

$scope.claim = [];

claimsService.getClaim().then(function (results) {

    $scope.claim = results.data;

});

// submit all claim objects entered
$scope.claimsSubmit = [];

$scope.claimInfo = {
    id: "",
    benefitId: "",
    isSecIns: "",
    isNoResId: "",
    expenseTypeId: "",
    fromDate: "",
    toDate: "",
    provider: "",
    who: "",
    depId: "",
    age: "",   
    amount: "",
    comments: "",
    isOffset: ""
};

$scope.saveClaim = function() {
    $scope.claimsSubmit.push($scope.claimInfo);
    //clears scope so form is empty
    $scope.claimInfo = {};
}

当我在字段中输入数据并单击“提交”时,任何事情都不会离开,也不会发布到表格中。我想作为对象与数组的原因是因为表可能有多个行项,具体取决于表单输出和提交的次数。

似乎在某个地方简单我错了,但无法想象在哪里。有什么帮助吗?

非常感谢。

1 个答案:

答案 0 :(得分:1)

您的代码中存在一些问题。在这里清理了一下......

HTML

<form class="addClaim"> // ng-submit not needed in form tag

ng-repeat绑定应该是项目。不是声明提交。

<tr ng-repeat="item in claimsSubmit">
  <td>{{ item.fromDate }}</td>
  <td>{{ item.provider }}</td>
  <td>{{ item.amount }}</td>
  <td><a href="#"><i class="fa fa-pencil-square-o"></i></a></td>
  <td><a href="#"><i class="fa fa-trash-o"></i></a></td>            
</tr>

控制器

$scope.claim = []; // This can be removed.

claimsService.getClaim().then(function (results) {
    $scope.claim = results.data;
});

// submit all claim objects entered
$scope.claimsSubmit = [];
$scope.claimInfo = {}; // This just needs to create an object.

$scope.saveClaim = function() {
    $scope.claimsSubmit.push($scope.claimInfo);
    //clears scope so form is empty
    $scope.claimInfo = {};
}

这应该足以让表格为您填充表格。表格中明显缺少表格数据,但它会让你朝着正确的方向前进。