Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

First off, starting out with this so a bit confused. I have a simple table, where the contents are pulled in from a database and injected into the DOM with Angular.

The table consists of an option, and its value. I would like the user to be able to edit the values, and then click a "save" button, where I make a http call to my back-end with the details.

I've got the basics working, clicking a button and the input fields replace the table cell content:

enter image description here

Clicking "Edit":

enter image description here

When clicking "Cancel", it reverts back - so this is all working.

So this bit I can't work out, is when I press update, I want to create an array (json?), where I can send somewhere using http.

I'd need something where each object in the array/json contains the "option" and the "value", so I can match these in the database.

My HTML:

<div ng-hide="loading" class="table-responsive">    
    <table class="table table-striped table-compact table-bordered">
        <thead>
            <tr>
                <th>Option</th>
                <th>Value</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="setting in settings">
                    <td><< setting.option >> <i class="fa fa-question pull-right pointer" tooltip="<< setting.description >>" ></i></td>
                    <td ng-switch="status">
                                <input ng-switch-when="editable" type="text" class="form-control" value="<< setting.value >>" />
                                <span ng-switch-when="uneditable"><< setting.value >></span>
                    </td>
            </tr>
        </tbody>
    </table>
</div>


<div ng-hide="loading" ng-switch="status">
        <button ng-switch-when="uneditable" ng-click="edit()" class="btn btn-sm btn-info">Edit</button>
        <button ng-switch-when="editable" ng-click="save()" class="btn btn-sm btn-success">Update</button>
        <button ng-switch-when="editable" ng-click="cancel()" class="btn btn-sm btn-danger">Cancel</button>
</div>

And finally my ng controller:

app.controller('appSettingsController', function($scope, ApplicationSettings) {

    $scope.settings = {};
    $scope.loading = true;
    $scope.status = 'uneditable';

    // Grab data for table
    ApplicationSettings.get()
        .success(function(data) {
            $scope.settings = data;
            $scope.loading = false;
        });

    $scope.edit = function() {
        $scope.status = 'editable';
        $scope.updates = {};
    };

    $scope.cancel = function() {
        $scope.status = 'uneditable';
    };

    $scope.save = function() {

        // Construct Array/JSON of inputs

    };

});

Anyone got any ideas? I have a feeling it's something to do with using ng-model?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Inside your table, the second column has the following input element tag when in edit mode:

<input ng-switch-when="editable" type="text" class="form-control"
 value="<< setting.value >>" />

Firstly - I think the value attribute should be {{setting.value}} and not << setting.value >> - I can't imagine the latter giving the value in AngularJS.

Now, for your requirements. Instead of using the value attribute, you can use the ng-model attribute as you guessed.

With the ng-model attribute in place, the input should now be:

<input ng-switch-when="editable" type="text" class="form-control"
 ng-model="setting.value" />

ng-model will take care of displaying the value for that input as well as due to two way data binding, the value entered into the input will be stored back into setting.value.

What this means is that, AngularJS automatically will update $scope.settings when you input something in the text box. You don't have to write any additional code to ensure that the value is put back. It's like the Update button was already clicked and data was saved - only that it wasn't clicked but data was still saved

The only downside to this is that when you click on cancel, the old values are no longer available (since the moment you type something into the text, the values are updated). You can store initial values of $scope.settings into another variable before switching to edit mode. That way, when you click cancel, you are still left with old values.

share|improve this answer
    
Ah awesome cheers. The << >> is because I'm using a framework which uses {{ }} - forgot to mention sorry. Will look into storing it in a variable, does make sense now though! –  Alias Feb 16 at 15:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.