javascript - How to delete multiple rows with button click with angularjs and datatables? -
i using angularjs plus datatables . trying delete row or multiple rows of checkboxes. whenever select checkbox , need status of checkbox id i.e in selected array . comes {"1":true,"2":false} true means id selected . stuck code.
(function(angular) { 'use strict'; var app=angular.module('vehicle',['datatables','datatables.select','datatables.tabletools','ngresource','ui','ui.sortable','datatables.buttons']); app.controller('datactrl',vehicledata); function vehicledata($compile,$scope,$resource,dtoptionsbuilder,dtcolumnbuilder){ $scope.selected = {}; $scope.selecteditems=""; $scope.selectall = false; $scope.toggleall = toggleall; $scope.toggleone = toggleone; var titlehtml = '<input ng-model="selectall" ng-click="toggleall(selectall,selected)" type="checkbox">'; $scope.sortableoptions = { 'ui-floating': true }; $scope.dtoptions = dtoptionsbuilder.fromfnpromise(function() { return $resource('data.json').query().$promise; }) .withoption('createdrow', function(row, data, dataindex) { $compile(angular.element(row).contents())($scope); }) .withoption('headercallback', function(header) { if (!$scope.headercompiled) { $scope.headercompiled = true; $compile(angular.element(header).contents())($scope); } }) .withtabletools('<?php echo $base_url;?>assets/swf/copy_csv_xls_pdf.swf') .withtabletoolsbuttons([ 'print', { 'sextends': 'collection', 'sbuttontext': 'save', 'abuttons': ['csv', 'xls', 'pdf'] } ]) .withpaginationtype('full_numbers') .withbuttons([ { text: 'deactivate', key: '1', action: function (e, dt, node, config) { //alert(json.stringify($scope.selected)); for(var id in $scope.selected){ alert($scope.selected.hasownproperty(id)); } } }, { text: 'activate', key: '2', action: function (e, dt, node, config) { for(var id in $scope.selected){ alert($scope.selected.hasownproperty(id)); } } }, { text: 'delete', key: '3', action: function (e, dt, node, config) { for(var id in $scope.selected){ alert($scope.selected.hasownproperty(id)); } } } ]); $scope.dtcolumns = [ //dtcolumnbuilder.newcolumn(null).withtitle('') //.notsortable() // need define mrender function, otherwise [object object] //.renderwith(checkboxhtml), dtcolumnbuilder.newcolumn(null).withtitle(titlehtml) .notsortable() .renderwith(function(data,type, full, meta) { $scope.selected[full.vehicle_id] = false; return '<input ng-model="selected[' + data.vehicle_id + ']" ng-click="toggleone(selected)" type="checkbox">'; }), dtcolumnbuilder.newcolumn('vehicle_id').withtitle('id').notvisible(), dtcolumnbuilder.newcolumn('vehicle_name').withtitle('vehicle name'), dtcolumnbuilder.newcolumn(null).withtitle('vehicle image') .notsortable() .renderwith(imagehtml), dtcolumnbuilder.newcolumn('vehicle_passanger').withtitle('max passengers'), dtcolumnbuilder.newcolumn('vehicle_luggage').withtitle('max luggage'), dtcolumnbuilder.newcolumn('agency_name').withtitle('company'), dtcolumnbuilder.newcolumn(null).withtitle('status').renderwith(showstatus) ]; } function imagehtml(data,type, full, meta) { return '<img src='+data.vehicle_image+' height=50 width=50>'; } function checkboxhtml(data,type, full, meta) { return '<input type="checkbox" value='+data.vehicle_id+' class="vehical_id" name="vehical_id[]">'; } function showstatus(data,type, full, meta){ if(data.vehicle_status==1){ return "active"; }else{ return "inactive"; } } function toggleall (selectall,selecteditems) { (var id in selecteditems) { if (selecteditems.hasownproperty(id)) { selecteditems[id] = selectall; } } } function toggleone (selecteditems) { (var id in selecteditems) { if (selecteditems.hasownproperty(id)) { if(!selecteditems[id]) { $scope.selectall = false; return; } } } $scope.selectall = true; } })(angular);
the data.json
[{"vehicle_id":"1","vehicle_name":"sedan","vehicle_company_id":"1","vehicle_passanger":"5","vehicle_luggage":"5","vehicle_image":"photo.jpg","vehicle_status":"1","vehicle_sorting_ord":"1","created":"2017-04-05 15:01:31","agency_id":"1","agency_name":"webtechstairs","agency_adress":"mohali","agency_phonenumer":"0172456789","agency_email":"help.webtechstairs@gmail.com","agency_firstname":"amit","agency_last_name":"kumar","agency_status":"1","agency_created":"2016-09-22 11:58:36","agency_company_logo":"","agency_website":"www.webtechstairs.com","agency_packageid":"1","agency_tan_number":"blqpk2240e"},{"vehicle_id":"2","vehicle_name":"suv","vehicle_company_id":"1","vehicle_passanger":"7","vehicle_luggage":"7","vehicle_image":"jellyfish.jpg","vehicle_status":"0","vehicle_sorting_ord":"2","created":"2017-04-04 16:31:02","agency_id":"1","agency_name":"webtechstairs","agency_adress":"mohali","agency_phonenumer":"0172456789","agency_email":"help.webtechstairs@gmail.com","agency_firstname":"amit","agency_last_name":"kumar","agency_status":"1","agency_created":"2016-09-22 11:58:36","agency_company_logo":"","agency_website":"www.webtechstairs.com","agency_packageid":"1","agency_tan_number":"blqpk2240e"}]
Comments
Post a Comment