jeudi 13 juillet 2023

Deleting selected rows with checkboxes not working properly when deselecting some rows after selecting all

I am working on a web application that allows the user to select multiple rows using checkboxes and delete them using an AJAX call. The code works fine when I select all checkboxes and delete them, but if I select all checkboxes and then deselect some before deletion, it does not delete any of the rows It just gives a success message. The issue seems to be with the way the code is handling deselected rows after selecting all checkboxes.

function delete() {
    deleteClicked = true;

    event.preventDefault();
    $("#eMsg").html("");
    $("#sMsg").html("");

    $("#dDS").val("");

    if (!validateSelection()) {
        $("eMsg").html("Please select at least one checkbox");
        return false;
    }

    let agree = confirm("Are you sure");
    if (agree) {
        let pkS = "";
        let lpS = "";
        let cBox = document.getElementsByName("ListofChkBox");
        let rL = cBox.length;
        for (let i = 0; i < rL; i++) {
            if (cBox[i].checked) {
                pkS = pkS + ":" + document.getElementById("lItem[" + i + "].lePk").value;
                lpS = lpS + ":" + document.getElementById("lItem[" + i + "].luPk").value;
            }
        }
        $("#dDS").val(pkS);
        $("#dLS").val(lpS);

        document.getElementById("dLS").value = lpS;
        let url = `e/l`;

        let fdata = {};
        $("#lFB").serializeArray().map(function(x) {
            fdata[x.name] = x.value;
        });
        fdata.lid = $("#lPK").val().split("=")[0];
        fdata.cPk = $("#cPk").val().split("=")[0];
        fdata.lDlist = cGlobal;
        fdata.dDS = $("#dDS").val();
        fdata.dLS = $("#dLS").val();

        
        $.ajax({
            type: "DELETE",
            url: dataurl,
            data: JSON.stringify(fdata),
            contentType: 'application/json;charset=UTF-8',
            success: () => {
                populateTable();
                $("#all")[0].checked = false;

                $("#sMsg").html("Success");
            },
            error: function(jqXHR, response) {
                if (!vB(jqXHR, event)) {
                    return false;
                } else {
                    $("eMsg").html("Failed");
                }
            },
        });
        return false;
    }
}



Aucun commentaire:

Enregistrer un commentaire