0

I'm trying to hide a column by evaluating $scope.visibility value to a custom directive as attribute, but it's not working.

<td data-column-visible="visibility" data-table-property='id' data-table-property-type='button'>
    <button class="btn btn-xs btn-default" data-toggle="modal"
        ng-click="modify(user)">
        <i class="icon-pencil bigger-110"></i>
    </button>
    <button class="btn btn-xs btn-danger" data-toggle="modal"
        ng-click="delete(user)">
        <i class="icon-trash bigger-110"></i>
    </button>                   
</td>

My directive :

'use strict';

define([ 'appModule' ], function(app) {
    // see example
    // http://stackoverflow.com/questions/14242455/using-jquery-datatable-with-angularjs
    app.directive('appDatatable', [ '$rootScope', '$compile', function($rootScope, $compile) {
        var _buildAoColumnDefs = function(scope, tableElement, tableData) {
                ....
                var defs = {
                    "mData": _tdElement.data("tableProperty"), "aTargets":[index], "bVisible": _tdElement.data("columnVisible")
                };
                ....
            return result;
        };
            return {
                scope : {
                    ...
                    columnVisible: "="
                },
                link : function(scope, element, attrs) {

                    // Watch changing aaData.
                    var dataTable = null;
                    scope.$watch('aaData', function(value) {
                        ...
                        var aoColumnDefs = scope.aoColumnDefs || _buildAoColumnDefs(scope, element, value);
                        ... 
                    }, true);
                }
            };
        }]);
    });

It always evaluates to "visibility" instead of true/false.

1 Answer 1

0

The data-column-visible="visibility" the visibility which you pass from your controller should be a primitive value... that's why its not changing as it create a new scope.

In your controller it should be $scope.visibility = true or false. Change that to an object.

$scope.someObject = {}
$scope.someObject.visibility = true or false

Its always advice to pass a object, not primitives.

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

9 Comments

I replaced visibilityby someObject.visibility in my $scope, and i still do have the same problem. When i console.log(_tdElement.data("columnVisible")) i'm still getting someObject.visibility
inside your link function create scope.columnVisible = columnVisible and now pass that to your _tdElement.data
I get a ReferenceError: columnVisible is not defined and you want to me to pass scope.columnVisible like _tdElement.data(scope.columnVisible) ?
You need to pass the scope variable to var aoColumnDefs = scope.aoColumnDefs || _buildAoColumnDefs(scope, element, value); here...
since you are already passing the scope there... collect it like this.. _tdElement.data(scope.columnVisible)
|

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.