/    Sign up×
Community /Pin to ProfileBookmark

Ways to pin or duplicate items to the top of a table

I have a single [webpage](https://www.alancrookes.com) that displays a list of teams competing in a tournament in table format (generated by https://divtable.com/table-styler/).

The user can click on each row to highlight it and JS code changes the CSS style of that row and vice versa if it’s highlighted will remove the style.

They can also clear all styles by clicking on a link at the top of the page.

All the information is stored using `localstorage`.
What I am trying to achieve now is

  • make the favourites or highlighted rows to also appear at the top also to save people scrolling.

  • Clear the localstorage variable when they arrive on the site after a certain date but only clear it the once e.g. as in don’t clear it each time they arrive after a date.
  • My questions are

  • is it possible to use the localstorage variable in PHP to pass those values to a query and replicate them in a new header table.

  • Maybe there is a better way to implement what I want to do and someone might suggest the process and I’ll investigate and try to develop a solution. I am eventually trying to get it somewhat similar to this [page](https://www.pgatour.com/leaderboard.html) when you click the + sign it adds them as a favourite.
  • “`
    <script>

    $(‘#clearHighlights’).click(function(){ window.localStorage.clear(“storedEntries”); return false; });

    if(localStorage.getItem(‘storedEntries’) === null){
    var selectedTeams = [];
    }else{
    var selectedTeams = JSON.parse( window.localStorage.getItem(‘storedEntries’) );
    var arrayLength = selectedTeams.length;
    console.log(arrayLength);
    for (var i = 0; i < arrayLength; i++) {
    document.getElementById(selectedTeams[i]).className = “highlight”;
    }
    }

    var teamIndex;

    $(‘#leaderboard’).on(‘click’, ‘tr’, function() {
    if($(this).hasClass(‘highlight’)){
    $(this).removeClass(‘highlight’)
    teamIndex = selectedTeams.indexOf(this.getAttribute(‘id’))
    selectedTeams.splice(teamIndex,1)
    window.localStorage.setItem(‘storedEntries’,JSON.stringify(selectedTeams));
    }
    else {
    $(this).addClass(‘highlight’)
    selectedTeams.push(this.getAttribute(‘id’))
    window.localStorage.setItem(‘storedEntries’,JSON.stringify(selectedTeams));
    }
    });

    </script>
    “`

    to post a comment
    CSSJavaScript

    9 Comments(s)

    Copy linkTweet thisAlerts:
    @SempervivumMar 07.2021 — >make the favourites or highlighted rows to also appear at the top also to save people scrolling.

    Create a table at the top, when a row is clicked, clone it and add the cloned version to the table at the top.

    >Clear the localstorage variable when they arrive on the site after a certain date but only clear it the once e.g. as in don't clear it each time they arrive after a date.

    Enter a timestamp to the local storage and check if a certain amount of time has passed when entering the page again.
    Copy linkTweet thisAlerts:
    @crookesaauthorMar 07.2021 — Thanks for the quick reply

    > @Sempervivum#1628994 Create a table at the top, when a row is clicked, clone it and add the cloned version to the table at the top.

    I can create a table ok and run a php query against my database but I'm just wondering how I get my localstorage values to use in the query. Or is there a CSS way to clone the table and just show the rows that have been styled based on click?

    > @Sempervivum#1628994 Enter a timestamp to the local storage and check if a certain amount of time has passed when entering the page again.

    Great thanks, I'll look into that too cheers
    Copy linkTweet thisAlerts:
    @SempervivumMar 07.2021 — I didn't get yet how your database will be involved: Roughly there are two ways of saving user specific data:
  • 1. Client side in the local storage.

  • 2. Server side in the database. To make data user specific a user management is necessary.


  • Or do you intend to save the highlighted rows in the database additionally?
    Copy linkTweet thisAlerts:
    @SempervivumMar 07.2021 — PS: Or do you need additional information for the table on top, so that cloning the row having been clicked is not sufficient?
    Copy linkTweet thisAlerts:
    @crookesaauthorMar 07.2021 — @Sempervivum#1628997

    > @Sempervivum#1628996 do you intend to save the highlighted rows in the database additionally?

    So I don't need to have it highlighted in the database just client side for the user to see their favourite (highlighted) selections. Their selections could be 1 or 10. A user would just arrive at the site with no authentication and highlight team or teams as they so wish.

    > @Sempervivum#1628997 PS: Or do you need additional information for the table on top, so that cloning the row having been clicked is not sufficient?

    If there is 422 as there is now, they shouldn't have to scroll down hundreds or rows to find their selections. I want them to appear as a separate table on top of the existing ones with just their selections, so as it's refreshed they can see their teams progress and compare to the leaders on the main table below.

    I refresh the table every 60 seconds with &lt;meta http-equiv="refresh" content="60"&gt;. It queries the database on each refresh to get the most up to date leaderboard. There is separate cron jobs running in the background to create the data for the table.
    Copy linkTweet thisAlerts:
    @SempervivumMar 07.2021 — I see. However, just for displaying the selected rows on top additionally, IMO there is no need to involve the database. Just clone the row when clicked and enter it to the additional table or get the row based on local storage and do the same.
    Copy linkTweet thisAlerts:
    @crookesaauthorMar 07.2021 — @Sempervivum#1628999

    Sounds like the easiest solution, can you direct to how do that? I'm not sure of that technique.

    Also will the row still be there after a page refresh or only when clicked?
    Copy linkTweet thisAlerts:
    @SempervivumMar 07.2021 — Create the table on top and apply an appropriate ID, e. g. "highlights". Then extend your code like this:
    ``<i>
    </i>$('#leaderboard').on('click', 'tr', function() {
    if($(this).hasClass('highlight')){
    $(this).removeClass('highlight')
    teamIndex = selectedTeams.indexOf(this.getAttribute('id'))
    selectedTeams.splice(teamIndex,1)
    window.localStorage.setItem('storedEntries',JSON.stringify(selectedTeams));
    }
    else {
    $(this).addClass('highlight')
    selectedTeams.push(this.getAttribute('id'))
    // Clone the clicked row:
    const trCloned = $(this).clone(true);
    // Modidy ID in order to keep it unique:
    trCloned.attr('id', trCloned.attr('id') + '-top');
    // Append cloned row to the table on top:
    $('#highlights tbody').append(trCloned);
    window.localStorage.setItem('storedEntries',JSON.stringify(selectedTeams));
    }
    });<i>
    </i>
    ``

    (not tested)

    Still to do:
  • - Remove row on top when highlighted row is clicked again.

  • - Restore table on top from local storage. Hint: Should be done easily by applying or simulating a click on the row after having been restored from local storage.
  • Copy linkTweet thisAlerts:
    @crookesaauthorMar 22.2021 — @Sempervivum#1629002 Just got around to trying this at the weekend and seems to work good.

    Another follow on question I had for this was that when the page reloads the cloning is gone. I presume to make it persistent I'd have to write so Javascript to check the localstorage and run the cloning each time?

    My existing code highlights the current table but on reload it keeps the highlight row with the 'highlight' class. Not sure how it does this and wondering if I could re-use it somehow?

    All the Javascript code I have is already above so not sure how the persistent class is being kept on the main table #leaderboard
    ×

    Success!

    Help @crookesa spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.24,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...