Radio Buttons on a DataTable

Radio Buttons on a DataTable

pedrocsapedrocsa Posts: 9Questions: 1Answers: 0

Hi,

I am using a datatable with checkboxes on the first column that permit multiple selection.

But now, I need that this checkboxes permit only single selection or it can be used a radio button on it.

This is the link to show my page http://cursosites-com-br.stackstaging.com/suporte_datatables.php

Can someone help me?

Regards,

Pedro

Replies

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    edited September 2017

    Not sure if you are using the select extension. This select example uses checkboxes and allows multiple selections if you hold down shift or control. But if you change style: 'os' to style: 'single' then you can only select one row even if shift or control are used.

    https://datatables.net/extensions/select/examples/initialisation/checkbox.html

    Kevin

  • pedrocsapedrocsa Posts: 9Questions: 1Answers: 0

    I wasn't Kthorngren, but I tried to insert this on the code, but it still not working, I'm still being able of selecting multiple checkboxes...

    The code for the datatable is written this way, check if I used the selectextension right...

               var table = $('.dataTables-example').DataTable({
                    dom: '<"html5buttons"B>lTfgitp',
                    columnDefs: [
                        {targets: 0, checkboxes: {selectRow: true}}
                    ],
                    buttons: [
                        {extend: 'copy'},
                        {extend: 'csv'},
                        {extend: 'excel', title: 'ExampleFile'},
                        {extend: 'pdf', title: 'ExampleFile'},
                        {extend: 'print',
                         customize: function (win){
                            $(win.document.body).addClass('white-bg');
                            $(win.document.body).css('font-size', '10px');
                            $(win.document.body).find('table')
                                .addClass('compact')
                                .css('font-size', 'inherit');
                            }
                        }
                    ],
                select: {
                            style:    'single',
                            selector: 'td:first-child'
                        }
                });
    
  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    Looked at your code and you are using the "checkboxes" plugin by gyrocode. I'm not sure if there is an option within that plugin to allow for only one row to be selected. There is this note in the docs:
    https://www.gyrocode.com/projects/jquery-datatables-checkboxes/

    "If you’re using Select extension, you may want to enable multiple row selection using select.style option."

    With this in the example:

       'select': {
          'style': 'multi'
       },
    

    Did you load the 'select' extension JS? If not you will need to load that and use the same example but change to this:

       'select': {
          'style': 'single'
       },
    

    I haven't tried that with the checkboxes plugin but I think it should work.

    Here is the docs for the select extension:
    https://datatables.net/extensions/select/

    Kevin

  • pedrocsapedrocsa Posts: 9Questions: 1Answers: 0

    Thank you Kevin!

    I managed to find an example in the comments of the this link (https://www.gyrocode.com/articles/jquery-datatables-row-selection-using-checkboxes-and-select-extension/comment-page-3/#comments)

    But I'll try to use checkboxes withtou the plugin, I didn't realize before that I was using an external plugin.

    Best regards,

    Pedro

This discussion has been closed.