Page of selected record

Page of selected record

rf1234rf1234 Posts: 2,787Questions: 85Answers: 403

How can I get the page index of the record currently selected?

My use case:
The user selects a record, all non-selected records are excluded from display. The user edits the record and saves it. Since the table order is influenced by the editing the selected record is usually no longer on the same page. Hence the user sees an empty page because the selected record - which is the only visible record - is no longer on that page. I would like to navigate to the page of the selected record on "submitSuccess".

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 19,891Questions: 26Answers: 4,691

    There are a couple of plugins to show the page of the desired row:
    https://datatables.net/plug-ins/api/

    Maybe one of them will help.

    Kevin

  • rf1234rf1234 Posts: 2,787Questions: 85Answers: 403
    edited December 2022

    Hi Kevin,

    thanks!

    I looked at those plugins and this seemed to be relevant:
    https://datatables.net/plug-ins/api/page.jumpToData()

    I finally got this code working but it is so bad that I don't want to use it. I still haven't found a way to determine the position of a selected row in a data table. I needed to take a detour
    - get the value of a field from the selected row (in my case "serial")
    - assign a class to the column of that field in the HTML to be able to find it
    - search for the value of "serial" in the right column of the selected row and determine the row's position as the "indexOf" the column cells

    What I need is a direct way to determine the position of a selected row. And I learned: It is not its "index". That is something data tables internal that doesn't coincide with the rows position.

    So this is the "bad" code that works:

    .on('submitSuccess', function (e, json, data, action) {   
        if ( action !== "remove" && typeof json.data[0] !== 'undefined' ) {
           var serial = parentTable.row({selected: true}).data().ctr.serial;
           var pos = parentTable.column('.serial', {order: 'current'}).data().indexOf( serial );
           var page = Math.floor( pos / parentTable.page.info().length );
           parentTable.page( page ).draw( false );
        }
    })
    

    This is what I would like to have but doesn't work because "index()" is not the position of the selected row:

    .on('submitSuccess', function (e, json, data, action) {   
        if ( action !== "remove" && typeof json.data[0] !== 'undefined' ) {
           var pos = parentTable.row({selected: true}).index();
           var page = Math.floor( pos / parentTable.page.info().length );
           parentTable.page( page ).draw( false );
        }
    })
    

    Any ideas? Or maybe @allan ?

    Roland

  • rf1234rf1234 Posts: 2,787Questions: 85Answers: 403

    Of course I would prefer this over all that complicated stuff above. I guess that would require new API features. "row().page()" to get the page of the specified row.

    var page = parentTable.row({selected: true}).page();
    parentTable.page( page ).draw( false );
    
  • kthorngrenkthorngren Posts: 19,891Questions: 26Answers: 4,691
    edited December 2022 Answer ✓

    I put together a simple example using row().show():
    http://live.datatables.net/guwafemu/319/edit

    Looks like this:

      editor.on('submitSuccess', function (e, json, data, action) {  
        if ( action !== "remove" && typeof json.data[0] !== 'undefined' ) {
          parentTable.row({selected: true}).show().draw(false);
        }
    })
    

    Kevin

  • rf1234rf1234 Posts: 2,787Questions: 85Answers: 403

    Hey Kevin,

    how come I overlooked Edouard's great plugin? I've got to be blind!
    "row().show()" did the trick in just one line of code!

    Many thanks for your help!
    Roland

  • allanallan Posts: 60,962Questions: 1Answers: 9,940 Site admin
    Answer ✓

    Yeah, that's awesome. Nice one :)

    Allan

Sign In or Register to comment.