简体   繁体   中英

Angularjs: update filter after user input

I am having an issue updating a value after user input using angularjs. I am giving the user a grid to enter numbers into, and after each keypress, I would like to sum that column and display it in the column header. The filter works for the initial value of table, but wont update when new number are input into the table. Is there a way to refresh the element?

<th>total<small>{{Rows|sumByKey:'ColOne'}}</small></th> ....

<tr ng-repeat="Row in Rows">
    <td><input type="text" value="{{Row.ColOne}}"/></td>
</tr>

Angularjs Filter:

app.filter('sumByKey', function () {
return function (data, key) {
    if (typeof (data) === 'undefined' || typeof (key) === 'undefined') {
        return 0;
    }

    var sum = 0;
    for (var i = data.length - 1; i >= 0; i--) {
        sum += parseInt(data[i][key]);
    }
    //filter.$stateful = true;
    return sum;
    };
});

I belive you need to bind you value to make angular aware of the changes. Use ng-model on the inputs to set the value. This way Angular is in control of all values.

<th>total<small>{{Rows|sumByKey:'ColOne'}}</small></th> ....

<tr ng-repeat="Row in Rows">
    <td><input type="text" ng-model="Row.ColOne"/></td>
</tr>

Here is a plunker for it running .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM