HTML5 local storage sort

后端 未结 2 1192
北恋
北恋 2020-12-20 22:22

I am using local storage to store user entries and am displaying the entries on another page. I need a way to sort them based on the most recent date and time of edit. Is th

2条回答
  •  情歌与酒
    2020-12-20 23:12

    You've got to pair the timestamp with the stored value somehow, you can create a wrapper object for each value and store the value and the timestamp in a single object. Assuming you have a value myvalue you want to store with reference myref:

    var d=new Date();
    var storageObject = {};
    storageObject.value = myvalue;
    storageObject.timestamp = d.getTime();
    localStorage.setItem(myref, JSON.stringify(storageObject));
    

    On the other page you then need to rehydrate your objects into an array and implement your compareFunction function.

    Your other option would be to use Web SQL Database and Indexed Database API which lets you more naturally store and query this sort of multifaceted info, but you would probably have to create some sort of abstract wrapper to make things work easily cross browser.

提交回复
热议问题