Я хотел бы сделать небольшую функцию, которая переместила бы 2 ячейки таблицы с помощью jQuery (или PHP/Ajax). Скажем, у меня есть следующая таблица:Как переставить 2 ячейки таблицы
<table class="table">
<tr>
<td class="btn" data-cellN="1">
01
</td>
<td class="btn" data-cellN="2">
02
</td>
</tr>
<tr>
<td class="btn" data-cellN="3">
05
</td>
<td class="btn" data-cellN="4">
06
</td>
</tr>
</table>
Как выбрать 2 клетки и переставить их?
EDIT:
Я сделал свой путь через функцию, благодаря вашим советам, тем не менее. Он работает в первый раз, когда у меня возникла странная ошибка, возникающая из переменных c1 c2 c3, я не знаю почему, может ли кто-нибудь помочь мне закончить мою функцию, пожалуйста?
Вот мой Javascript: (он обернут в документ готов)
var nbbuttonclicked=0; // to trigger the movecell function after the second button was clicked
var count=0; //Number of tinme we moved a cell
var table = {
c1:null,
c2:null,
c3:null,
check: function(){
$('td').on('click', function(){ //When the user click on a button we verify if he already clicked on this button
if($(this).hasClass('btn-info')) {
//If yes, then remove the class and variable c1
$(this).removeClass('btn-info');
table.c1=0;
nbbuttonclicked--;
}else{
//if no, then add a class to show him it is clicked and remember the value of the cell
$(this).addClass('btn-info');
if(typeof table.c1 === 'undefined' || table.c1===null) {
table.c1 = $(this);
console.log('c1:'+table.c1.html());
}else{
table.c2 = $(this);
console.log('c2:'+table.c2.html());
}
nbbuttonclicked++;
if(nbbuttonclicked==2) {
table.movecell(table.c1,table.c2); // we trigger the function to permute the values
}
}
});
},
movecell: function(c1, c2){
//We reset that variable for the next move
nbbuttonclicked=0;
//we move the cells
table.c3=c1.html();
c1.html(c2.html());
c2.html(table.c3)
c1.removeClass('btn-info');
c2.removeClass('btn-info');
table.c1=table.c2=table.c3=c1=c2=null;
},
// movecell: function(c1, c2){
// We check if the cells.html() are = to data-cellN. If yes, you won
// foreach()
// },
// var row = $('table tr:nth-child(1)')[0];
// moveCell(row, 0, 3);
};
table.check();
и вот пример: http://jsbin.com/exesof/2/edit
You означает, что когда вы меняете значения таблиц, а также влияет на db? – Ranjith
Я думаю, что он означает объединить их, но я не уверен xD – aleation
Ваша разметка HTML недействительна. – Eli