FILES/CSV
π CSV λ°μ΄ν°λ€μ HTMLλ¬Έμμ ν μ΄λΈλ‘ μΆλ ₯νκΈ°
μΈν_
2021. 9. 15. 19:30
CSV νμΌμ HTML ν μ΄λΈλ‘ λ³ννκΈ°
csv νμΌμ μΉλ¬Έμμ λΆλ¬μ€κΈ° μν΄ ajax μμ²μ νλ€.
μμ μ μ¬μ©ν csv νμΌ (csv_data.csv)μ λ€μκ³Ό κ°μ΄ ꡬμ±λμ΄μλ€κ³ νμ.
$.ajax({
url: 'csv_data.csv',
dataType: 'text'
}).done(successFunction);
λ§μΌ ajax μμ²μ΄ μ±κ³΅νλ€κ³ κ°μ νλ©΄ successFunction() λ©μλκ° μ€νλλ€. μ΄ ν¨μλ λ°νλ λ°μ΄ν°λ₯Ό νμ±ν΄μ HTML ν
μ΄λΈλ‘ λ§λ€μ΄ λ³ννλ€.
function successFunction(data) {
var allRows = data.split(/\r?\n|\r/);
var table = '<table>';
for (var singleRow = 0; singleRow < allRows.length; singleRow++) {
if (singleRow === 0) {
table += '<thead>';
table += '<tr>';
} else {
table += '<tr>';
}
var rowCells = allRows[singleRow].split(',');
for (var rowCell = 0; rowCell < rowCells.length; rowCell++) {
if (singleRow === 0) {
table += '<th>';
table += rowCells[rowCell];
table += '</th>';
} else {
table += '<td>';
table += rowCells[rowCell];
table += '</td>';
}
}
if (singleRow === 0) {
table += '</tr>';
table += '</thead>';
table += '<tbody>';
} else {
table += '</tr>';
}
}
table += '</tbody>';
table += '</table>';
$('body').append(table);
}
ν μ΄λΈ μ€νμΌ μΆκ°
κΈ°λ³Έ html ν μ΄λΈμ΄ λ°λ°νλ©΄ μλμ μ€νμΌ μ½λλ₯Ό μΆκ°ν΄λ³΄μ.
table {
margin: 0 auto;
text-align: center;
border-collapse: collapse;
border: 1px solid #d4d4d4;
}
tr:nth-child(even) {
background: #d4d4d4;
}
th, td {
padding: 10px 30px;
}
th {
border-bottom: 1px solid #d4d4d4;
}
# μ°Έκ³ μλ£
https://code.tutsplus.com/ko/tutorials/parsing-a-csv-file-with-javascript--cms-25626