I have tried to get the solution on line but can't find one.
I will try and explain my problem because i can't make this in fiddle because the values are from the database.
I have two tables one displays data from the database.I have a button which when I click add it copy the contents of the first table and append them to the second table.
I am able to do it.
Now the issue am stuck is that i want to be able to check if i have added a data before in the second table and update the one that was added.
My jquery code here :
$('#centerElem tr').each(function(index, items){
$(this)find('input:checked')each(function(){
$this= $(this);
var chkItemcol = $this.parent().siblings('td');
var chklen = chkItemcol,length;
var chkItemValue = chkItemcol.eq(0).text(); var chkItemPrice = chkItemcol.eq(chklen-1).text();
var sumprice=0;
createrow ="<tr class='data'><td class='itemQty'>"+count+"</td>";
// iterate through the columns.
var mlen = chklen-1;
for(var i = 0; i<chklen; i++){ // add class to the item name
if(i == 0){
createrow += "<td class='name'>";
}else{
createrow += "<td>";
}
createrow += $this.parent().siblings('td').eq(i).text();
}
createrow += "</td>";
//alert(createrow);
createrow += "<td class='subtotal'></td></tr>";
if(i == (mlen)){
sumprice = ($this.parent().siblings('td').eq(0).text()) * ($this.parent().siblings('td').eq().text(mlen));
}
createTotal = "<tr><td>Total</td><td class='totalsum'>"+sumprice+"</td></tr>";
$('.ordertable .name').each(function (index, item) {
// get the checked <td>
var chkItemcol = $this.parent().siblings('td');
// get the checked row numbers of columns
var $item = $(item);
$data = $item.text();
var olen = $($item.siblings()).length;
var itemprice;
var subTotal
if ($data == chkItemValue) {
count++;
flag = true;
//get the item price
itemprice = $($item.siblings()[olen - 2]).text();
//multiple the qty with the item price
subTotal = count * itemprice;
// set the qty
$($item.siblings()[0]).text(count);
// set the subtotal.
$($item.siblings()[olen - 1]).text(subTotal);
return count;
} else {
count = 1;
itemprice = $($item.siblings()[olen - 2]).text();
alert("first add price " + itemprice);
subTotal = count * itemprice;
$($item.siblings()[olen - 1]).text(subTotal);
flag = false;
return count;
}
});
// check if the item was added to the ordered list table
if (flag) {
count++;
} else {
count = 1;
$(createrow).appendTo($('.ordertable > tbody'));
}
});
});
here is my html part, the table that display the database values.
<table><thead><tr><td><input type="checkbox" name="checkall"></td><td>Dish Name</td><td>Discription</td><td>ingredients</td><td>type</td><td>price</td></tr></thead>
<tbody>
<tr><td><input type="checkbox" name="chk" id="chk"/></td><td class='name'>Rice </td><td>white parboiled rice</td><td>rice</td><td>none</td><td>300</td></tr>
<tr><td><input type="checkbox" name="chk" id="chk"/></td><td class='name'>Beans </td><td>parboiled beans</td><td>beans an d salt/td><td>none</td><td>400</td></tr>
</tbody>
</table>
Here is the one i am appending the copied values to :
<TABLE class="ordertable" style="width:100%; border-collapse:collapse; border: solid 1px #000000">
<TBODY>
</TBODY><TFOOT></TFOOT></TABLE>
How can i do this ?