How protect a row from edit and delete

How protect a row from edit and delete

RappiRappi Posts: 82Questions: 18Answers: 1

Hello.

I can't find any solution in the forum for my problem.

In my database the first 3 entrys are protcted and a user is not allowed to use the edit and delete button for these entrys only.
He can insert new entrys and edit and delete this but not the first 3.
How can I make this?

Rappi

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Is there some way in which these rows are identified - for example a class name? Also, what form of editing are you using - full form, inline, bubble?

    Allan

  • RappiRappi Posts: 82Questions: 18Answers: 1

    Hi Allen.

    Thanks for your reply.

    I'am using full form and the rows can identified by id.

    Here are my source:

    HTML

    <table cellpadding="0" cellspacing="0" border="0" class="display nowrap dt-responsive table table-striped table-bordered table-hover" id="tm_adrtyp" width="100%">
                    <thead>
                        <tr>
                            <th>id</th>
                            <th>Adresstyp</th>
                        </tr>
                    </thead>
                </table>
    

    Processing

    <?php
    require_once("models/config.php"); 
    
    /*
     * Editor server script for DB table tierart
     * Created by http://editor.datatables.net/generator
     */
    
    // DataTables PHP library and database connection
    include( "classes/DataTables.php" );
    $db->sql("SET character_set_client=utf8");
    $db->sql("SET character_set_connection=utf8");
    $db->sql("SET character_set_results=utf8");
    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Join,
        DataTables\Editor\Validate;
    
    
    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'tm_adrtyp', 'id' )
        ->fields(
            Field::inst( 'tm_adrtyp.id' ),
            Field::inst( 'tm_adrtyp.Typ' )
        )
        ->process( $_POST )
        ->json();
    ?>
    

    And the javascript

    /*
     * Editor client script for DB table tierart
     * Created by http://editor.datatables.net/generator
     */
    
    (function($){
    
    $(document).ready(function() {
    
    
        
        var editor = new $.fn.dataTable.Editor( {
            "ajax": "adm_adrtyp_processing.php",
            "table": "#tm_adrtyp",
            "sPaginationType": "full_numbers",
            "fields": [
                {
                    "label": "ID",
                    "name": "id"
                },
                {
                    "label": "Adresstyp",
                    "name": "Typ"
                }
            ],
             i18n: {
                create: {
                    button: "Neu",
                    title:  "Neuer Eintrag",
                    submit: "Erstellen"
                },
                edit: {
                    button: "Edit",
                    title:  "Eintrag editieren",
                    submit: "Speichern"
                },
                remove: {
                    button: "Löschen",
                    title:  "Eintrag löschen",
                    submit: "Löschen",
                    confirm: {
                        _: "Wollen Sie wirklich %d Zeilen löschen?",
                        1: "Wollen Sie den Eintrag wirklich löschen?"
                    }
                }
             }
        } );
    
        $('#tm_adrtyp').DataTable( {
    
            "language": {
            "url": "js/language/dataTables.german.lang"
            
        },
        
            "dom": "Bfrtip",
            buttons: [
                { extend: "create", editor: editor },
                { extend: "edit",   editor: editor }
                //{ extend: "remove", editor: editor }
            ],
            "select": true,
            "ajax": "adm_adrtyp_processing.php",
            "searching": false,
            "columns": [
                {
                    "data": "tm_adrtyp.id"
                },
                {
                    "data": "tm_adrtyp.Typ"
                }
            ],
        
            
        } )
    } );
    
    }(jQuery));
    

    The first 3 entrys have id 1-3 in the database.

    I hope this help and there is a solution

    Rappi

  • RappiRappi Posts: 82Questions: 18Answers: 1

    The line

    //{ extend: "remove", editor: editor }
    

    is temporary disabled.....

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    What I would suggest is replacing { extend: "edit", editor: editor } with something like:

    {
      extend: 'selected',
      text: 'Edit',
      action: function ( e, dt, node, conf ) {
        var selectedRows = dt.rows( { selected: true } );
        var dontEdit = false;
    
        selectedRows.every( function () {
          if ( this.node().id == ... ) {
            dontEdit = true;
          }
        } );
    
        if ( ! dontEdit ) {
           editor.edit( selectedRows.indexes() );
        }
      }
    }
    

    So basically you are calling edit() yourself (rather than using the default action of the edit button). Using the DataTables API we can check to see if any of the rows that are selected show not be editable.

    An alternative option is to listen for the select event - check to see if the row that was selected is one that shouldn't be editable. If so then immediately deselect it (row().deselect()). That might be easier thinking about it :-)

    Allan

  • RappiRappi Posts: 82Questions: 18Answers: 1

    Hi Allan.

    It don't work :-(

    Now I have this:

    /*
     * Editor client script for DB table tierart
     * Created by http://editor.datatables.net/generator
     */
    
    (function($){
    
    $(document).ready(function() {
    
    
        
        var editor = new $.fn.dataTable.Editor( {
            "ajax": "adm_adrtyp_processing.php",
            "table": "#tm_adrtyp",
            "sPaginationType": "full_numbers",
            "fields": [
                {
                    "label": "ID",
                    "name": "id"
                },
                {
                    "label": "Adresstyp",
                    "name": "Typ"
                }
            ],
             i18n: {
                create: {
                    button: "Neu",
                    title:  "Neuer Eintrag",
                    submit: "Erstellen"
                },
                edit: {
                    button: "Edit",
                    title:  "Eintrag editieren",
                    submit: "Speichern"
                },
                remove: {
                    button: "Löschen",
                    title:  "Eintrag löschen",
                    submit: "Löschen",
                    confirm: {
                        _: "Wollen Sie wirklich %d Zeilen löschen?",
                        1: "Wollen Sie den Eintrag wirklich löschen?"
                    }
                }
             }
        } );
    
        $('#tm_adrtyp').DataTable( {
    
            "language": {
            "url": "js/language/dataTables.german.lang"
            
        },
        
            "dom": "Bfrtip",
            buttons: [
                { extend: "create", editor: editor },
                {
                    extend: 'selected',
                    text: 'Edit',
                    action: function ( e, dt, node, conf ) {
                    var selectedRows = dt.rows( { selected: true } );
                    var dontEdit = false;
     
                    selectedRows.every( function () {
                    if ( this.node().id == 1 ) {
                        dontEdit = true;
                    }
                    } );
     
                    if ( !dontEdit ) {
                        editor.edit( selectedRows.indexes() );
                    }
                    }
                }
                //{ extend: "remove", editor: editor }
            ],
            "select": true,
            "ajax": "adm_adrtyp_processing.php",
            "searching": false,
            "columns": [
            {
                "data": "id"
            },
                {
                    "data": "Typ"
                }
            ],
        
            
        } )
    } );
    
    }(jQuery));
    

    The Edit Botton is always active and when I click on it, the editor opens but without an Save Botton....

    And it is the same wherever I click on...

    The second way I don't understand :-( My javascript is terrible ;-) I can write PHP and MySQL but javascript......

    Rappi

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Let's do it the second way - that will be easier I think.

    table.on( 'select', function ( e, dt, type, indexes ) {
      for ( var i=0, ien=indexes.length ; i<ien ; i++ ) {
        if ( ... test to see if row should be unselectable ... ) {
          dt.row( indexes[i] ).deselect();
        }
      }
    } );
    

    You just need to fill in the if condition with whatever is suitable for your use case.

    Allan

This discussion has been closed.