簡體   English   中英

HTML DOM表刪除/刪除選定的行/行

[英]HTML DOM table removing/deleting selected row/rows

我有以下代碼。 我試圖包括一個刪除按鈕以刪除所選的一行或多行...在代碼中包括所選內容以及刪除功能,但是我不知道如何獲取所選的行並將其傳遞給該功能以刪除它...代碼中包含的一個按鈕應該足夠了,但是如果您知道如何在每一行中都包含一個刪除按鈕“ 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>

這就是我要做的:

首先,我們在html表的每一行中添加一個帶有刪除按鈕的td

然后,我們可以將偵聽器附加到#table元素( id="table"的元素)上。 該偵聽器偵聽具有delete-table, when one is clicked, we take the button and find the closest類的#table后代的delete-table, when one is clicked, we take the button and find the closest tr`(在DOM樹上),然后將其刪除。

工作jsFiddle

 $('#table').on('click','.delete-row',function(){ $(this).closest('tr').remove(); // $(this) is the button }); 
 td {border: 1px #DDD solid; padding: 5px; cursor: pointer;} .selected { background-color: brown; color: #FFF; } 
 <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> 

這是我過去做過類似事情的一個例子...

基本上我以編程方式向每行添加一個刪除按鈕:

// 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();
})

這是工作演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM