0

I am trying to add the json data to a table on clicking the + icon. There are 2 section namely Pick Stocks from where the stocks and price (which inturn are coming from data.json) has to be added to the table present in Manage Portfolio section.

First Section first section

Second Section second section So I want the stock name i.e. ADFI, ALAN etc and its respective price to be added to respective stock and price columns of table in second section when a respective + icon is clicked. I have tried adding but I am not entirely sure about how to go forward. Thank you. Here is the plunker

             <table>
                <tr>
                    <th>STOCK</th>
                    <th>PRICE</th>
                    <th>SHARES</th>
                    <th>WEIGHT</th>
                </tr>
                <tr ng-repeat="portfolio in portfolios">
                    <td ng-model="portfolio.stockfromdata"></td>
                    <td ng-model="portfolio.pricefromdata"></td>
                    <td>num</td>
                    <td ng-model="portfolio.weightfromprice"></td>
                </tr>
            </table>

Script

 $scope.portfolios = [];

    $scope.addStock = function() {
        $scope.portfolios.push([{
            "key": $scope.portfolio.stockfromdata,
            "value": $scope.portfolio.pricefromdata
            // "weightfromprice": $scope.weightfromprice
        }]);
    };
4
  • Your plunker is not at all clear. And where you get data $scope.portfolio. And $scope.portfolios.push([{ "key": $scope.portfolio.stockfromdata, "value": $scope.portfolio.pricefromdata // "weightfromprice": $scope.weightfromprice }]); this is not correct as you are uisng data in ui with key names it ahould be like $scope.portfolios.push([{ "stockfromdata": $scope.portfolio.stockfromdata, "pricefromdata": $scope.portfolio.pricefromdata // "weightfromprice": $scope.weightfromprice }]); Commented Jul 6, 2017 at 12:47
  • Can you tell me why the plnker is not clear and I have tried already what you said but it gives this error "Cannot read property 'stockfromdata' of undefined" Commented Jul 6, 2017 at 12:55
  • Initialise the object as {} so that you won't get error as above. Commented Jul 6, 2017 at 13:08
  • It is already initialized if you see the code. but error says "stockfromdata" is undefined Commented Jul 6, 2017 at 13:13

2 Answers 2

1

If you follow similarly of having object & showing key values in table then you can write following on ng-click of plus sign

$scope.addToTable = function(key, value){
  var mystock={key: value};
    if (!(key in $scope.stocksObj)){
    $scope.stocksObj[key] = value;
    }
}

<tr ng-repeat="(key, value) in stocksObj"><td>{{key}}</td><td>{{value}}</td></tr>

This's working plunk https://plnkr.co/edit/b3QECT1pvqnWwyQoEBTb?p=preview

But again this will not be useful if you need to have more fields in that stock. So better create object with properties as key, value, shares, weight, etc and push accordingly in array. The updated function will be

$scope.addToTable = function(key, value, index){
  var mystock={};
  mystock.key = key;
  mystock.value = value;
    console.log(index);
    if(indexes.indexOf(index) == -1){
        indexes.push(index);
        $scope.stocksArray.push(mystock);
    }
}

Here's working plunk for that https://plnkr.co/edit/r2WLGakBU9kGmWLWBN7K?p=preview

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

Comments

0

ng-model directive is used for a 2-way binding, so instead of <td ng-model="portfolio.stockfromdata"></td>

try using <td ng-bind="portfolio.stockfromdata"></td> or <td>{{portfolio.stockfromdata}}</td>

and if want to make it editable, then you can use your ng-model with input like <td ><input ng-model="portfolio.stockfromdata"/></td>

here ng-model will binfd your data to the input model, and change in UI will also reflect to JSON binded data in your controller, thats the purpose of ng-model

7 Comments

But how do i add the data to table from the other section? I should be giving some connection between the first and 2nd section
just push it to the array, and angular binding will take care for the rendering of the table in DOM, try it with my code :)
well, whatever you are pusing you need to initialize that data. if you want to take it from some textbox in UI, then create two <input> element outside the table, and give two ng-model="stock", ng-model="price" and then push $scope.price` and $scope.stock as a object in the array. first read about how to use controller and scope with ng-model in html template in angular docs, it will take 15minutes hadrly
I don't need a input boxes here. I am not sure if you have understand the issue completely. I have updated the question for a better understanding. Check it. Thank you.
1> push an object instead of an array 2> push it like $scope.portfolios.push({stock: stockValue, price: priceValue}) 3> don't use ng-model in your <td> Now, from where stockValue, priveValue you will get thats your headache (I was just suggesting from input box, incase you want to take it from user)
|

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.