根据表的行(索引)删除localStorage键

时间:2017-06-14 18:40:23

标签: javascript jquery angularjs html5 local-storage

这是我的ctrl:

app.controller('ctrl', function ($window, $scope) {

    $scope.initData = [
        {
            firstName: "John",
            lastName: "Doe",  
        },
        {
            firstName: "Jane",
            lastName: "Doe",
        },
        {
            firstName: "John",
            lastName: "Smith",
        }
    ];

    $window.localStorage.setItem('initData', JSON.stringify($scope.initData));
    $scope.retrievedData = JSON.parse($window.localStorage.getItem('initData'));
    console.log($scope.retrievedData);
    $scope.sortedType = 'firstName';
    $scope.sortedReverse = false;


    $scope.removeRow = function (row) {
        $scope.retrievedData.splice(row, 1);
        $window.localStorage.removeItem('initData');
    }
});

HTML:

<table class="table table-bordered table-striped table-hover">
                <thead>
                <tr>
                    <th>
                        <span ng-show="sortedType == 'firstName' && sortedReverse" class="fa fa-long-arrow-up"></span>
                        <span ng-show="sortedType == 'firstName' && !sortedReverse" class="fa fa-long-arrow-down"></span>
                        <span href="#" ng-click="sortedType = 'firstName'; sortedReverse = !sortedReverse" style="cursor:pointer;">First Name</span>
                    </th>
                    <th>
                        <span ng-show="sortedType == 'lastName' && sortedReverse" class="fa fa-long-arrow-up"></span>
                        <span ng-show="sortedType == 'lastName' && !sortedReverse" class="fa fa-long-arrow-down"></span>
                        <span href="#" ng-click="sortedType = 'lastName'; sortedReverse = !sortedReverse" style="cursor:pointer;">Last Name</span>
                    </th>
            </tr>
            </thead>
            <tbody>
            <tr ng-repeat="(k, v) in retrievedData | orderBy: sortedType: sortedReverse">
                <td>{{v.firstName}}</td>
                <td>{{v.lastName}}</td>
                <td>
                    <button class="btn btn-primary">Edit</button>
                    <button class="btn btn-primary" ng-click="removeRow();">Delete</button>
                </td>
            </tr>
            </tbody>
        </table>

我的ng-controllerng-app已在html中分配,因此我们无需担心。 removeRow()函数在这里发生的事情是我设法删除该行,但我还需要它从localStorage中删除它。此时,执行$window.localStorage.removeItem('initData');不是一个选项,因为它会删除整个对象。我怎么能这样做?

如何删除仅包含随行删除的数据的localStorage部分?

编辑:我知道我无法编辑键值,但如何设置新值?将$window.localStorage.setItem('initData', JSON.stringify($scope.initData));放在函数中并没有多大帮助。

SOLUTIN:感谢那些回答的人。我通过在$scope.initData.splice(row, 1);下面添加$scope.retrievedData.splice(row, 1);来修复它。我已经发现我已经在row参数中使用了索引。之后我写道:$window.localStorage.setItem('initData', JSON.stringify($scope.initData));谢谢。

1 个答案:

答案 0 :(得分:1)

无法编辑localStorage值的内容,只能覆盖它。

您应该做的是修改$scope.initData,然后使用$window.localStorage.setItem('initData', JSON.stringify($scope.initData));覆盖它。

您需要将点击更改为ng-click="removeRow($index)"$index是当前重复的元素索引)然后在函数中    $ scope.removeRow = function(rowIndex){         $ scope.retrievedData.splice(rowIndex,1);         $ window.localStorage.setItem('initData',JSON.stringify($ scope.retrievedData));     }

因为你在再次保存到localstorage之前更改了retrievedData,它将获得数据的新值 - 即没有行。

相关问题