在一页中添加和编辑表单 [英] Add and edit form in one page

查看:44
本文介绍了在一页中添加和编辑表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有两个按钮添加"和编辑".当我尝试单击添加"按钮时,我得到一个弹出表单,然后填写详细信息,并将其添加到数据库中.当我单击编辑"按钮时,同样的事情应该显示相同的表格,并填写详细信息.我知道如何从后端获取数据.但是我不知道如何区分一种形式的添加和编辑.

https://jqueryui.com/dialog/#modal-form

我已经引用了此链接,并添加了添加表单的详细信息. 有人可以帮助我如何进行编辑吗?

<html>

        <input class="btn btn-info" type="button" id="create-user" value="Add user">


        <div class="row-fluid top-space-20">
            {% if result | length == 0 %}
            <div>
                <p>There are no user details ,If you want you can add it </p>
            </div>
            {% else %}
            <table class="table table-striped">
                <thead>
                    <th>user name</th>
                    <th>user duration</th>
                    <th>user Description</th>
                    <th>user requirements</th>
                    <th>Actions</th>
                </thead>
                {% for each_item in result %}
                <tbody>
                    <td>{{each_item.user_name}}</td>
                    <td>{{each_item.user_time}}</td>
                    <td>{{each_item.user_description}}</td>
                    <td>{{each_item.user_requirement}}</td>
                    <td>
                        <input class="btn btn-info" type="button" id="edituser" value="Edit">

                    </td>
                </tbody>
                {% endfor %}
                {% endif %}
            </table>
        </div>
    </div>
    <div id="dialog-form" title="Create new user">
        <p class="validateTips">All form fields are required.</p>

        <form>
            <fieldset>
                <label for="username">user name</label>
                <input type="text" name="username" id="username" class="text ui-widget-content ui-corner-all">
                <label for="duration">Duration</label>
                <input type="text" name="duration" id="duration" class="text ui-widget-content ui-corner-all">
                <label for="description">Description</label>
                <input type="text" name="description" id="description" class="text ui-widget-content ui-corner-all">
                <label for="requirements">Requirements</label>
                <input type="requirements" name="requirements" id="requirements"
                    class="text ui-widget-content ui-corner-all">
                <input type="hidden" id='departmentId' ,value="{{department_id}}">
                <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
            </fieldset>
        </form>
    </div>

    <script>
        $(function () {
            var dialog, form,
                username = $("#username"),
                duration = $("#duration"),
                description = $("#description"),
                requirements = $("#requirements"),
                allFields = $([]).add(username).add(duration).add(description).add(requirements),
                tips = $(".validateTips");

            function updateTips(t) {
                console.log(t);
                tips
                    .text(t)
                    .addClass("ui-state-highlight");
                setTimeout(function () {
                    tips.removeClass("ui-state-highlight", 1500);
                }, 500);
            }

            function checkLength(o, n, min, max) {

                if (o.val().length > max || o.val().length < min) {
                    o.addClass("ui-state-error");
                    updateTips("Length of " + n + " must be between " +
                        min + " and " + max + ".");

                    return false;
                } else {
                    return true;
                }
            }

            function addUser() {
                var valid = true;
                allFields.removeClass("ui-state-error");
                var username = $("#username");
                var duration = $("#duration");
                var description = $("#description");
                var requirements = $("#requirements");
                var departmentId = document.getElementById("departmentId").value;
                valid = valid && checkLength(username, "username", 3, 16);
                valid = valid && checkLength(duration, "duration", 3, 16);
                valid = valid && checkLength(description, "description", 3, 300);
                valid = valid && checkLength(requirements, "requirments", 5, 300);

                if (valid) {
                    var username = $("#username").val();
                    var duration = $("#duration").val();
                    var description = $("#description").val();
                    var requirements = $("#requirements").val();
                    var departmentId = document.getElementById("departmentId").value;
                    $("#users tbody").append("<tr>" +
                        "<td>" + username + "</td>" +
                        "<td>" + duration + "</td>" +
                        "<td>" + description + "</td>" +
                        "<td>" + requirements + "</td>" +
                        "</tr>");
                    $.ajax({
                        type: 'POST',
                        url: '/department/%d/user' % departmentId,
                        data: {
                            'username': username,
                            'duration': duration,
                            'description': description,
                            'requirements': requirements
                        },
                        success: function (result) {
                            console.log(result);
                            alert("The user has been added");
                            document.location.href = "/departments";
                        },
                    })

                    dialog.dialog("close");
                }
                return valid;
            }

            dialog = $("#dialog-form").dialog({
                autoOpen: false,
                height: 400,
                width: 350,
                modal: true,
                buttons: {
                    "Create user": addUser,
                    Cancel: function () {
                        dialog.dialog("close");
                    }
                },
                close: function () {
                    form[0].reset();
                    allFields.removeClass("ui-state-error");
                }
            });

            form = dialog.find("form").on("submit", function (event) {
                event.preventDefault();
                addUser();


            });

            $("#create-user").button().on("click", function () {
                console.log("I am first");
                dialog.dialog("open");
            });
        });
    </script>


