Why doesn't my "create new record" or edit/delte icons work

Why doesn't my "create new record" or edit/delte icons work

This question has an accepted answers - jump to answer

«1

Answers

  • kthorngrenkthorngren Posts: 21,364Questions: 26Answers: 4,956

    First, you are getting this error:

    Uncaught TypeError: Cannot read properties of undefined (reading 'style')

    You have defined 3 th in the header but have for columns of data.

    Second, I might be missing it but I don't see any event handlers for your buttons. See this example explaining how to create jQuery events. See this example with buttons in a column:
    http://live.datatables.net/xijecupo/1/edit

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    forgot to include the javascript

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    This error just started when I added the icons

    Uncaught TypeError: Cannot read properties of undefined (reading 'style')
    not sure why

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0
    edited February 2022

    so I guess I am missing an event handler?

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    not getting this error anymore:
    Uncaught TypeError: Cannot read properties of undefined (reading 'style')

  • kthorngrenkthorngren Posts: 21,364Questions: 26Answers: 4,956

    Did you try the example I linked to?
    http://live.datatables.net/xijecupo/1/edit

    It shows how to create a click event for buttons in the columns and to get the row data.

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    yes I did. Didn't seem to do anything for me..

    However I did try this If you click on any row it does alert
    http://live.datatables.net/cihuvamo/1/edit
    but it says
    You clicked on undefined row
    I was hoping it would have the module value

  • kthorngrenkthorngren Posts: 21,364Questions: 26Answers: 4,956

    Your test case is confusing as you have JS code in both the HTML tab and the JS tab. You are using this code for your click event:

        $('#modport tbody').on('click', 'tr', function () {
            var data = table.row( this ).data();
            alert( 'You clicked on '+data[0]+' row' );
        } );
    

    data[0] is for array data but you are using objects. You will need to use data.Module, like this

    http://live.datatables.net/tajuqobu/1/edit

    Likely you will need a more specific selector so you can determine if edit or delete was clicked.

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    Likely you will need a more specific selector so you can determine if edit or delete was clicked. I am not sure what you mean here.

  • kthorngrenkthorngren Posts: 21,364Questions: 26Answers: 4,956

    Take a look at the example I posted:
    http://live.datatables.net/xijecupo/1/edit

    It has this:

    $('#example tbody').on('click', '.name', function () {
    

    The classname name is assigned to the Name button. You will need something similar so you know when you click on the edit or delete buttons. You can use a classname, like this:
    http://live.datatables.net/tajuqobu/2/edit

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    ok got it.. so if I want my edit, add & delete to work like this:
    https://editor.datatables.net/examples/simple/inTableControls.html
    Does that require PHP? or can I accomplish the same without?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    If you want it to behave like Editor, it would be worth buying an Editor license, rather than redeveloping the wheel :) Editor includes both the browser-side code, and the PHP for the server to update the data source,

    Colin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    um ok well I really do not use it enough to buy a license. just was hoping to get an answer to adding, editing and deleting

  • kthorngrenkthorngren Posts: 21,364Questions: 26Answers: 4,956

    If you want to save the edits to a database then you will need to use an ajax request to send the updated data to the server. In your event handler for the buttons you will package the data to send via ajax and in the success function you will update the client side Datatable.

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    Hi Kevin,

    Yea I have that piece already written. I am having an issue getting the "create record", "Edit" & "Delete" stuff to work. Just need an example of how to add a record or deleting a record

  • kthorngrenkthorngren Posts: 21,364Questions: 26Answers: 4,956

    For creating a new record you will need a create button, like the example you linked. It will present a form with all the fields then, when submitted, an ajax request is sent to the server. There is nothing Datatables specific about this.

    To delete a record you can submit, via ajax, the whole or of data or just the unique row id. Likely you will want a confirmation message. You can get the row data this similar to the edit example posted earlier:
    http://live.datatables.net/tajuqobu/2/edit

    To edit you will get the row data, like the example, and populate a form with the data. Then send the data, via ajax, to update the record in the DB.

    Except for getting the row data of the clicked row there is nothing specific to Datatbles for the above processes. Stack Overflow or other tutorials will be the pale to look for information of how to submit edit forms to the server for updating the database.

    Once the database has been updated then the server script should provide a response to the client. The ajax success function will receive the response. There are a couple of options for updating the Datatables data. The simple option is to use ajax.reload() to refresh the table from the database. The other is to have the server respond with the specific updates an Datatables updates the specific records in the table, similar to the Editor client/server protocol. Use the following API's to update the data:

    Kevin

  • kthorngrenkthorngren Posts: 21,364Questions: 26Answers: 4,956

    When I first started with Datatables I was new to Javascript and HTML I decided the small cost of Editor was well worth my time. It would have taken me a long to to work out the code needed for basic editing. Purchasing it allowed me to concentrate on the rest of the application.

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    hi Kevin,

    So I tried this for deleting a record:

     // New record
     33         $('#modport tbody').on('click', 'create', function () {
     34            var jRow = $('<tr>').append('<td>Module</td>','<td>Ports</td>','<td>Edit</td>','<td>Delete</td>');
     35            table.row.add(jRow).draw();
     36     } );
     37    
     38     // Edit record
     39     $('#modport tbody').on('click', 'tr .fa-pencil', function () {
     40         var data = table.row( this.closest('tr') ).data();
     41         alert( 'Edit:n '+data.Module+' row' );
     42     } );
     43     
     44     // Delete a record
     45     $('#modport tbody').on('click', 'tr .fa-trash', function () {
     46         var data = table.row( this.closest('tr') ).data();
     47         data.row( this ).delete();
     48     } );
    

    but it say " table is not defined"

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Check and see if it is defined, it sounds like a generic variable out of scope. But as I said before, it would be easier to look at Editor - we'll be happy to support any issues with your Editor implementation :)

    Colin

  • kthorngrenkthorngren Posts: 21,364Questions: 26Answers: 4,956

    Sounds like you haven't defined the variable table or you defined it in a different context. For example:
    http://live.datatables.net/xijecupo/1/edit

    Note the var table = $('#example').DataTable({ .. }) to define the variable table which has an instance of the Datatables API.

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0
    edited February 2022

    yep so I added it

        var table = $('#modport').DataTable;
        // New record
            $('#modport tbody').on('click', 'create', function () {
               var jRow = $('<tr>').append('<td>Module</td>','<td>Ports</td>','<td>Edit</td>','<td>Delete</td>');
               table.row.add(jRow).draw();
        } );
    
        // Edit record
        $('#modport tbody').on('click', 'tr .fa-pencil', function () {
            var data = table.row( this.closest('tr') ).data();
            alert( 'Edit:n '+data.Module+' row' );
        } );
    
        // Delete a record
        $('#modport tbody').on('click', 'tr .fa-trash', function () {
            var data = table.row( this.closest('tr') ).data();
            data.row( this ).delete();
        } );
    

    now I get "TypeError: table.row is not a function"

    Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide

  • kthorngrenkthorngren Posts: 21,364Questions: 26Answers: 4,956

    This is incorrect:

    var table = $('#modport').DataTable;
    

    Use this with the parenthesis ():

    var table = $('#modport').DataTable();
    

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    ok got it

    var table = $('#modport').DataTable();
    // New record
        $('#modport tbody').on('click', 'create', function () {
           var jRow = $('<tr>').append('<td>Module</td>','<td>Ports</td>','<td>Edit</td>','<td>Delete</td>');
           table.row.add(jRow).draw();
    } );
    
    // Edit record
    $('#modport tbody').on('click', 'tr .fa-pencil', function () {
        var data = table.row( this.closest('tr') ).data();
        alert( 'Edit:n '+data.Module+' row' );
    } );
    
    // Delete a record
    $('#modport tbody').on('click', 'tr .fa-trash', function () {
        var data = table.row( this.closest('tr') ).data();
        table.row( data ).delete();                       <===================
    } );
    

    new error:
    "table.row(...).delete is not a function"
    :(

  • kthorngrenkthorngren Posts: 21,364Questions: 26Answers: 4,956
    $('#modport tbody').on('click', 'tr .fa-trash', function () {
        var data = table.row( this.closest('tr') ).data();
        table.row( data ).delete();                       <===================
    } );
    

    table.row( this.closest('tr') ).data();

    Returns the data for the row. This won't work as a row-selector. Use something like this:

    $('#modport tbody').on('click', 'tr .fa-trash', function () {
        var row = table.row( this.closest('tr') );
        table.row( row ).delete(); 
    } );
    

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    hmm...

    var table = $('#modport').DataTable();
    // New record
        $('#modport tbody').on('click', 'create', function () {
           var jRow = $('<tr>').append('<td>Module</td>','<td>Ports</td>','<td>Edit</td>','<td>Delete</td>');
           table.row.add(jRow).draw();
    } );
    
    // Edit record
    $('#modport tbody').on('click', 'tr .fa-pencil', function () {
        var data = table.row( this.closest('tr') ).data();
        alert( 'Edit:n '+data.Module+' row' );
    } );
    
    // Delete a record
    $('#modport tbody').on('click', 'tr .fa-trash', function () {
      var row = table.row( this.closest('tr') );
      table.row( row ).delete();
    } );
    

    still getting this error:

       Uncaught TypeError: table.row(...).delete is not a function
    
  • kthorngrenkthorngren Posts: 21,364Questions: 26Answers: 4,956

    The API to use is row().remove() not delete(). You will need to add draw() to update the table display, something like ths:

    table.row( row ).remove().draw();
    

    Kevin

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    yay thank you.. that worked!! I owe you like a case a beer!! :)

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    http://live.datatables.net/dugigipe/4/edit
    Ok I am getting real close I have delete working and the popup for the create record.
    but when I click the create button the new row is not added.
    is my function "addModule" with table.row correct?

  • drfunkdrfunk Posts: 58Questions: 8Answers: 0

    now new row is added but only the Module value

Sign In or Register to comment.