Django:想要删除带有复选框的项目

发布于 2024-10-15 03:48:54 字数 5780 浏览 1 评论 0原文

我有一个显示项目列表的模板。它的每一项都有一个复选框。我希望能够在勾选复选框时从复选框中删除项目。所以我需要一个按钮,一旦选中复选框即可删除项目。这是我的模板。对于巨大的模板表示歉意。

{% extends "base_popup.html" %}


{% block title %}
      {{title}}
{% endblock %}

{% block script %}

                <script type="text/javascript" src="{{MEDIA_URL}}ui/ui.datepicker.min.js"></script>
                <script type="text/javascript">
                        $(function(){
                                $("#id_required_date").datepicker({dateFormat:"dd/mm/yy"});
                                $(":checkbox").css("width","auto");
                        });
                        $(function(){
                                $("#check_all").click(function(){
                                        if(this.checked ==true)
                                                        $("tbody :checkbox").each(function(){
                                                                this.checked=true;
                                                        });
                                                else
                                                        $("tbody :checkbox").each(function(){
                                                                this.checked=false;
                                                        });
                                });
                        });
                </script>
                {% endblock %}
                {% block content %}
                                <div id="location_header">{{title}}</div>
                <div id="form_container">
                <form action="." method="post">
                        <fieldset class="model">

                                <p>
                                        <span style="font-weight:bold;font-size:14px">Contact : {{order.contact}}</span>
                                </p>
                                <p>
                                        <span style="font-weight:bold;font-size:14px">Cost : {{order.cost}}</span>
                                </p>
             {{ form.as_p }}
                    </fieldset>
                    <fieldset class="model">
                            <legend>Items</legend>
                            <table id="items_table">

                                    <thead>
                                            <tr>
                                                    <td><input type="checkbox" id="check_all" checked="checked"></td>
                                                    <td>Tiptop no</td><td>Client no</td><td>Title</td><td>Item type</td>
                                                    <td>Format</td>
                                            </tr>
                                    </thead>
                                    <tbody>
                            {% for item in items %}
                                    <tr>
                                    <td><input type="checkbox" name="item" value="{{item.pk}}" checked="checked"></td>
         <td>{{item.tiptop_id}}</td><td>{{item.alternative_id}}</td><td>{{item.title}}</td>
                                <td>{{item.type}}</td><td>{{item.format}}</td>           

                                </tr>
                        {% endfor %}

                        </tbody>
                        </table>
                        <p>
                                <form method="post" action="help">
                                <table width="60%">
                                        <tr>
                                                <td>
                                                        <select name="contact_id">
                                                        {% for contact in order.contact.client.contact_set.all %}
                                                                <option value="{{contact.pk}}">{{contact}}</option>
         {% endfor %}
                                                    </select>
                                            </td>
                                            <td>
                                                    <select name="status_id">
                                                    {% for status in status_list %}
                                                            <option value="{{status.pk}}">{{status}}</option>
                                                    {% endfor %}
                                                    </select>
                                            </td>
                                            <td><input type="submit" name="save_status" value="set status for selected items"></td>
                                    </tr>
                            </table>
                    </form>
                    </p>
            </fieldset>
    <div id="form_footer">
                    <span style="font-size:10px;font-weight:bold;margin-right:10px">
                    </span>
                    <input type="submit" name="save_item" value="Save" onclick="validate_item(this.form)">
            </div>
    </form>
    </div>
    <input type="button" onclick="window.location.href='{% url tiptop.views.client_items name.pk %}'" value="add_item">

    {% endblock %}

I have a template that displays the list of items. It has one checkbox to each item. I want to be able to remove an item from a checkbox when a checkbox is ticked. So I would need a button that deletes an item once a checkbox is selected. Here is my template. Apologies for the huge template.

{% extends "base_popup.html" %}


{% block title %}
      {{title}}
{% endblock %}

