Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
That is I have a OldFieldName(a hidden textbox taking values from ),Fieldname a textbox,isdefault a checkbox and isdefaultvalue a textbox . It is taking from datatable . When I modify fieldname , isdefault and isdefaultvalue it should modify and oldfieldname should be same as fieldname please fix this js code issue

What I have tried:

JavaScript
function Modifyproof() {
           
    var Settings = [];
    let SettingsList = $('#Tbl_Proof_Table_For_Proof').DataTable();

    $('#Tbl_Proof_Table_For_Proof tbody tr').each((index, row) => {
        let OldField_Name = SettingsList.cell(index, 0).data();
        let Field_Name = SettingsList.cell(index, 1).data();
        var IsDefault = SettingsList.cell(index, 2).data();
        let DefaultValue = SettingsList.cell(index, 3).data();

        
        Settings.push({
            Old_Field_Name: OldField_Name,
            Field_Name: Field_Name,
            IsDefault: IsDefault,
            DefaultValue: DefaultValue,
        });
    });

    var data = {
        ResultData: Settings
    };

    $.ajax({
        type: "POST",
        async: true,
        url: "/InputSettings/CreateInputSettings_Admin",
        data: JSON.stringify(data),
        contentType: 'application/json; charset=utf-8',
        dataType: "json",
        success: function (Data) {
            for (var i in Data) {
                swal(Data[i].Status);
            }
        },
        error: function (error) {
            console.log("Error:", error);
        }
    });
}
Posted
Updated 19-Dec-23 7:29am
v2
Comments
Richard MacCutchan 16-Dec-23 4:46am    
You forgot to explain what the problem is.
Dave Kreskowiak 16-Dec-23 12:03pm    
The "question" you did post makes no sense what-so-ever. Also, I agree with Richard, you never described what the problem is.

1 solution

It is not absolutely clear what you are asking, but looking at your code paints a picture of what you are wanting to do (I think).

You are not properly addressing your table rows in the .each function call.

To make better use of the JQuery functions, you might be served better to use classes to denote the columns or elements containing your data. That's what JQuery does best. It helps you target id's and classes.

This also helps with code readability and purpose and can be useful if you decide to apply css or if someone else has to read your code (ya... there's that).

If you were to do that, then you can iterate to each row, use the .find() looking for the classes that you are interested in.

It should be modified to look similar to the following where you are iterating through each row, looking for your values by class and assigning them to variables.

Hopefully I got close to the problem that you are trying to solve. Part of the puzzle that my 64 year old brain likes to solve is unraveling intent.


$('#Tbl_Proof_Table_For_Proof tbody tr').each((index, row) => {
    // The fields are input elements within the DataTable >> rows <<
    let OldField_Name = $(row).find('.old-field-name').val();
    let Field_Name = $(row).find('.field-name').val();
    var IsDefault = $(row).find('.is-default').prop('checked');
    let DefaultValue = $(row).find('.default-value').val();

    // Update OldField_Name to be the same as Field_Name
    OldField_Name = Field_Name;

    Settings.push({
        Old_Field_Name: OldField_Name,
        Field_Name: Field_Name,
        IsDefault: IsDefault,
        DefaultValue: DefaultValue,
    });
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900