Checkbox disappears when trying to add in additional formatted columns

Checkbox disappears when trying to add in additional formatted columns

rajc2020rajc2020 Posts: 10Questions: 3Answers: 0

I'm trying to create a table with a checkbox in the first column using this code:

    <script>
        $.fn.dataTable.ext.buttons.view = {
          text: 'View Data',
            action: function ( e, dt, node, config ) {
            alert( 'Button activated' );
          }
        };
        $.fn.dataTable.ext.buttons.design = {
          text: 'Design',
            action: function ( e, dt, node, config ) {
            alert( 'Button activated' );
          }
        };
        $(document).ready(function() {
            $('#results').DataTable( {
                columnDefs: [ {
                    orderable: false,
                    className: 'select-checkbox'
                    targets: 0,
                },
                ],
                select: {
                    style:    'multi',
                    selector: 'td:first-child'
                },
                dom: 'Bfrtip',
                buttons: [
                  'copy', 'csv', 'excel', 'view', 'design'
                ]

            } );
        } );
    </script>

I would like to add a column rendering to the 14th and 16th column of my table and i thought I could do this in the columnDefs part:

                columnDefs: [ {
                    orderable: false,
                    className: 'select-checkbox'
                    targets: 0,
                },
                {
                    targets: [14,16],
                    "render": function (data, type, row, meta) {
                      var pm_list = data.split(";")
                      var pmid;
                      var value = "";
                      for pmid in pm_list {
                        value += '<a href="https://pubmed.ncbi.nlm.nih.gov/'+ pmid +'">' + pmid + '</a>;'
                      } 
                      return value;
                    }   
                }
                ],

But this not only didn't work, but my select checkboxes disappeared. The other I wanted to do was to be able to freeze the first six columns when scrolling to the right as my table has 16 columns.

Any help with this would be greatly appreciated!

Raj

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    Nothing obvious stands out in you code snippet. Do you get errors in your browser's console?

    able to freeze the first six columns when scrolling to the right as my table has 16 columns.

    Take a look at the FixColumns Extension.

    Kevin

  • rajc2020rajc2020 Posts: 10Questions: 3Answers: 0

    Thanks for your quick response Kevin. I don't get any errors in the console. What happens is that the checkboxes disappear. It seems as though when adding another column def, it just overwrites it.

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    I don't get any errors in the console.

    I copied the above code into a test case and found two syntax errors. Please see the test case with comments of the syntax error fixes:
    http://live.datatables.net/lipiriya/1/edit

    Kevin

  • rajc2020rajc2020 Posts: 10Questions: 3Answers: 0

    Thanks Kevin! Much appreciated with your help. I notice that it just hyperlinks 0s and 1s for some reason and not the data itself. I've managed to incorporate what you did but it's not hyperlinking with the data that's there

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923
    Answer ✓

    I notice that it just hyperlinks 0s and 1s for some reason and not the data itself.

    If you are talking about in my example thats probably true since I don't have data with semicolons for data.split(";") to work. However if you are having problems in our environment please update the test case to reflect your data.

    Or you can do a bit of debugging to make sure pm_list, etc is what you expect it to be and right click on the link and inspect it to see the HTML being built. Datatables isn't going to influence the link.

    Kevin

  • rajc2020rajc2020 Posts: 10Questions: 3Answers: 0

    I figured out what happened. The for loop was outputting array indices instead of the elements. I fixed it and it works! Thank you so much!

This discussion has been closed.