1

So I create a cached json object within an array with the following method in Angular:

$scope.saveClaim = function() {

//always set isOffset to false - empty string does not work for non-string objects in web api when field is required
$scope.claimInfo.isOffset = false;
$scope.claimsSubmit.push($scope.claimInfo);

//clears scope so form is empty
$scope.claimInfo = {
id: "",
benefitId: "",
isSecIns: "",
isNoResId: "",
expenseTypeId: "",
fromDate: "",
toDate: "",
provider: "",
who: "",
depId: "",
age: "",   
amount: "",
comments: "",
isOffset: ""

}; }

The idea is the user fills out a form, and at the end either selects to add another claim or submit a claim (the object). After each time the form is filled and user selects file or add another, the form clears and the user then enters more data. The results is an array of object(s) that look like:

[
  {
    "id": "",
    "benefitId": "",
    "isSecIns": "",
    "isNoResId": "",
    "expenseTypeId": "",
    "fromDate": "",
    "toDate": "",
    "provider": "",
    "who": "",
    "depId": "",
    "age": "",
    "amount": "",
    "comments": "",
    "isOffset": false
  }
]

If more than one claim is entered, then we get multiple objects with same properties.

Each claim is then displayed with limited data in info boxes that display only 3-4 of the properties.

So I am trying to figure best way to do 3 things. First, add a unique "id" to each object. Second, if the delete icon in the info box selected, then remove that object from the array and if the "edit" icon is selected in the info box, then all the relative properties that that object in the array is populated back to the form.

Googling best tries for this, but not sure how I can work with the json objects this for for now. Can some of you help me on this?

Thanks much.

1 Answer 1

1

Hard to give the best way. Probably comes down to your style and preferences. But here is one way to do it, to get you going.

Define your model. It will contain the claim that is bound to the form and an array of added claims.

$scope.viewModel = {
  claim: {},
  claims: []
};

Add a function that assigns a claim object with default values:

var resetClaim = function() {

  $scope.viewModel.claim = {
    name: '',
    city: ''
  };
};

resetClaim();

The form elements will use ng-model:

<input type="text" model="viewModel.claim.name">

We will use ng-repeat to show the added claims:

<tr ng-repeat="claim in viewModel.claims">

Our form will have two buttons:

<button type="submit" ng-click="saveClaim()">Save Claim</button>
<button type="button" ng-click="cancel()">Cancel</button>

The cancel button will just reset the form.

The saveClaim function will look like this:

$scope.saveClaim = function() {

  if (!isValidClaim()) return;

  $scope.viewModel.claim.id ? updateClaim() : saveNewClaim();

  resetClaim();
};

The isValidClaim function just checks if we have entered the requied fields. You could use form validation for this instead.

In this solution when saving a claim it could either be a new claim or an existing one that we have edited, and what we will do in the two cases will differ, so we need a way to tell what we are doing. Here we just check if it has an id. If it hasn't - it's a new claim. If it has, it's an existing.

To save a new claim we will do the following:

var saveNewClaim = function() {

  var newClaim = angular.copy($scope.viewModel.claim);
  newClaim.id = id++;
  $scope.viewModel.claims.push(newClaim);
};

Note that it's important that we use for example angular.copy to create a new copy of the claim that is bound to the view. Otherwise we would just push a reference to the same object to the claims array which is not good since we want to reset one of them.

In this example id is just a variable starting at 0 that we increment each time we create a new claim.

Each element in our ng-repeat will have an edit and a remove icon:

<tr ng-repeat="claim in viewModel.claims">
  <th>{{claim.id}}</th>
  <td>{{claim.name}}</td>
  <td>{{claim.city}}</td>
  <td><i class="glyphicon glyphicon-edit" ng-click="editClaim(claim)"></i></td>
  <td><i class="glyphicon glyphicon-remove" ng-click="removeClaim(claim)"></i></td>
</tr>

The removeClaim function simply takes a claim and removes it from the array:

$scope.removeClaim = function(claim) {

  var index = $scope.viewModel.claims.indexOf(claim);
  $scope.viewModel.claims.splice(index, 1);
};

The editClaim function will make a copy of the claim to edit and put it in the variable that is bound to the form:

$scope.editClaim = function(claim) {

  $scope.viewModel.claim = angular.copy(claim);
};

You can also do the following:

$scope.viewModel.claim = claim;

And when you edit the claim in the form it will update in the ng-repeat at the same time. But then you have no good way of canceling and the save button wouldn't be needed. So it depends on how you want it to work.

If you edit the claim in the form now and save, we will come back to the saveClaim function:

$scope.saveClaim = function() {

  if (!isValidClaim()) return;

  $scope.viewModel.claim.id ? updateClaim() : saveNewClaim();

  resetClaim();
};

This time the claim will have an id, so the updateClaim function will execute:

var updateClaim = function() {

  var claim = $scope.viewModel.claims.filter(function(c) {
    return c.id === $scope.viewModel.claim.id;
  })[0];

  angular.extend(claim, $scope.viewModel.claim);
};

It will retrieve the claim that we are editing from the claims array based on the id. We need to do this since we used angular.copy earlier and have two difference objects.

We will then use angular.extend to move all the new edited values to the claim that we pressed edit on in the ng-repeat.

Demo: http://plnkr.co/edit/yuNcZo7nUyxVsOyPTBEd?p=preview

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.