Using tabbing in jeditable fields in datatables

2019-09-09 04:08发布

Right now I have a datatable, some fields are editable, some are not. I have the following code (taken from tabbing between jeditable fields in a table):

$('#table .select').bind('keydown', function(evt) {
            if(evt.keyCode==9) {
                console.log("next");
                var nextBox='';
                var currentBoxIndex=$("#table .select").index(this);
                console.log("currentBoxIndex",currentBoxIndex);
                 if (currentBoxIndex == ($("#table .select").length-1)) {
                       nextBox=$("#table .select:first");         //last box, go to first
                       console.log("nextBox", nextBox);
                   } else {
                        nextBox=$("#table .select").eq(currentBoxIndex+1);    //Next box in line
                        console.log("nextBox", nextBox);
                   }
                $(this).find("#table  .select").blur();
                $(nextBox).click();  //Go to assigned next box
                return false;           //Suppress normal tab
            };
            }); 

This works great for tabbing to each editable field! Except one issue: I need to be able to tab to the field, select a value from a dropdown in the editable field, and then be able to tab. Right now I can tab through each one if I don't change the value in the field. If I change the value, the tabbing will stop and I have to re-click on the next field. Help?

I'm using:

datatables - http://datatables.net/

Bootstrap

jquery jeditable

1条回答
小情绪 Triste *
2楼-- · 2019-09-09 04:34

I found the solution.

        $('#table.select').bind('keydown', function(evt) {
                if(evt.keyCode === 9) {
                    console.log("next");
                    var nextBox = '';
                    var currentBoxIndex = $("#table.select").index(this);
                    console.log("currentBoxIndex",currentBoxIndex);
                    var showDropdown = function (element) {
                        var event;
                        event = document.createEvent('MouseEvents');
                        event.initMouseEvent('mousedown', true, true, window);
                        element.dispatchEvent(event);
                    };
                     if (currentBoxIndex === ($("#table.select").length-1)) {
                           nextBox = $("#table.select:first");         //last box, go to first   
                        showDropdown($(nextBox).get( 0 ));   
                       } else {
                            nextBox = $("#table.select").eq(currentBoxIndex+1);    //Next box in line                         
                            console.log("nextBox", nextBox);    
                            showDropdown($(nextBox).get( 0 ));   
                       }
                    $(this).find("#table.select").blur();                                                     
                    $(nextBox).click();  //Go to assigned next box                                      
                    return false;           //Suppress normal tab
                }
            }); 
查看更多
登录 后发表回答