Open the same node when refresh

Open the same node when refresh

gmsettagmsetta Posts: 7Questions: 2Answers: 0

Is there a way to reopen the node that user was when he hits de refresh button of the browser?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    You would need to use stateSave for that. It wouldn't store that information automatically, you would need to add it. This example here shows stateSave storing selected rows, you would do the same for your child rows - call state.save whenever a child is opened or closed.

    Colin

  • gmsettagmsetta Posts: 7Questions: 2Answers: 0

    Ok, thks for the answer but I don't know how to adapt and where to put it on my code :

    $(document).ready(function () {
            var table = $('#example').DataTable({
                "lengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "Tudo"]],
                "language": {
                    "url": "js/datatables_Portuguese-Brasil.json"
                },
                 "data": testdata.data,
                 select:"single",
                 "columns": [
                     {
                         "className": 'details-control',
                         "orderable": false,
                         "data": null,
                         "defaultContent": '',
                         "render": function () {
                             return '<i class="fa fa-plus-circle" aria-hidden="true"></i>';
                         },
                         width:"15px"
                     },
                     { "data": "nome" },
                     { "data": "cargo" },
                     { "data": "linha" },
                     { "data": "modelo" },
                     { "data": "ativo" }
                 ],
                 "order": [[1, 'asc']]
             });
    
             // Add event listener for opening and closing details
             $('#example tbody').on('click', 'td.details-control', function () {
                 var tr = $(this).closest('tr');
                 var tdi = tr.find("i.fa");
                 var row = table.row(tr);
    
                 if (row.child.isShown()) {
                     // This row is already open - close it
                     row.child.hide();
                     tr.removeClass('shown');
                     tdi.first().removeClass('fa-minus-circle');
                     tdi.first().addClass('fa-plus-circle');
                 }
                 else {
                     // Open this row
                     row.child(format(row.data())).show();
                     tr.addClass('shown');
                     tdi.first().removeClass('fa-plus-circle');
                     tdi.first().addClass('fa-minus-circle');
                 }
             });
    
             table.on("user-select", function (e, dt, type, cell, originalEvent) {
                 if ($(cell.node()).hasClass("details-control")) {
                     e.preventDefault();
                 }
             });
         });
    
  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    It was getting caught in the spam filter, I just released it.

    The example I gave before is a good template. As I said before, you'll need to call state.save whenever the children are open or closed.

    I've knocked up an example here, hope that helps.

    Colin

  • gmsettagmsetta Posts: 7Questions: 2Answers: 0

    Almost there! If you hit more than one time the refresh button, it closes all opened children.

    I'm trying to find a way out...

  • colincolin Posts: 15,144Questions: 1Answers: 2,586
    Answer ✓

    That just needs a call to state.save() in initComplete - see here.

    Colin

  • gmsettagmsetta Posts: 7Questions: 2Answers: 0

    AWESOME!!! Thks a lot man!

This discussion has been closed.