angularjs - 如何选择和删除多行sqlite

时间:2017-02-13 15:37:47

标签: angularjs sqlite cordova ionic-framework

我有这个应用程序,我拍照并将图像URL保存到sqlite。 我可以添加,删除和显示所有这些,现在我正在尝试使用ID从数据库中选择多个(图像)并删除所选的

$scope.selected = [];
    $scope.deleteMultiple = function(){
        var selectedImages = [];
    angular.forEach($scope.selected, function(isSelected, index) {
            if(isSelected)
            selectedImages.push($scope.images[$index])
            $scope.delete(selectedImages.id)
        })
    console.log(selectedImage)
        }


 $scope.add = function(path) {             
            if (imagesP != null) {          
                $cordovaSQLite.execute(db, "INSERT INTO imageTable (images) VALUES(?)", [path] );        
            }        
            alert("Inserted.");      
        },
        function(e) {        
            alert(e);      
        };

    $scope.delete = function(id) {                
            if (id != '') {          
                $cordovaSQLite.execute(db, "DELETE FROM imageTable WHERE id=?", [id]);        
            }        
            alert("Deleted.");        
            $scope.ShowAllData();      
        },
        function(e) {        
            alert(e);      
        };    

      
 $scope.getImgIDbyName = function(name) {

    console.log('[getImgIDbyName] - get image with name:  ' + name );
    var sql = "SELECT * FROM imageTable WHERE image = '" + name + "';";
    console.log(sql);
    $cordovaSQLite.execute(db,
        sql
    ).then(
        function(res) {
            if (res.rows.length > 0) {
                if (res.rows.length > 1) {
                    console.log('[getImgIDbyName] - OOPS more than 1 image returned!!!!  ' + name );
                } else {
                    $scope.delete (res.rows.item(0).id);
                }
            } else {
                console.log('[getImgIDbyName] - no image found with name:  ' + name );
            }
        },
        function(error) {
            alert("error occured: " + error.message);
        }
    );



}

HTML

<div ng-repeat="image in images " > <!-- container -->

 <div>

    <img ng-src="{{image.image}}" style="width: 70%; height: 80%;"  ng-class="{'selected-image': selected[$index]}" ng-click="selected[$index] = !selected[$index] />

 <div  ng-click="getImgIDbyName(image.image)">
    <div style="font-size:2rem;margin:0.5rem 0 1rem" > DELETE IMAGE (ID:{{image.id}}) </div>
 </div>
     </div>


</div>      

    </ion-scroll>

1 个答案:

答案 0 :(得分:0)

以下代码应该按照您的预期进行多次删除:

$scope.selected = [];
    $scope.deleteMultiple = function(){
        var selectedImages = [];
        var selectIDs = $scope.selected.map(function(x) {return x +',';};
        console.log(selectedImages.selectIDs)
        $scope.delete(selectedImages.selectIDs)
        }

$scope.delete = function(ids) {                
            if (ids != '') {          
                $cordovaSQLite.execute(db, "DELETE FROM imageTable WHERE id IN (?)", [ids]);        
            }        
            alert("Deleted.");        
            $scope.ShowAllData();      
        },

这篇文章描述了sqllite的“IN”子句。 https://www.techonthenet.com/sqlite/in.php