{% block script %}

                <script type="text/javascript" src="{{MEDIA_URL}}ui/ui.datepicker.min.js"></script>
                <script type="text/javascript">
                        $(function(){
                                $("#id_required_date").datepicker({dateFormat:"dd/mm/yy"});
                                $(":checkbox").css("width","auto");
                        });
                        $(function(){
                                $("#check_all").click(function(){
                                        if(this.checked ==true)
                                                        $("tbody :checkbox").each(function(){
                                                                this.checked=true;
                                                        });
                                                else
                                                        $("tbody :checkbox").each(function(){
                                                                this.checked=false;
                                                        });
                                });
                        });
                </script>
                {% endblock %}
                {% block content %}
                                <div id="location_header">{{title}}</div>
                <div id="form_container">
                <form action="." method="post">
                        <fieldset class="model">

                                <p>
                                        <span style="font-weight:bold;font-size:14px">Contact : {{order.contact}}</span>
                                </p>
                                <p>
                                        <span style="font-weight:bold;font-size:14px">Cost : {{order.cost}}</span>
                                </p>
             {{ form.as_p }}
                    </fieldset>
                    <fieldset class="model">
                            <legend>Items</legend>
                            <table id="items_table">

                                    <thead>
                                            <tr>
                                                    <td><input type="checkbox" id="check_all" checked="checked"></td>
                                                    <td>Tiptop no</td><td>Client no</td><td>Title</td><td>Item type</td>
                                                    <td>Format</td>
                                            </tr>
                                    </thead>
                                    <tbody>
                            {% for item in items %}
                                    <tr>
                                    <td><input type="checkbox" name="item" value="{{item.pk}}" checked="checked"></td>
         <td>{{item.tiptop_id}}</td><td>{{item.alternative_id}}</td><td>{{item.title}}</td>
                                <td>{{item.type}}</td><td>{{item.format}}</td>           

                                </tr>
                        {% endfor %}

                        </tbody>
                        </table>
                        <p>
                                <form method="post" action="help">
                                <table width="60%">
                                        <tr>
                                                <td>
                                                        <select name="contact_id">
                                                        {% for contact in order.contact.client.contact_set.all %}
                                                                <option value="{{contact.pk}}">{{contact}}</option>
         {% endfor %}
                                                    </select>
                                            </td>
                                            <td>
                                                    <select name="status_id">
                                                    {% for status in status_list %}
                                                            <option value="{{status.pk}}">{{status}}</option>
                                                    {% endfor %}
                                                    </select>
                                            </td>
                                            <td><input type="submit" name="save_status" value="set status for selected items"></td>
                                    </tr>
                            </table>
                    </form>
                    </p>
            </fieldset>
    <div id="form_footer">
                    <span style="font-size:10px;font-weight:bold;margin-right:10px">
                    </span>
                    <input type="submit" name="save_item" value="Save" onclick="validate_item(this.form)">
            </div>
    </form>
    </div>
    <input type="button" onclick="window.location.href='{% url tiptop.views.client_items name.pk %}'" value="add_item">

    {% endblock %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永不分离 2024-10-22 03:48:54

我将您的问题解释为,本质上,当单击复选框时,您希望从表中删除该行。

您需要编写一个页面,获取要删除的项目的 ID,并根据删除是否成功返回 true 或 false。我还建议您在更改复选框时不要这样做。您应该添加一个按钮来确认用户是否确实希望删除该行,然后触发此功能。

我还假设您使用的是 jQuery 1.3+。

<script type="text/javascript">
$(function ()
{
    if ($( '#items_table' ).length > 0)
    {
        var table = $( '#items_table' );

        table
            .find( 'tbody input[type=checkbox]' )
            .click(function()
            {
                // Get the ID of the item to delete
                var item_id = $(this).val();

                // Post it to the server
                $.post
                (
                    '/path/to/delete/'+item_id,
                    function(data)
                    {
                        // Assuming the page will return true
                        if( data )
                        {
                            // Remove the table row
                            $(this)
                                .closest('tr')
                                .slideUp()
                                .remove()
                            .end();
                        }
                    }
                );
            })
    }
});
</script>

I'm interpreting your question as, essentially, when a checkbox is clicked, you want to remove that row from your table.

You'll need to write a page that takes the ID of the item to be deleted, and returns true or false based on whether it's successful or not. I'd also advise you to not do this when the checkbox is changed. You should add a button that confirms whether the user really wants the row deleted or not, then trigger this function.

I'm also assuming that you are using jQuery 1.3+.

<script type="text/javascript">
$(function ()
{
    if ($( '#items_table' ).length > 0)
    {
        var table = $( '#items_table' );

        table
            .find( 'tbody input[type=checkbox]' )
            .click(function()
            {
                // Get the ID of the item to delete
                var item_id = $(this).val();

                // Post it to the server
                $.post
                (
                    '/path/to/delete/'+item_id,
                    function(data)
                    {
                        // Assuming the page will return true
                        if( data )
                        {
                            // Remove the table row
                            $(this)
                                .closest('tr')
                                .slideUp()
                                .remove()
                            .end();
                        }
                    }
                );
            })
    }
});
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文