"Add Column Left"と "Add Column Right"という2つのボタンがあります。私はクリックされたセルのインデックスを取得する必要がありますので、私は列インデックスを取得し、新しい列を前のインデックスに追加できます。Jquery - 前の列に左または右の列を追加
私の現在のコード:
function AddColumn(addLeft){ // Which button is clicked? Add Left || Add Right
var header = $('#tableHeader'); // Get the table header element
var headerContentToAppend = "<th>Header</th>"; // The default text of the new header
var table = $('#tableBody'); // Get the table body element
var rowContentToAppend = "<td>Content</td>"; // The default text of the new cells
// issue 1: missing index of the clicked/focussed column
if (addLeft) { // Button "Add Column Left" clicked
header.prepend(headerContentToAppend); // Add the Column to the left of the previous column
}
else { // Button "Add Column Right" clicked
header.append(headerContentToAppend); // Add the Column to the right of the previous column
}
$('table th').last().attr("contenteditable", true).focus(); // set all cells editable and focus the last one
table.find("tr").each(function() { // Get all existing rows ....
// issue 2: maybe the cells don't match to the right column, I have to test this after finishing the first issue
$(this).append(rowContentToAppend); // ... and add cells to the new column
});
}
だから私の問題は、クリックされた列の右のインデックスを取得することです。新しい列を追加した後、私はそれを新しいセルで埋める必要があります。
「issue」という2つのコメントをコードに挿入しました。これは明確にする必要があります。
私のHTMLコード:
<table id="dataTable">
<thead id="tableHeader">
</thead>
<tbody id="tableBody">
</tbody>
</table>
私は、動的にそれを維持するためのJavascriptでテーブルを制御したいです。
あなたはjQueryので 'index'機能があります気づきましたか? –
いいえ、ヒントありがとう – peterHasemann