【问题标题】:Build table from angular form data从角度形式数据构建表格
【发布时间】:2015-10-14 07:09:18
【问题描述】:

所以我正在尝试构建一个表格,该表格将输入的表单数据存储在客户端上,并将每个输入属性推送到一个组中以创建一个对象。从那里,表格是使用 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 = {};
}

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

似乎在某个简单的地方我出错了,但不知道在哪里。有什么帮助吗?

非常感谢。

【问题讨论】:

  • 您是否尝试过:{{ item.fromDate }}?
  • 是的,我确实尝试过那条路线,但没有运气。
  • 检查您的拼写错误...使用ng-submit在此处plnkr.co/edit/ZMOqLs5rPx9GFwrmUDtj?p=preview进行修改
  • 是的,claimSubmit 还是 claimSubmit?
  • @charlietfl - 因此,基于那个 plunker,我试图弄清楚如果浏览器意外刷新,我该如何保持这种状态。我想要做的是像你展示的那样将每个对象保存为一个对象,但是然后在我的 html 中,我将添加一个带有函数的按钮,然后将数据作为对象数组推送到 API。这可能吗?

标签: javascript html angularjs forms


【解决方案1】:

您的代码中存在一些问题。这里稍微整理了一下……

HTML

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

ng-repeat 绑定应该是 item。不主张提交。

<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 = {};
}

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

【讨论】:

  • 如果浏览器意外刷新,我如何保持这种状态。我想要做的是像你展示的那样将每个对象保存为一个对象,但是然后在我的 html 中,我将添加一个带有函数的按钮,然后将数据作为对象数组推送到 API。这可能吗?
  • 可以将数据持久化到浏览器localStorage中。只需创建一个 keyup 或 blur 事件侦听器,将表对象作为 JSON 字符串写入存储。当页面加载时,您可以检查它是否存在,如果存在的话,您可以使用在那里找到的数据填充表对象。在表单提交时,您可以删除存储值。
  • 我不同意使用按钮事件而不是 ng-submit.... 可以让您捕获对用户更友好的键盘提交
  • @charlietfl 同意。不过,我不想更改太多此代码。只是想为这个问题启动并运行它。里面有不少东西需要更新。
  • $http.post( url, $scope.claimInfo) 应该这样做...使用回调将数据推送到数组...哦,使用angular.copy 这样你就不会一直推送同一个对象
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-10
  • 1970-01-01
  • 2016-03-30
  • 2016-03-26
  • 2018-05-13
  • 2020-07-15
  • 1970-01-01
相关资源
最近更新 更多