Set Focus to Cell by Click a Button in Table

Set Focus to Cell by Click a Button in Table

Hildeb67Hildeb67 Posts: 85Questions: 23Answers: 1

Hi everyone,
I want to set the focus to Celle KM when clicking the plus button. Unfortunately, it doesn't work.

here my code

        tablefzg.on('click', 'button', function (e) {            
            let data = tablefzg.row(e.target.closest('tr')).data();
            var action = this.className;
            let tr = e.target.closest('tr');
            let row = tablefzg.row(tr);

            if (action=='GetFZG'){                                                  
                if (this.innerHTML == '-') {
                    this.innerHTML = "+";           
                    fahrzeugstatus = 0;
        
                    this.style.backgroundColor = '#F0F0F0';     
                    var set = 0;
                        $.ajax({             
                            url: "/FOM/controllers/fzg_eintragen_einsatz.php",
                            type: 'post',            
                            data: {dsid:data.ID,wertset:set},     
                            success: function (data) { 
                                return data;                                   
                            }   
                        })                       
                } else if (this.innerHTML == '+') {               
                    this.innerHTML = "-";
                    
                    // >> Here the focus should be set on the column/cell KM <<
                    tablefzg.cell('KM').focus();
                    
                    fahrzeugstatus = 1;     
                    this.style.backgroundColor = '#60c724'; 
                    var set = 1;
                        $.ajax({             
                            url: "/FOM/controllers/fzg_eintragen_einsatz.php",
                            type: 'post',            
                            data: {dsid:data.ID,wertset:set},         
                            success: function (data) { 
                                return data;                                   
                            }   
                        })  
                }
            }   
                                    
        })

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 64,410Questions: 1Answers: 10,635 Site admin

    It's not the cell you are trying to give focus you, but rather the input element in the cell. Try:

    tablefzg.cell(tr, 3).node().querySelector('input).focus();
    

    Note that I've used .cell(tr, 3) rather than your .cell('KM') since cell() is a top level method - you need to select a specific cell, or a row and column to get a specific cell.

    Allan

  • Hildeb67Hildeb67 Posts: 85Questions: 23Answers: 1

    Thanks Allan,
    but I'm getting an error message.
    And I think a "'" was missing after the input.

    Here's the error message:

    Uncaught TypeError: Cannot read properties of null (reading 'focus')

    Chris

  • allanallan Posts: 64,410Questions: 1Answers: 10,635 Site admin

    Can you link you your page so I can use a debugger on it please?

    And yes, sorry, I missed a closing quote.

    Allan

  • kthorngrenkthorngren Posts: 21,983Questions: 26Answers: 5,077
    Answer ✓

    Make sure the column index used in cell(tr, 3) contains the input. Based on the screenshot above the KM column is column 3. If you have hidden columns the column index might be different. Include the hidden columns when determining the column index. If you need help with this then please provide the test case Allan asked for so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • Hildeb67Hildeb67 Posts: 85Questions: 23Answers: 1

    Allan I wrote you a private email

  • Hildeb67Hildeb67 Posts: 85Questions: 23Answers: 1

    Great Kevin, thanks, it worked. Was the bottle split?

  • allanallan Posts: 64,410Questions: 1Answers: 10,635 Site admin
    Answer ✓

    Hi,

    Sorry I'm only just seeing the replies here now. Great to hear you were able to make it work :).

    Thanks for the assist Kevin!

    Allan

Sign In or Register to comment.