layUI關於table編輯列支持方向鍵功能


最近使用layui的框架時,發現table插件不支持鍵盤快捷鍵切換單元格,花了點時間實現此功能。

分享給有需要的朋友們~~~

 

 

 

   /*
    *編輯數據表格,鍵盤快捷鍵方法。
    *可跳過無編輯屬性的列
    *tab 右邊一個單元格
    *shift + tab 左邊一個單元格
    *enter 下一行的單元格
    *shift + enter 上一行的單元格
    */
    //支持tab+enter 的切換
    //$(document).on('keydown keyup', '.layui-input',
    //    function (event) {
    //        var td = $(this).parent('td'),
    //            index = td.index(),
    //            tr = td.parent('tr'),
    //            isShift = $(document).data('shift'),
    //            isKeydown = (event.type == "keydown");
    //        switch (event.key) {
    //            case "Shift":
    //                $(document).data('shift', isKeydown);
    //                break;
    //            case "Tab":
    //                event.preventDefault();
    //                isKeydown && td[isShift ? 'prevAll' : 'nextAll']('[data-edit="text"]:first').click();
    //                break;
    //            case "Enter":
    //                isKeydown && tr[isShift ? 'prev' : 'next']().children('td').eq(index).click();
    //                break;
    //        }
    //    }); 
    //方向鍵的切換
    $(document).on('keydown', '.layui-table-edit', function (e) {
        var td = $(this).parent('td'),
            tr = td.parent('tr'),
            trs = tr.parent().parent().find("tr")
        tr_index = tr.index(),
            td_index = td.index(),
            td_last_index = tr.find('[data-edid="text"]:last').index(),
            td_first_index = tr.find('[data-edid="text"]:first').index();

        switch (e.keyCode) {
            case 13:
            case 39:
                td.nextAll('[data-edit="text"]:first').click();
                if (td_index == td_last_index) {
                    tr.next().find('td').eq(td_first_index).click();
                    if (tr_index == trs.length - 1) {
                        trs.eq(0).find('td').eq(td_first_index).click()
                    }
                }
                setTimeout(function () {
                    $('.last-table-edit').select()
                }, 0)
                break;
            case 37:
                td.prevAll('[data-edit="text"]:first').click();
                setTimeout(function () {
                    $('.last-table-edit').select()
                }, 0)
                break;
            case 38:
                tr.prev().find('td').eq(td_index).click();
                setTimeout(function () {
                    $('.last-table-edit').select()
                }, 0)
                break;
            case 40:
                tr.next().find('td').eq(td_index).click();
                setTimeout(function () {
                    $('.last-table-edit').select()
                }, 0)
                break;
        }
    }); 
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM