简体   繁体   中英

How to delete specific row in table in localstorage in dataTables

I want to do is delete the value in localstorage on that specific row so that it wont comeback even if the user refresh the page.

My problem is it doesnt delete the localstorage value of that specific row that i want to be remove.

current output: http://jsfiddle.net/4GP2h/75/

$(document).on('click', '.delete', function () {
    var row = $(this).closest('tr');
    oTable.row(row).remove().draw();  
    localStorage.removeItem(row);
});

Not very well, but working example, based on comparing name, age and gender:

$(document).on('click', '.delete', function () {
    var row = $(this).closest('tr');
    oTable.row(row).remove().draw();
    var rowElements = row.find("td");
    for (var i = 0; i < dataSet.length; i++) {
        var equals = true;
        for (var j = 0; j < 3; j++) {
            if (dataSet[i][j] != rowElements[j].innerHTML) {
                equals = false;
                break;
            }
        }
        if (equals) {
            dataSet.splice(i, 1);
            break;
        }
    }
    localStorage.setItem('dataSet', JSON.stringify(dataSet));
});

And the fiddle of this

You need to confirm the value is stored with the key row.

LocalStorage is a key-value store. Where both keys and values have to be strings. row here is a DOM object and is probably not a key.

Usually a large Object is JSON.strigify'ed and saved in Local Storage. In that case you would need to parse that object, remove the one property you don't want and override the existing key-value pair.

clear a local storage setting you would need to call localStorage.remove('key'); where 'key' is the key of the value you want to remove. If you want to clear all settings, you need to call localStorage.clear() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM