Skip to content

Instantly share code, notes, and snippets.

@japharr
Last active November 28, 2016 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save japharr/1640640135621b984d962d3a87156ee9 to your computer and use it in GitHub Desktop.
Save japharr/1640640135621b984d962d3a87156ee9 to your computer and use it in GitHub Desktop.
How to use checkbox on jhipster table and delete selected rows.
(function() {
'use strict';
angular
.module('schoolApp')
.controller('BookSelDeleteController',BookSelDeleteController);
BookDeleteController.$inject = ['$uibModalInstance', '$stateParams', 'Book'];
function BookSelDeleteController($uibModalInstance, $stateParams, Book) {
var vm = this;
vm.selectedItems = $stateParams.selectedItems;
vm.clear = clear;
vm.confirmDelete = confirmDelete;
function clear () {
$uibModalInstance.dismiss('cancel');
}
function confirmDelete (id) {
Book.deleteSel({}, vm.selectedItems,
function () {
$uibModalInstance.close(true);
},
function () {
console.log("An error occurred");
});
}
}
})();
(function() {
'use strict';
angular
.module('schoolApp')
.factory('Book', Book);
Book.$inject = ['$resource', 'DateUtils'];
function Book ($resource, DateUtils) {
var resourceUrl = 'api/books/:id';
return $resource(resourceUrl, {}, {
'query': { method: 'GET', isArray: true},
'deleteSel': { method: 'POST', isArray: true, params:{'delete-sel':''}},
...
});
}
})();
...
.state('book.delete-sel', {
parent: 'book',
url: '/delete-sel',
data: {
authorities: ['ROLE_USER']
},
params: {
selectedItems: null
},
onEnter: ['$stateParams', '$state', '$uibModal', function($stateParams, $state, $uibModal) {
$uibModal.open({
templateUrl: 'app/entity/book/book-delete-sel-dialog.html',
controller: 'BookSelDeleteController',
controllerAs: 'vm',
size: 'md'
}).result.then(function() {
$state.go('books', null, { reload: true });
}, function() {
$state.go('^');
});
}]
});
...
@Inject
BookRepository bookRepository;
/**
* DELETE /books/:id : delete the "id" book.
*
* @param books the id of the book to delete
* @return the ResponseEntity with status 200 (OK)
*/
//@DeleteMapping("/books")
@RequestMapping(value = "/books",
params = "delete-sel",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<Void> deleteBooks(@RequestBody List<Book> books) {
log.debug("REST request to delete Books : {}", books.size());
bookRepository.delete(books);
return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("book", null)).build();
}
...
vm.checkAll = [];
var map = {};
vm.checkboxes = [];
vm.selectedItems = [];
vm.selectAll = selectAll;
vm.select = select;
function selectAll () {
var value = vm.checkAll[vm.page];
angular.forEach(vm.books, function(item) {
if (angular.isDefined(item.id)) {
if(vm.checkboxes[item.id] != value) {
vm.checkboxes[item.id] = value;
vm.select(item);
}
}
});
};
function select (item) {
var value = vm.checkboxes[item.id];
if(value) {
vm.selectedItems.push(item);
if(map[vm.page] == null) map[vm.page] = 1;
else map[vm.page] = map[vm.page] + 1;
if(map[vm.page] == vm.books.length) {
vm.checkAll[vm.page] = true;
}
} else {
vm.selectedItems.splice(item, 1);
if(map[vm.page] == null) map[vm.page] = 0;
else map[vm.page] = map[vm.page] - 1;
if(map[vm.page] < vm.books.length) {
vm.checkAll[vm.page] = false;
}
}
};
<button ng-if="vm.selectedItems.length > 0" type="submit"
tooltip-placement="top" uib-tooltip="{{'entity.action.delete' | translate}}"
ui-sref="book.delete-sel({selectedItems: vm.selectedItems})"
class="btn btn-success">
<i class="fa fa-trash-o"></i>
</button>
<table>
<thead>
<tr jh-sort="vm.predicate" ascending="vm.reverse" callback="vm.transition()">
<th><input type="checkbox" icheck ng-change="vm.selectAll()" ng-model="vm.checkAll[vm.page]"></th>
<th jh-sort-by="id"><span data-translate="global.field.id">ID</span></th>
---
</tr>
</thead>
<tbody>
<tr ng-repeat="book in vm.books track by book.id">
<td><input type="checkbox" icheck ng-model="vm.checkboxes[book.id]" ng-change="vm.select(book)"/></td>
<td>{{($index + 1) + (vm.page - 1) * vm.itemsPerPage}}</td>
...
</tr>
</tbody>
</table>
@japharr
Copy link
Author

