我有這個表格結構,它使用箭頭鍵導航td
中的div。當頁面加載重新,但是當更多的tr
被添加到使用jQuery append
或insetAfter
箭頭鍵導航表新tr
td
細胞箭頭鍵導航將不會移動到表中的附加TR
你可以看到my live example here
下面是導航工作正常表的HTML結構:
<table id="product_table_body" class="table" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr tabindex="0" id="1">
<td><div class="product_id cells ">1</div></td>
<td><div class="product_name cells ">STREET</div></td>
<td><div class="product_prices product_cost_price cells ">1.00</div></td>
<td><div class="product_warehouse cells ">TAFO-WAREHOUSE</div></td>
<td><div class="product_quantity cells ">2</div></td>
<td><div class="product_prices product_retail_price cells ">4.00</div></td>
<td><div class="product_prices product_whole_sale_price cells ">0.00</div></td>
<td><div class="product_prices product_credit_price cells ">0.00</div></td>
</tr>
</tbody>
</table>
的Jquery
var o = {
38: 'up',
40: 'bottom',
37: 'prev',
39: 'next'
}
var $cells = $('.cells');
var colcount = $("#product_table_body > tbody").find("> tr:first > td").length;
//$(window).keydown(function (key) {
$(document).on('keydown', '.product_table_body', function (key) {
var direction = o[key.which];
var $active = $('.active'), $editing = $('.editing'), i=$cells.index($active);
if(!$active.length && !$editing.length && direction === 'bottom')
{
key.preventDefault();
$cells.first().addClass('active').focus();
}
else
{
if(direction === 'next')
{
$active.removeClass('active').parent('td').next('td').find($cells).first().addClass('active').focus();
}
else if(direction === 'prev')
{
//if(!$editing.length)
$active.removeClass('active').parent('td').prev('td').find($cells).first().addClass('active').focus();
}
else if(direction === 'up' || direction === 'bottom')
{
key.preventDefault();
var p = direction === 'up' ? (i - colcount) : (i + colcount);
if(!$editing.length)
{
var activeRow = $cells.removeClass('active').eq(p).addClass('active').focus();
var RowID = activeRow.closest('tr').attr('id');
$('.table tbody tr#'+RowID).focus();
}
}
}
});
感謝
@abhitalks在將記錄插入到數據庫後,新行將從'Ajax'響應追加。你可能想用你的螢火蟲來檢查它。 thkk – user1862764