30/09/2018, 18:56

sửa Code trong javascript

Mọi người ơi cho mình hỏi. Mình đánh số trong bảng hóa đơn này. Mình đã đánh số tự động tăng được rồi. Thế nhưng khi xóa 1 dòng thì mình muốn nó tự động đánh lại thì phải làm sao mọi người.
Biến stt là index

(function() {

var index = 0;

$('.btn-choose').on('click', choose);
$('#bill-table').on('change keyup paste', '.bill-quantity', changeValue);

function choose() {
	var self = this,
		bill = $('#bill-table'),
		data = getDataMenu($(self).parent().parent()),
		html = createRow(data);

	if (bill.find('.empty').length) {
		bill.empty();
	}

	bill.append(html);
	total();
}

function changeValue() {
	var self = this,
		row = $(self).parent().parent(),
		data = getDataBill(row);
	
	row.find('.bill-total').html(calculate(data.price, data.quantity));
	total();
}

function getDataMenu(row) {
	var id = $(row.find('.col-id')).html(),
		name = $(row.find('.col-name')).html(),
		price = $(row.find('.col-price')).html();
	
	var data = {
		id: id,
		name: name,
		price: price

	}
	index += 1;
	total();
	return data;
}

function getDataBill(row) {
	var price = $(row.find('.bill-price')).html(),
		quantity = $(row.find('.bill-quantity')).val();
		amt =  $(row.find('.bill-total')).val();
		
	var data = {
		price: price,
		quantity: quantity,
	
	}
	index += 1;
	total();
	return data;
}

function createRow(data) {
	var html = '<tr>';

	html += '<td>' + index + '</td>';
	html += '<td><input type="text" name="col-id[]" size="3" readonly="readonly" value="' + data.id + '" /></td>';
	html += '<td name="col-name[]">' + data.name + '</td>';
	html += '<td class="bill-price" name = "bill-price[]" >' + data.price + '</td>';
	html += '<td><input type="text" value="1" class="bill-quantity" name="bill-quantity[]" size = "3"/></td>';
	html += '<td class="bill-total" name= "bill-total[]" >' + calculate(data.price, 1) + '</td>';
	html += '<td><input type="submit" value="Delete" class="delete btnthem" name="delete"/></td>';
	
	html += '</tr>';
	

	return html;
}

function calculate(price, quantity) {
	return price * quantity;
}



})();

</script>
Hieu Huynh viết 21:06 ngày 30/09/2018

Mình không có khiếu diễn đạt lắm, bạn xem code chắc hiểu ^^.
http://jsfiddle.net/mysunday20/kcrn16n9/1/

Bài liên quan
0