Is there a better way to toggle in and out of edit mode in ng-grid?

Viewed 2614

So far I've only been able to change the edit mode in ng-grid by using separate templates and displaying the correct template based on some user input.

Example: Plunker (re-size a column and then switch to another mode)

This is an issue because any column re-sizing, sorting, or grouping that is preformed is lost when switching from template to template.

Is there a better approach?

Workaround

If anyone is interested, I created a workaround for myself by modify ng-grid.js 2.0.7

First I modified the cellEditTemplate.html and added the attribute disable-Cell=\"!isEditing\" (isEditing is a scope variable from the controller which would be required with this workaround)

    $templateCache.put("cellEditTemplate.html",
      "<div ng-cell-has-focus disable-Cell=\"!isEditing\" ng-dblclick=\"editCell()\">\n" +
      "\t<div ng-edit-cell-if=\"!isFocused\">\t\n" +
      "\t\tDISPLAY_CELL_TEMPLATE\n" +
      "\t</div>\n" +
      "\t<div ng-edit-cell-if=\"isFocused\">\n" +
      "\t\tEDITABLE_CELL_TEMPLATE\n" +
      "\t</div>\n" +
      "</div>"
    );  

Then I modified the ngCellHasFocus directive and added a $watch on the disableCell attribute. When disableCell is set to true I prevent the click and mousedown event from firing in that directive.

            return function ($scope, elm, attrs) {
                var disabled = false;
                //Added $watch
                $scope.$watch(attrs.disableCell, function (newVal) {
                    disabled = newVal;
                });
                var isFocused = false;
                var isCellEditableOnMouseDown = false;

                $scope.editCell = function () {
                    if (!$scope.enableCellEditOnFocus) {
                        setTimeout(function () {
                            focusOnInputElement($scope, elm);
                        }, 0);
                    }
                };
                elm.bind('mousedown', function (evt) {
                    //Added return
                    if (disabled) return;
                    if ($scope.enableCellEditOnFocus) {
                        isCellEditableOnMouseDown = true;
                    } else {
                        elm.focus();
                    }
                    return true;
                });
                elm.bind('click', function (evt) {
                    //Added return
                    if (disabled) return;
                    if ($scope.enableCellEditOnFocus) {
                        evt.preventDefault();
                        isCellEditableOnMouseDown = false;
                        focusOnInputElement($scope, elm);
                    }
                });
                elm.bind('focus', function (evt) {
                    isFocused = true;
                    if ($scope.enableCellEditOnFocus && !isCellEditableOnMouseDown) {
                        focusOnInputElement($scope, elm);
                    }
                    return true;
                });
                elm.bind('blur', function () {
                    isFocused = false;
                    return true;
                });
                elm.bind('keydown', function (evt) {
                    if (!$scope.enableCellEditOnFocus) {
                        if (isFocused && evt.keyCode !== 37 && evt.keyCode !== 38 && evt.keyCode !== 39 && evt.keyCode !== 40 &&    evt.keyCode !== 9 && !evt.shiftKey && evt.keyCode !== 13) {
                            focusOnInputElement($scope, elm);
                        }
                        if (isFocused && evt.shiftKey && (evt.keyCode >= 65 && evt.keyCode <= 90)) {
                            focusOnInputElement($scope, elm);
                        }
                        if (evt.keyCode === 27) {
                            elm.focus();
                        }
                    }
                    return true;
                });
            };
        }]);

And here are my grid options

        $scope.gridOptions = {
            data: 'myData',
            enableColumnResize: true,
            enableCellSelection: true,
            enableRowSelection: true,
            enableCellEditOnFocus: true,
            columnDefs: 'columnDefs',
            plugins: [layoutPlugin],
            //My row template has a ng-dblClick() attached to it for navigation in non-edit mode
            rowTemplate: 'Templates/gridRowView.html'
        };
2 Answers
Related