</body>


</html>

解决方案

有多种方法可以完成,下次请发布代码.让我假设您使用的是 查看模型

https://jqueryui.com/dialog/#modal-form .

有关发布代码的更多详细信息,我们可以为您提供帮助

这是更新的答案

<tbody>
                    <td>{{each_item.user_name}}</td>
                    <td>{{each_item.user_time}}</td>
                    <td>{{each_item.user_description}}</td>
                    <td>{{each_item.user_requirement}}</td>
                    <td>
                        <input class="edituser btn btn-info" type="button" value="Edit" data-user-id = "{{each_item.user_id}}">

                    </td>
                </tbody>

将ID更改为class

       $(".edituser").button().on("click", function () {
           var id = $(this).attr("data-user-id");
           var tempUser;
           for(var user in results){
               if(user.id == id){
                     tempUser = user;
                }
           }
             $("#username").val(tempUser.user_name);
             $("#duration").val(tempUser.user_name);;

            dialog.dialog("open");
        });

您可以使用用户ID"来相应地设置值

并在提交时更改用于构造视图的结果"对象中的值

I have two buttons 'Add' and 'Edit' in my page. When I try to click the 'Add' button I get one popup form and I fill the details and will be added in to the database. The same thing when I click the 'Edit' button the same form should be displayed with the filled in details. I know how to get the data from the backend. But I am not aware of how should I differentiate the add and edit in one form.

https://jqueryui.com/dialog/#modal-form

I have refered this link and I have added the details for add form. Can some one help me how do I do the edit?

<html>

        <input class="btn btn-info" type="button" id="create-user" value="Add user">


        <div class="row-fluid top-space-20">
            {% if result | length == 0 %}
            <div>
                <p>There are no user details ,If you want you can add it </p>
            </div>
            {% else %}
            <table class="table table-striped">
                <thead>
                    <th>user name</th>
                    <th>user duration</th>
                    <th>user Description</th>
                    <th>user requirements</th>
                    <th>Actions</th>
                </thead>
                {% for each_item in result %}
                <tbody>
                    <td>{{each_item.user_name}}</td>
                    <td>{{each_item.user_time}}</td>
                    <td>{{each_item.user_description}}</td>
                    <td>{{each_item.user_requirement}}</td>
                    <td>
                        <input class="btn btn-info" type="button" id="edituser" value="Edit">

                    </td>
                </tbody>
                {% endfor %}
                {% endif %}
            </table>
        </div>
    </div>
    <div id="dialog-form" title="Create new user">
        <p class="validateTips">All form fields are required.</p>

        <form>
            <fieldset>
                <label for="username">user name</label>
                <input type="text" name="username" id="username" class="text ui-widget-content ui-corner-all">
                <label for="duration">Duration</label>
                <input type="text" name="duration" id="duration" class="text ui-widget-content ui-corner-all">
                <label for="description">Description</label>
                <input type="text" name="description" id="description" class="text ui-widget-content ui-corner-all">
                <label for="requirements">Requirements</label>
                <input type="requirements" name="requirements" id="requirements"
                    class="text ui-widget-content ui-corner-all">
                <input type="hidden" id='departmentId' ,value="{{department_id}}">
                <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
            </fieldset>
        </form>
    </div>

    <script>
        $(function () {
            var dialog, form,
                username = $("#username"),
                duration = $("#duration"),
                description = $("#description"),
                requirements = $("#requirements"),
                allFields = $([]).add(username).add(duration).add(description).add(requirements),
                tips = $(".validateTips");

            function updateTips(t) {
                console.log(t);
                tips
                    .text(t)
                    .addClass("ui-state-highlight");
                setTimeout(function () {
                    tips.removeClass("ui-state-highlight", 1500);
                }, 500);
            }

            function checkLength(o, n, min, max) {

                if (o.val().length > max || o.val().length < min) {
                    o.addClass("ui-state-error");
                    updateTips("Length of " + n + " must be between " +
                        min + " and " + max + ".");

                    return false;
                } else {
                    return true;
                }
            }

            function addUser() {
                var valid = true;
                allFields.removeClass("ui-state-error");
                var username = $("#username");
                var duration = $("#duration");
                var description = $("#description");
                var requirements = $("#requirements");
                var departmentId = document.getElementById("departmentId").value;
                valid = valid && checkLength(username, "username", 3, 16);
                valid = valid && checkLength(duration, "duration", 3, 16);
                valid = valid && checkLength(description, "description", 3, 300);
                valid = valid && checkLength(requirements, "requirments", 5, 300);

                if (valid) {
                    var username = $("#username").val();
                    var duration = $("#duration").val();
                    var description = $("#description").val();
                    var requirements = $("#requirements").val();
                    var departmentId = document.getElementById("departmentId").value;
                    $("#users tbody").append("<tr>" +
                        "<td>" + username + "</td>" +
                        "<td>" + duration + "</td>" +
                        "<td>" + description + "</td>" +
                        "<td>" + requirements + "</td>" +
                        "</tr>");
                    $.ajax({
                        type: 'POST',
                        url: '/department/%d/user' % departmentId,
                        data: {
                            'username': username,
                            'duration': duration,
                            'description': description,
                            'requirements': requirements
                        },
                        success: function (result) {
                            console.log(result);
                            alert("The user has been added");
                            document.location.href = "/departments";
                        },
                    })

                    dialog.dialog("close");
                }
                return valid;
            }

            dialog = $("#dialog-form").dialog({
                autoOpen: false,
                height: 400,
                width: 350,
                modal: true,
                buttons: {
                    "Create user": addUser,
                    Cancel: function () {
                        dialog.dialog("close");
                    }
                },
                close: function () {
                    form[0].reset();
                    allFields.removeClass("ui-state-error");
                }
            });

            form = dialog.find("form").on("submit", function (event) {
                event.preventDefault();
                addUser();


            });

            $("#create-user").button().on("click", function () {
                console.log("I am first");
                dialog.dialog("open");
            });
        });
    </script>


</body>


</html>

解决方案

There are Multiple Way to do it, Next time Please Post the code. Let me assume that you were using the Model to view

https://jqueryui.com/dialog/#modal-form.

For more details post the code we can help you out

Here is the updated Answer

<tbody>
                    <td>{{each_item.user_name}}</td>
                    <td>{{each_item.user_time}}</td>
                    <td>{{each_item.user_description}}</td>
                    <td>{{each_item.user_requirement}}</td>
                    <td>
                        <input class="edituser btn btn-info" type="button" value="Edit" data-user-id = "{{each_item.user_id}}">

                    </td>
                </tbody>

change the id to class

       $(".edituser").button().on("click", function () {
           var id = $(this).attr("data-user-id");
           var tempUser;
           for(var user in results){
               if(user.id == id){
                     tempUser = user;
                }
           }
             $("#username").val(tempUser.user_name);
             $("#duration").val(tempUser.user_name);;

            dialog.dialog("open");
        });

you can set the value accordingly from using "user id"

and on submit change the value in the "results" object you using to construct the view

这篇关于在一页中添加和编辑表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