hrming
[JavaScript] 테이블 로우 추가/삭제(숨김)에 따른 넘버처리 본문
function fn_addRow(){
var innerHtml = “”;
innerHtml += ~~~~~
innerHtml += ~~~~~
$(‘.table’)append(innerHtml);
fn_updateRowNo();
}
function fn_deleteRow(){
$(‘.table tbody tr’).each(function(){
var checkbox = $(this).find(‘input[type=“checkbox”]’);
if(checkbox.prop(‘checked’)){
$(this).hide();
}
});
fn_updateRowNo();
}
function fn_updateRowNo(){
var indexNo = 1;
$(‘table tbody tr’).each(function(){
if($(this).is(‘:visible’)){
$(this).find(‘#rowNo’).val(rowIndex);
rowIndex++;
}
});
}
$(this).remove(); 일때는, 아래처럼 넘버 설정이 가능했는데, hide해야해서 위처럼 수정!@
function fn_updateRowNo(){
$(‘table tbody tr’).each(function(index){
$(this).find(‘#rowNo’).val(index+1);
});
}
☑️ 테이블 행 추가/삭제 시, 아래와 같이 insertRow(), insertCell(), deleteRow() 함수 사용 가능
function addRow(){
var table = document.getElementById(‘table’);
var newRow = table.insertRow();
var cell1 = newRow.insertCell();
var cell2 = newRow.insertCell();
var cell3 = newRow.insertCell();
cell1.innerHTML = ‘<input type=“checkbox”>’;
cell1.className = ‘table-cell’;
cell1.innerHTML = ‘<input type=“text”>’;
cell1.className = ‘table-cell’;
cell1.innerHTML = ‘<input type=“test”>’;
cell1.className = ‘table-cell’;
}
function deleteRow() {
var table = document.getElementByID(‘table’);
for(var i=1; i<table.row.length; i++){
if(check){
table.deleteRow(i);
i—-; // 삭제 후, 인덱스를 감소시켜 다음 행을 건너뛰지 않도록 처리
}
}
}
참고 및 출처 :
https://baessi.tistory.com/m/109
[jQuery] 테이블 tr 동적 추가 삭제하기
안녕하세요 오늘은 해당 테이블의 tr 개수를 구하는 방법에 대해 알려드리도록 하겠습니다. 우선 간단한 테이블 예제 코드입니다. column1 column2 column3 1 내용 기타 자 그리고 항목 추가, 항목을 삭
baessi.tistory.com
https://hianna.tistory.com/443
[Javascript] 테이블에 행 추가, 삭제하기 (버튼 클릭)
이번에는 버튼을 클릭하여 테이블에 행을 추가하고 삭제하는 방법을 알아보도록 하겠습니다. 1. 테이블에 행 추가하기 - insertRow(), insertCell() 2. 테이블에 행 삭제하기 - deleteRow() 1. 테이블에 행 추
hianna.tistory.com
'JavaScript' 카테고리의 다른 글
[JavaScript] getElementById vs querySelector (1) | 2024.12.25 |
---|---|
[JavaScript] 이중 for문 한번에 탈출 (0) | 2024.12.17 |
[JavaScript] 팝업창 최상단 띄우기 (0) | 2024.10.31 |
[JavaScript] Optional chaining (?.) (0) | 2024.07.30 |
[JavaScript] Object.values() (0) | 2024.07.30 |
Comments