我有以下代码。 我试图包括一个删除按钮来删除选定的一行或多行...在代码中包含选择,还有删除功能,但我无法弄清楚如何获取所选行并将其传递给要删除的函数它... 代码中包含的一个按钮应该足够了,但如果您知道如何在每行中包含删除按钮“X”,那么请告诉我......
以下是示例页面: http://knightfire66.bplaced.net/testcode.php 这里有一些其他代码,但它不适用于我的代码: http://jsfiddle.net/Z22NU/12/
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test code</title>
<link rel="stylesheet" type="text/css" href="http://www.w3cschool.cc/try/jeasyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="http://www.w3cschool.cc/try/jeasyui/themes/icon.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="http://www.w3cschool.cc/try/jeasyui/jquery.easyui.min.js"></script>
<script type="text/javascript">
var pricelist = new Map({Name: 1, Preis: 2});
pricelist.set("Balloon", 25);
var ddamount = new Map({Name: 1, amount: 2});
ddamount.set("Balloon", 1);
</script>
<style type="text/css">
.products{
list-style:none;
margin-right:300px;
padding:0px;
height:100%;
}
.products li{
display:inline;
float:left;
margin:10px;
}
.item{
display:block;
text-decoration:none;
}
.item img{
border:1px solid #333;
}
.item p{
margin:0;
font-weight:bold;
text-align:center;
color:#c3c3c3;
}
.cart{
position:fixed;
right:0;
top:0;
width:300px;
height:100%;
background:#ccc;
padding:0px 10px;
}
h1{
text-align:center;
color:#555;
}
h2{
position:absolute;
font-size:16px;
left:10px;
bottom:20px;
color:#555;
}
.total{
margin:0;
text-align:right;
padding-right:20px;
}
th {
border: 1px #DDD solid;
padding: 5px;
cursor: pointer;}
.selected {
background-color: brown;
color: #FFF;
}
</style>
<!------------------------------------------------------------------------------------------------------- -->
<script>
var data = {"total":0,"rows":[]};
var totalCost = 0;
var name;
var amount;
var price;
//DragDropFunction
$(function(){
$('#cart').datagrid({
singleSelect:true //here with false i can select more then 1
});
$('.item').draggable({
revert:true,
proxy:'clone',
onStartDrag:function(){
$(this).draggable('options').cursor = 'not-allowed';
$(this).draggable('proxy').css('z-index',10);
},
onStopDrag:function(){
$(this).draggable('options').cursor='move';
}
});
$('.cart').droppable({
onDragEnter:function(e,source){
$(source).draggable('options').cursor='auto';
},
onDragLeave:function(e,source){
$(source).draggable('options').cursor='not-allowed';
},
onDrop:function(e,source){
name = $(source).find('p:eq(0)').html();
addProduct(name, parseFloat(ddamount.get(name))); //parseFloat(price.split('€')[1])
}
});
});
function addProduct(name, amount){
amount = parseFloat(amount);
price = pricelist.get(name);
price = amount*price;
function add(){
for(var i=0; i<data.total; i++){
var row = data.rows[i];
if (row.name == name){
row.quantity += amount;
row.price += price;
return;
}
}
data.total += 1;
data.rows.push({
name:name,
quantity:amount, //statt amount
price:price
});
}
add();
totalCost += price;
$('#cart').datagrid('loadData', data);
$('div.cart .total').html('Total: €'+totalCost);
}
//HERE---------------------------------------------------------------------------------------------------------
function deleteRow(r) {
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("cart").deleteRow(i); //instead of i i want the selected row/or rows.. i can select more thn one
</script>
</head>
<!-- ---------------------------------------------------------------------------------------------------- -->
<body style="margin:0;padding:0;height:100%;background:#fafafa;">
<ul class="products">
<li>
<a href="#" class="item"> <!-- class ist Pflicht -->
<div>
<img src="images/shirt1.gif" style="height="300"; width="150";"/>
</div>
<div>
<p style="font-size:14pt">Balloon</p>
<p>Price: €25</p>
<p style="font-size:12pt">Amount:</p>
</div>
</a>
<div>
<textarea type="amount" id="Balloonamount" rows="1" cols="6" style="overflow:hidden"><?php echo $amount;?></textarea>
<input align="right" type="button" value="ADD" id="btn1" /> <!-- style="width:30px" -->
<script>
document.getElementById('btn1').addEventListener('click', function(event) {
addProduct("Balloon", document.getElementById('Balloonamount').innerHTML);
});
</script>
</div>
</li>
</ul>
<!-- ---------------------------------------------------------------------------------------------------- -->
<div class="cart">
<h1>Cart</h1>
<div style="background:#fff">
<table id="cart" fitColumns="true" style="width:300px;height:auto;">
<thead>
<tr>
<th field="name" width=140>Name</th>
<th field="quantity" width=60 align="right">Quantity</th>
<th field="einheit" width=20 align="right"></th>
<th field="price" width=60 align="right">Price</th>
<th field="delete" width=20 align="right">X</th> <!--here the delete button -->
</tr>
</thead>
</table>
</div>
<div align="center">
<input type="button" value="Delete" width=auto align="center" id="deletebtn" onclick="deleteProduct()">
<input type="button" value="Reset" width=auto align="right" id="resetbtn" onclick="">
<script>
document.getElementById('resetbtn').addEventListener('click', function(event) {
if (confirm("Sure?") == true) {
$('#cart').datagrid('loadData', {"total":0,"rows":[]});
totalCost = 0;
$('div.cart .total').html('Total: €'+0);
}
});
</script>
</div>
<p class="total"><b>Total: €0</b></p>
</div>
</body>
</html>
答案 0 :(得分:0)
我就是这样做的:
首先我们在其中添加一个带有删除按钮的td
到html表中的每一行
然后我们可以将一个监听器附加到#table
元素(带有id="table"
的元素)。该侦听器侦听#table
后代的点击次数,这些后代具有类delete-table, when one is clicked, we take the button and find the closest
tr`到按钮(向上移动到DOM树),然后我们将其删除。
$('#table').on('click','.delete-row',function(){
$(this).closest('tr').remove();
// $(this) is the button
});
&#13;
td {border: 1px #DDD solid; padding: 5px; cursor: pointer;}
.selected {
background-color: brown;
color: #FFF;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table">
<tr>
<td>1 Ferrari F138</td>
<td>1 000€</td>
<td>1 200€</td>
<td>Model monopostu stajne Scuderia Ferrari pre sezónu 2013</td>
<td>1</td>
<td>F138</td>
<td>Klik pre detaily</td>
<td><button class="delete-row">Delete</button></td>
</tr>
<tr>
<td>2 Ferrari F138</td>
<td>1 000€</td>
<td>1 200€</td>
<td>Model monopostu stajne Scuderia Ferrari pre sezónu 2013</td>
<td>1</td>
<td>F138</td>
<td>Klik pre detaily</td>
<td><button class="delete-row">Delete</button></td>
</tr>
<tr>
<td>3 Ferrari F138</td>
<td>1 000€</td>
<td>1 200€</td>
<td>Model monopostu stajne Scuderia Ferrari pre sezónu 2013</td>
<td>1</td>
<td>F138</td>
<td>Klik pre detaily</td>
<td><button class="delete-row">Delete</button></td>
</tr>
</table>
&#13;
答案 1 :(得分:0)
以下是我过去做过类似事情的例子......
基本上我以编程方式为每一行添加一个删除按钮:
// ADD A DELETE BUTTON ON EACH ROW
$('td').each(function(i, val){
$('td').eq(i).prepend('<span class="deleteButton">Delete</span>')
});
然后,只要点击其中一个按钮,我们就会删除相应的(父)行:
// WHEN DELETE BUTTON IS CLICKED REMOVE THE TABLE ROW
$(document).on('mousedown', '.deleteButton', function(){
$('tr').eq($('.deleteButton').index(this)).remove();
})
heres working demo。