0

I have a SpringBoot application I have a Datatable 1.10.15 defined where I want to Keep the selected row after submit. This is the code in my Thymeleaf template

<script th:inline="javascript">
/*<![CDATA[*/   

$(document).ready(function() {


    $('#deviceEventTable').dataTable( {
        order: [[ 0, "desc" ]],
        select: true,
        bLengthChange: false,
        stateSave: true,
        pageLength: 20,
        initComplete: function() {
            var api = this.api();

            if (localStorage.getItem( 'DataTables_selected' )!=null && localStorage.getItem( 'DataTables_selected' ) != 'undefined') {          
                var selected = localStorage.getItem( 'DataTables_selected' ).split(',');
                var selected = '0';
                selected.forEach(function(s) {
                api.row(s).select();
                })
            }

          } 
    });


    table.on('select.dt deselect.dt', function() {
          localStorage.setItem( 'DataTables_selected', table.rows( { selected: true }).toArray() )   
        })      
} );

/*]]>*/
</script>

But the row is not selected and instead I have this error:

jquery.min.js:2 Uncaught ReferenceError: table is not defined
    at HTMLDocument.<anonymous> (1:473)
    at j (jquery.min.js:2)
    at k (jquery.min.js:2)
1
  • 1
    You haven't defined a variable table. Commented Jun 30, 2017 at 12:24

2 Answers 2

1

This can help

 $(document).ready(function() {


 table = $('#deviceEventTable').dataTable( {
    order: [[ 0, "desc" ]],
    select: true,
    bLengthChange: false,
    stateSave: true,
    pageLength: 20,
    initComplete: function() {
        var api = this.api();

        if (localStorage.getItem( 'DataTables_selected' )!=null && localStorage.getItem( 'DataTables_selected' ) != 'undefined') {          
            var selected = localStorage.getItem( 'DataTables_selected' ).split(',');
            var selected = '0';
            selected.forEach(function(s) {
            api.row(s).select();
            })
        }

      } 
});

table.on....
.....
Sign up to request clarification or add additional context in comments.

Comments

0

You have forgotten to initialize table variable:

$(document).ready(function() {


    var table = $('#deviceEventTable').dataTable( {
        order: [[ 0, "desc" ]],
        select: true,
        bLengthChange: false,
        stateSave: true,
        pageLength: 20,
        initComplete: function() {
            var api = this.api();

            if (localStorage.getItem( 'DataTables_selected' )!=null && localStorage.getItem( 'DataTables_selected' ) != 'undefined') {          
                var selected = localStorage.getItem( 'DataTables_selected' ).split(',');
                var selected = '0';
                selected.forEach(function(s) {
                api.row(s).select();
                })
            }

          } 
    });


    table.on('select.dt deselect.dt', function() {
          localStorage.setItem( 'DataTables_selected', table.rows( { selected: true }).toArray() )   
        })      
} );    

1 Comment

I do like this and I get selected first row after submit by condition I select no row of table. How can I get table without selected rows by condition I select no row?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.