How do I remove the NEW button based on the record count

How do I remove the NEW button based on the record count

tonyhyamstonyhyams Posts: 4Questions: 2Answers: 0

I want to limit a user to adding no more than 30 records. In order to do this I just wanted to not display the NEW record button if the number of records returned for them was > 30. Any ideas how to do this, I've tried various things and thought that using the page.info() might work but can't seem to get the syntax right

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,488Questions: 1Answers: 10,467 Site admin

    Hi,

    At the moment you would need to do something like this, using drawCallback or draw:

    function () {
      if ( table.page.info().recordsTotal >= 30 ) {
        $('#ToolTables_example_0').css( 'display', 'none' );
      }
    }
    

    The nasty bit is the selector for the TableTools button. You'll need to use the inspector in your browser to find the correct ID for your button. An upgrade is coming which will allow this behaviour through an IP.

    Regards,
    Allan

  • tonyhyamstonyhyams Posts: 4Questions: 2Answers: 0
    edited March 2015

    I've progressed a little, I've found the selector and can make it hide. However the condition table.page.info().recordsTotal >= 30 is returning a "Uncaught ReferenceError: table is not defined" error. I assume the table variable needs to be defined so page.info can be called. It is this syntax I can't get right. My code below. Thx for your help

    $('#bnbdata').DataTable( {
        ajax: 'php/bnb.php',
        dom:  'Tfrtip',
        columns: [
            { data: 'id' },
            { data: 'title' },
            { data: 'description' },
            { data: 'price' },
            { data: 'category' },
            // etc
        ],
        tableTools: {
            sRowSelect: 'os',
            aButtons: [
                { sExtends: 'editor_create', editor: editor },
                { sExtends: 'editor_edit',   editor: editor },
                { sExtends: 'editor_remove', editor: editor }
            ]
        },
         "drawCallback": function( settings ) {
             if (table.page.info().recordsTotal >= 30 ) {
      $('#ToolTables_bnbdata_0').css( 'display', 'none' );
      }
        }
    } );
    
  • allanallan Posts: 63,488Questions: 1Answers: 10,467 Site admin
    Answer ✓

    Try this:

    $('#bnbdata').DataTable( {
        ajax: 'php/bnb.php',
        dom:  'Tfrtip',
        columns: [
            { data: 'id' },
            { data: 'title' },
            { data: 'description' },
            { data: 'price' },
            { data: 'category' },
            // etc
        ],
        tableTools: {
            sRowSelect: 'os',
            aButtons: [
                { sExtends: 'editor_create', editor: editor },
                { sExtends: 'editor_edit',   editor: editor },
                { sExtends: 'editor_remove', editor: editor }
            ]
        },
         "drawCallback": function( settings ) {
             var table = this.api();
    
             if (table.page.info().recordsTotal >= 30 ) {
                $('#ToolTables_bnbdata_0').css( 'display', 'none' );
             }
        }
    } );
    

    table wasn't defined. You could have used it from the DataTable() call, but the above is the way to be absolutely certain.

    Allan

  • tonyhyamstonyhyams Posts: 4Questions: 2Answers: 0

    Thanks this works fine. Thank for your help, saved me a load of time.

This discussion has been closed.