1

I have a json object in angular

[
    {
        "orderNumber": 5821784,
        "accountNumber": 167067702,
        "taskCodes": {
            "3": 1,
            "5": 1,
            "23": 1,
            "51": 1,
            "71": 1
        },
        "orderCode": "TC",
        "orderState": 0
    },
    {
        "orderNumber": 5821785,
        "accountNumber": 167067703,
        "taskCodes": {
            "23": 1,
            "41": 1,
            "51": 1
        },
        "orderCode": "TC",
        "orderState": 0
    }
]

i need to update, delete and add new taskCodes in taskCodes list.

Here is my html code :-

    <table class="table table-bordered">

                        <thead>
                            <tr>
                                <th>Code</th>
                                <th>Quantity</th>
                            </tr>
                        </thead> 
                        <tbody>
                            <tr data-ng-repeat="(key, taskCode) in Obj.taskCodes">
                                <td>
                                    <span data-ng-hide="editMode[$index]">{{key}}</span>
                                    <input type="text" class="input-sm form-control" data-ng-model="key" data-ng-show="editMode[$index]">
                                </td>
                                <td>
                                    <span data-ng-hide="editMode[$index]"> {{taskCode}} </span>
                                    <input type="number" class="input-sm form-control" data-ng-model="taskCode" data-ng-show="editMode[$index]" >
                                </td> 
                                <td><span class="glyphicon glyphicon-edit accordion-toggle clickable" data-ng-hide="editMode[$index]" data-ng-click="edit($index)"></span>
<span class="glyphicon glyphicon-ok accordion-toggle clickable" data-ng-hide="!editMode[$index]" data-ng-click="addItem($index)"></span>
</td>
                            </tr>
                        </tbody>
                    </table>

Controller Edit function :-

$scope.items = [];

    $scope.addItem = function () {
     //Need code here
    }

    $scope.editMode=[];
    $scope.edit = function(index) {
        $scope.editMode[index] = true;
    };

Can someone please help me to do all that. Thanks in advance

4
  • It should work in the way the code is right now. I think this is not the actual code however. I bet the $scope.items variable is undefined in the moment you try to push, test that. Commented Apr 19, 2015 at 19:58
  • I agree with Victor, might be related to some other code. Also not related I guess but there is a semicolon missing at the end of $scope.addItem = function () {} ... Commented Apr 19, 2015 at 20:01
  • @victor and Sebasti now i update my question please check it. and thanks for your reply Commented Apr 19, 2015 at 20:31
  • Try the code you had before, but do: if (!(Object.prototype.toString.call($scope.items) === '[object Array]')) { $scope.items = []; } As a first operation. Commented Apr 19, 2015 at 20:39

2 Answers 2

1

Got the answer

var o = { "23": 1, "41": 1, "51": 1 }

console.log(o)

delete o[41];

console.log(o)

o[56] = 5; console.log(o)

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

Comments

0

You can try it following way!

 $scope.jsonObject.key.push(value);

Replace jsonObject with your object name and same with the key and value pair! I hope the solution would work for you!

1 Comment

@konrda i just update my question please check now. and thanks for your reply

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.