japharr commented Nov 25, 2016

If you are using Google chrome, screenshot the developer console and drop it here @Atul21

@japharr
Copy link
Author

japharr commented Nov 25, 2016

I meant in the "books.delete-sel" state @Atul21

@Atul21
Copy link

Atul21 commented Nov 25, 2016

error

@japharr
Copy link
Author

japharr commented Nov 25, 2016

screenshot_7

@japharr
Copy link
Author

japharr commented Nov 25, 2016

screenshot_8

@Atul21
Copy link

Atul21 commented Nov 25, 2016

@japharr I changed schoolApp to penapp but I didn't know where will I get pen.delete-sel

@japharr
Copy link
Author

japharr commented Nov 25, 2016

check your pen.state.js @Atul21

@Atul21
Copy link

Atul21 commented Nov 25, 2016

untitled

@Atul21
Copy link

Atul21 commented Nov 25, 2016

@japharr my app name is pen and entity name is book

@japharr
Copy link
Author

japharr commented Nov 25, 2016

@Atul21 do the screenshot of the book.state.js and drop it here

@japharr
Copy link
Author

japharr commented Nov 25, 2016

or just copy and paste it

@Atul21
Copy link

Atul21 commented Nov 25, 2016

`(function() {
'use strict';

angular
    .module('penApp')
    .config(stateConfig);

stateConfig.$inject = ['$stateProvider'];

function stateConfig($stateProvider) {
    $stateProvider
    .state('book', {
        parent: 'entity',
        url: '/book',
        data: {
            authorities: ['ROLE_USER'],
            pageTitle: 'Books'
        },
        views: {
            'content@': {
                templateUrl: 'app/entities/book/books.html',
                controller: 'BookController',
                controllerAs: 'vm'
            }
        },
        resolve: {
        }
    })
    .state('book-detail', {
        parent: 'entity',
        url: '/book/{id}',
        data: {
            authorities: ['ROLE_USER'],
            pageTitle: 'Book'
        },
        views: {
            'content@': {
                templateUrl: 'app/entities/book/book-detail.html',
                controller: 'BookDetailController',
                controllerAs: 'vm'
            }
        },
        resolve: {
            entity: ['$stateParams', 'Book', function($stateParams, Book) {
                return Book.get({id : $stateParams.id}).$promise;
            }],
            previousState: ["$state", function ($state) {
                var currentStateData = {
                    name: $state.current.name || 'book',
                    params: $state.params,
                    url: $state.href($state.current.name, $state.params)
                };
                return currentStateData;
            }]
        }
    })
    .state('book-detail.edit', {
        parent: 'book-detail',
        url: '/detail/edit',
        data: {
            authorities: ['ROLE_USER']
        },
        onEnter: ['$stateParams', '$state', '$uibModal', function($stateParams, $state, $uibModal) {
            $uibModal.open({
                templateUrl: 'app/entities/book/book-dialog.html',
                controller: 'BookDialogController',
                controllerAs: 'vm',
                backdrop: 'static',
                size: 'lg',
                resolve: {
                    entity: ['Book', function(Book) {
                        return Book.get({id : $stateParams.id}).$promise;
                    }]
                }
            }).result.then(function() {
                $state.go('^', {}, { reload: false });
            }, function() {
                $state.go('^');
            });
        }]
    })
    .state('book.new', {
        parent: 'book',
        url: '/new',
        data: {
            authorities: ['ROLE_USER']
        },
        onEnter: ['$stateParams', '$state', '$uibModal', function($stateParams, $state, $uibModal) {
            $uibModal.open({
                templateUrl: 'app/entities/book/book-dialog.html',
                controller: 'BookDialogController',
                controllerAs: 'vm',
                backdrop: 'static',
                size: 'lg',
                resolve: {
                    entity: function () {
                        return {
                            bookName: null,
                            remarks: null,
                            id: null
                        };
                    }
                }
            }).result.then(function() {
                $state.go('book', null, { reload: 'book' });
            }, function() {
                $state.go('book');
            });
        }]
    })
    .state('book.edit', {
        parent: 'book',
        url: '/{id}/edit',
        data: {
            authorities: ['ROLE_USER']
        },
        onEnter: ['$stateParams', '$state', '$uibModal', function($stateParams, $state, $uibModal) {
            $uibModal.open({
                templateUrl: 'app/entities/book/book-dialog.html',
                controller: 'BookDialogController',
                controllerAs: 'vm',
                backdrop: 'static',
                size: 'lg',
                resolve: {
                    entity: ['Book', function(Book) {
                        return Book.get({id : $stateParams.id}).$promise;
                    }]
                }
            }).result.then(function() {
                $state.go('book', null, { reload: 'book' });
            }, function() {
                $state.go('^');
            });
        }]
    })
    .state('book.delete', {
        parent: 'book',
        url: '/{id}/delete',
        data: {
            authorities: ['ROLE_USER']
        },
        onEnter: ['$stateParams', '$state', '$uibModal', function($stateParams, $state, $uibModal) {
            $uibModal.open({
                templateUrl: 'app/entities/book/book-delete-dialog.html',
                controller: 'BookDeleteController',
                controllerAs: 'vm',
                size: 'md',
                resolve: {
                    entity: ['Book', function(Book) {
                        return Book.get({id : $stateParams.id}).$promise;
                    }]
                }
            }).result.then(function() {
                $state.go('book', null, { reload: 'book' });
            }, function() {
                $state.go('^');
            });
        }]
    })
    
    .state('books.delete-sel', {
        parent: 'books',
        url: '/delete',
        data: {
            authorities: ['ROLE_USER']
        },
        params: {
            selectedItems: null
        },
        onEnter: ['$stateParams', '$state', '$uibModal', function($stateParams, $state, $uibModal) {
            $uibModal.open({
                templateUrl: 'app/entities/book/book-delete-sel-dialog.html',
                /*controller: 'BookDeleteController',*/
                controller: 'BookDeleteSelController',
                controllerAs: 'vm',
                size: 'md'
            }).result.then(function() {
                    $state.go('books', null, { reload: true });
                }, function() {
                    $state.go('^');
                });
        }]
    });
    
}

})();
`

