lundi 4 juillet 2016

Retrieving selected checkbox value after page reload

I am trying use cookies to hide and show columns in a table. The problem that I am having is that I am not able to display the previously selected checkbox value(s). I am trying to use ng-init to get back the value and set the checkbox to true if has been selected.

Modal(Attempt 1)

<div class="modal-header">
    <h3 class="modal-title">Additional Collections Columns</h3>
</div>
<div class="modal-body">
    <h4>Select addional columns from the list below to display in 'Collections' list</h4><br/>

    <div class="row">
    <form name="collectionsColumnsForm">
        <div class="col-sm-6">
            <ul>
                ...
                <li>
                    <label class="control c control--checkbox">
                        <input type="checkbox" name="phone" ng-model="columns.coll_phone" value="1" ng-init="columns={coll_phone:''}">
                        Phone
                        <div class="control__indicator"></div>
                    </label>
                </li>
                ...
            </ul>
        </div>
        </form>
    </div>
    <br>
</div>

<div class="modal-footer">
    <button class="btn btn-danger" type="button" ng-click="cancel()">Cancel</button>
    <button class="btn btn-info" type="button" ng-click="save()">Save</button>
</div>


Modal(Attempt 2)

<div class="modal-header">
    <h3 class="modal-title">Additional Collections Columns</h3>
</div>
<div class="modal-body">
    <h4>Select addional columns from the list below to display in 'Collections' list</h4><br/>

    <div class="row">
    <form name="collectionsColumnsForm">
        <div class="col-sm-6">
            <ul>
                ...
                <li>
                    <label class="control c control--checkbox">
                        <input type="checkbox" name="phone" ng-model="columns.coll_phone" value="1" ng-init="columns={columns.coll_phone:''}">
                        Phone
                        <div class="control__indicator"></div>
                    </label>
                </li>
                ...
            </ul>
        </div>
        </form>
    </div>
    <br>
</div>

<div class="modal-footer">
    <button class="btn btn-danger" type="button" ng-click="cancel()">Cancel</button>
    <button class="btn btn-info" type="button" ng-click="save()">Save</button>
</div>


Modal(Attempt 3)

<div class="modal-header">
    <h3 class="modal-title">Additional Collections Columns</h3>
</div>
<div class="modal-body">
    <h4>Select addional columns from the list below to display in 'Collections' list</h4><br/>

    <div class="row">
    <form name="collectionsColumnsForm">
        <div class="col-sm-6">
            <ul>
                ...
                <li>
                    <label class="control c control--checkbox">
                        <input type="checkbox" name="phone" ng-model="columns.coll_phone" value="1" ng-init="columns=cookie_coll_phone">
                        Phone
                        <div class="control__indicator"></div>
                    </label>
                </li>
                ...
            </ul>
        </div>
        </form>
    </div>
    <br>
</div>

<div class="modal-footer">
    <button class="btn btn-danger" type="button" ng-click="cancel()">Cancel</button>
    <button class="btn btn-info" type="button" ng-click="save()">Save</button>
</div>

In the code above, the user has the option of selecting any of the 3 options available to them. Once the user selects one or more checkboxes that value is then passed to the angularjs controllers where it is then set in a cookie. The value that I get back from the cookie is true whenever I checked in the browser tools or on the view itself.


Angularjs Controller

...

// stored cookie columns for collections table
...
$scope.cookie_coll_phone = $cookies.get('coll_phone');
...

// add more columns to collections list
$scope.collectionColumns = function() {

    var modalInstance = $uibModal.open({
       animation: true,
       templateUrl: '/js/modals/collection-columns.html',
        size: 'md',
       resolve:{
       },
      controller: function ($scope,$uibModalInstance) {

        ...
        $scope.cookie_coll_phone = $cookies.get('coll_phone');
        ...


        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };

        $scope.save = function() {

            ...
            $cookies.put('coll_phone', $scope.columns.coll_phone);
            ...

            toaster.pop('success', 'Success!', 'Additional column(s) have been added.');
            location.assign('/collections');
            $uibModalInstance.close();
        };

      }
   });

    modalInstance.result.then(function (data) {

    },function () {

    });
};
...

Any help with this problem would be greatly appreciated




Aucun commentaire:

Enregistrer un commentaire