How do I add button on each row in datatable?

前端 未结 7 1320
醉话见心
醉话见心 2020-11-30 23:54

I am newbie for DataTables. I want to add button on each row for edit and delete(like below image)

\"enter

相关标签:
7条回答
  • 2020-12-01 00:01

    Take a Look.

    $(document).ready(function () {     
            $('#datatable').DataTable({
                columns: [
                    { 'data': 'ID' },
                    { 'data': 'AuthorName' },
                    { 'data': 'TotalBook' },
                    { 'data': 'DateofBirth' },
                    { 'data': 'OccupationEN' },   
                    { 'data': null, title: 'Action', wrap: true, "render": function (item) { return '<div class="btn-group"> <button type="button" onclick="set_value(' + item.ID + ')" value="0" class="btn btn-warning" data-toggle="modal" data-target="#myModal">View</button></div>' } },
                ],            
                bServerSide: true,
                sAjaxSource: 'EmployeeDataHandler.ashx'           
            });       
        });
    
    0 讨论(0)
  • 2020-12-01 00:07

    I contribute with my settings for buttons: view, edit and delete. The last column has data: null At the end with the property defaultContent is added a string that HTML code. And since it is the last column, it is indicated with index -1 by means of the targets property when indicating the columns.

    //...
    columns: [
        { title: "", "data": null, defaultContent: '' }, //Si pone da error al cambiar de paginas la columna index con numero de fila
        { title: "Id", "data": "id", defaultContent: '', "visible":false },
        { title: "Nombre", "data": "nombre" },
        { title: "Apellido", "data": "apellido" },
        { title: "Documento", "data": "tipo_documento.siglas" },
        { title: "Numero", "data": "numero_documento" },
        { title: "Fec.Nac.", format: 'dd/mm/yyyy', "data": "fecha_nacimiento"}, //formato
        { title: "Teléfono", "data": "telefono1" },
        { title: "Email", "data": "email1" }
        , { title: "", "data": null }
    ],
    columnDefs: [
        {
            "searchable": false,
            "orderable": false,
            "targets": 0
        },
        { 
          width: '3%', 
          targets: 0  //la primer columna tendra una anchura del  20% de la tabla
        },
        {
            targets: -1, //-1 es la ultima columna y 0 la primera
            data: null,
            defaultContent: '<div class="btn-group"> <button type="button" class="btn btn-info btn-xs dt-view" style="margin-right:16px;"><span class="glyphicon glyphicon-eye-open glyphicon-info-sign" aria-hidden="true"></span></button>  <button type="button" class="btn btn-primary btn-xs dt-edit" style="margin-right:16px;"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></button><button type="button" class="btn btn-danger btn-xs dt-delete"><span class="glyphicon glyphicon-remove glyphicon-trash" aria-hidden="true"></span></button></div>'
        },
        { orderable: false, searchable: false, targets: -1 } //Ultima columna no ordenable para botones
    ], 
    //...
    

    enter image description here

    0 讨论(0)
  • 2020-12-01 00:15
    var table =$('#example').DataTable( {
        data: yourdata ,
        columns: [
            { data: "id" },
            { data: "name" },
            { data: "parent" },
            { data: "date" },
    
            {data: "id" , render : function ( data, type, row, meta ) {
                  return type === 'display'  ?
                    '<a href="<?php echo $delete_url;?>'+ data +'" ><i class="fe fe-delete"></i></a>' :
                    data;
                }},
        ],
        }
    }
    
    0 讨论(0)
  • 2020-12-01 00:20

    well, i just added button in data. For Example, i should code like this:

    $(target).DataTable().row.add(message).draw()
    

    And, in message, i added button like this : [blah, blah ... "<button>Click!</button>"] and.. it works!

    0 讨论(0)
  • 2020-12-01 00:22

    my recipe:

    datatable declaration:

    defaultContent: "<button type='button'....
    

    events:

    $('#usersDataTable tbody').on( 'click', '.delete-user-btn', function () { var user_data = table.row( $(this).parents('tr') ).data(); }
    
    0 讨论(0)
  • 2020-12-01 00:23

    take a look here... this was very helpfull to me https://datatables.net/examples/ajax/null_data_source.html

    $(document).ready(function() {
        var table = $('#example').DataTable( {
            "ajax": "data/arrays.txt",
            "columnDefs": [ {
            "targets": -1,
            "data": null,
            "defaultContent": "<button>Click!</button>"
        } ]
    } );
    
    $('#example tbody').on( 'click', 'button', function () {
        var data = table.row( $(this).parents('tr') ).data();
        alert( data[0] +"'s salary is: "+ data[ 5 ] );
        } );
    } );
    
    0 讨论(0)
提交回复
热议问题