@japharr
Copy link
Author

japharr commented Nov 25, 2016

screenshot_9

@Atul21
Copy link

Atul21 commented Nov 25, 2016

not working @japharr

@japharr
Copy link
Author

japharr commented Nov 25, 2016

@Atul21 what is the error in the developer console?

@Atul21
Copy link

Atul21 commented Nov 25, 2016

untitled1

@japharr
Copy link
Author

japharr commented Nov 25, 2016

@Atul21

The error should be coming from the book-delete-sel-dialog.controller.js change the "schoolApp" to "penApp"

@Atul21
Copy link

Atul21 commented Nov 25, 2016

book-delete-sel-dialog.controller.js

`(function() {
'use strict';

angular
    .module('penApp')
    .controller('BookDeleteController',BookDeleteController);
     /*.controller('BookDeleteSelController',BookDeleteSelController);*/
BookDeleteController.$inject = ['$uibModalInstance', '$stateParams', 'Book'];

function BookDeleteController($uibModalInstance, $stateParams, Book) {
/*function BookDeleteSeController($uibModalInstance, $stateParams, Book) {*/
    var vm = this;

    vm.selectedItems = $stateParams.selectedItems;
    vm.clear = clear;
    vm.confirmDelete = confirmDelete;

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

    function confirmDelete (id) {
        Book.deleteSel({}, vm.selectedItems,
            function () {
                $uibModalInstance.close(true);
            },
            function () {
                console.log("An error occurred");
            });
    }
}

})();`

@Atul21
Copy link

Atul21 commented Nov 25, 2016

@japharr did you get my mail? in which I sent you each and every file of book entity

@japharr
Copy link
Author

japharr commented Nov 25, 2016

Check the book.state.js
change the "books.delete-sel" to "book.delete-sel"

Check the delete all button in the html and confirm the ui-sref to be book.delete-sel


@japharr
Copy link
Author

japharr commented Nov 25, 2016

No

@Atul21
Copy link

Atul21 commented Nov 25, 2016

@japharr it is giving same error in developer console

@Atul21
Copy link

Atul21 commented Nov 25, 2016

untitled2

@Atul21
Copy link

Atul21 commented Nov 25, 2016

@japharr Can I send you all files in book entities?

@japharr
Copy link
Author

japharr commented Nov 25, 2016

@Atul21
yes, you can

@Atul21
Copy link

Atul21 commented Nov 25, 2016

@japharr I sent you

@Atul21
Copy link

Atul21 commented Nov 25, 2016

@japharr please check your mail.

@japharr
Copy link
Author

japharr commented Nov 27, 2016

@Atul21
no mail from you

@Atul21
Copy link

Atul21 commented Nov 28, 2016

@japharr please check your spam folder.

@japharr
Copy link
Author

japharr commented Nov 28, 2016

@Atul21

Check your mail for update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment