简体   繁体   English

传递来自REST API的AJAX PUT请求的仅编辑的表单字段

[英]Pass onlyedited form fields for AJAX PUT request from REST API

I have a form with several fields in it, on Save button click it will a trigger an AJAX request to update my data. 我有一个包含多个字段的表单,在“ Save按钮上单击它会触发AJAX请求以更新我的数据。 Currently its only working if I fill all of the fields in the form for update because I'm passing the $('#field').val() , but what if I want to only update one of the field? 当前,仅当我通过传递$('#field').val()填写表单中的所有字段以进行更新时,它才有效,但是如果我只想更新一个字段,该怎么办? How can I do it without requiring the rest of the field because I'm using this for multiple editing based on which row(s) of data I've selected to update 我如何做到这一点而不需要其余字段,因为我正在根据选择的数据行对它进行多次编辑

below is my code : 下面是我的代码:

HTML : HTML:

    <form id="create_project" method="POST" enctype="multipart/form-data">{% csrf_token %}
        <div>
            <label>Priority:</label>
                <select id="priority_field">
                    <option selected="selected">Select Your Priority</option>
                    <option value="Low">Low</option>
                    <option value="Normal">Normal</option>
                    <option value="Medium">Medium</option>
                    <option value="High">High</option>
                    <option value="Critical">Critical</option>
                </select><br>
            <label>Assign To: </label>
                <select id="assign_to_field" onchange="filter_by_users();">
                    <option selected="selected">Select Your User</option>
                    <option value="None">None</option>
                    {% for user in selected_project.user.all %}
                        <option value="{{ user }}">{{ user }}</option>
                    {% endfor %}
                </select>
            <br>
            <label> Start Date:</label>
            <input type="date" class="form-control" id="start_date_field"><br>
            <label>Duration:</label>
            <input type="number" class="form-control" id="duration_field"><br>
        </div>
        <div class="modal-footer modal_styling">
            <button type="button" class="btn btn-primary" onclick="editForm()">Edit
            </button>
            <button type="button" class="btn btn-default" data-dismiss="modal">Cancel
            </button>
        </div>

    </form>

javascript : javascript:

// TASK MULTI EDIT
function editForm(){
    var selected_task = gantt.getSelectedTasks();
    for(var i = 0; i < selected_task.length; i++){
        var task = selected_task[i];
        var data = gantt.getTask(task);
        $.ajax({
                type: 'PUT',
                url: '/api/dashboard/workorder_detail/' + data.workorder_id + '/',
                data: { "id" : data.workorder_id,
                        "priority": $('#priority_field').val(),
                        "assign_to": $('#assign_to_field').val(),
                        "start_date": $('#start_date_field').val(),
                        "duration": $('#duration_field').val(),
                    },
                success: function () {
                    location.reload();
                },
                error: function (err) {
                alert("Failed");
                }
        })
    }
}

Any help is much appreciated, thanks. 非常感谢任何帮助,谢谢。

First You should handle it which fields is changed then you can select for sending as parameter on ajax request. 首先,您应该处理更改了哪些字段,然后可以选择将其作为参数发送给ajax请求。 This is listener for which field has changed and add class name of 'has-changed' 这是监听器,其字段已更改,并添加类名称“已更改”

function changeEvent() {
    if(!$(this).hasClass('has-changed'))
        $(this).addClass('has-changed');
}

This is set event for inputs 这是输入的设置事件

$('#create_project input,select').on('change',changeEvent);

Then you can select changed fields with below code 然后,您可以使用以下代码选择更改的字段

var postData = {};
    $('#create_project .has-changed').each(function (i,e) {
        var id = $(e).attr('id');
        var value = $(e).val();
        postData[id]=value;

    });

after then you add the postData in your ajax 'data' properties like this 之后,您可以像这样在ajax的“数据”属性中添加postData

$.ajax({
            type: 'PUT',
            url: '/api/dashboard/workorder_detail/' + data.workorder_id + '/',
            data: postData,
            success: function () {
                location.reload();
            },
            error: function (err) {
                alert("Failed");
            }
        })

EDIT : you should use below code before "editForm" function because this code didn't read from browser before click edit button, you can see it works after click and change any form item and click second time edit button in your code. 编辑:您应在“ editForm”功能之前使用以下代码,因为单击“编辑”按钮之前未从浏览器读取此代码,单击并更改任何表单项,然后单击代码中的第二次编辑按钮,您可以看到它起作用。

function changeEvent() {
        if(!$(this).hasClass('has-changed'))
            $(this).addClass('has-changed');
    }

$('#edit_form input,select').on('change',changeEvent);

// TASK MULTI EDIT
function editForm(){
    var selected_task = gantt.getSelectedTasks();



    var postData = {};
    $('#edit_form .has-changed').each(function (i,e) {
        var id = $(e).attr('id');
        var value = $(e).val();
        postData[id]=value;

    });
    for(var i = 0; i < selected_task.length; i++){
        var task = selected_task[i];
        var data = gantt.getTask(task);
        $.ajax({
                type: 'PUT',
                url: '/api/dashboard/workorder_detail/' + data.workorder_id + '/',
                data: postData,
                success: function () {
                   // location.reload();
                   console.log(postData);
                },
                error: function (err) {
                alert("Failed");
                }
        })
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM