简体   繁体   中英

How to count number of objects I create?

I can create member objects using following html coding. But for some calculations, I need to get the count of the member objects. Also when I delete one of the member object it should reduce the member objects count.

Following fuction will create the members.

function appendMember(rowNumber) {
    debugger

    var subrowNumber = parseInt($("#itemmapWrapper" + rowNumber + "").attr("data-subrowNumber"));


    subrowNumber = isNaN(subrowNumber) ? 1 : subrowNumber + 1;

    var addNewItemDetailHtml = "<div class='col-lg-10 col-md-10 col-sm-10 col-xs-10 itemmapAllocationWrapper  custom-paaddingnone form-group' \
                               id='dvaddNewItemDetailSub" + subrowNumber + "' data-subrowNumber='" + subrowNumber + "'>";

    addNewItemDetailHtml += "<div class='col-lg-4 col-md-4 col-sm-4 col-xs-4 custom-paaddingnone'>\
                                <div class='col-lg-3 col-md-3 col-sm-3 col-xs-3 custom-paaddingnone text-center'>\
                                </div>\
                                <div class='col-lg-9 col-md-9 col-sm-9 col-xs-9 custom-paaddingnone'>\
                                    <div class='col-lg-12 col-md-12 col-sm-12 col-xs-12 custom-paaddingnone'>\
                                        <div class='col-lg-10 col-md-10 col-sm-10 col-xs-10 custom-paaddingnone'>\
                                            <select class='form-control' id ='memberid"+ rowNumber + subrowNumber + "' ></select>\
                                        </div>\
                                    </div>\
                                </div>\
                             </div>";

    addNewItemDetailHtml += "<div class='col-lg-2 col-md-2 col-sm-2 col-xs-2 custom-paaddingnone'>\
                                <span class='glyphicon glyphicon-trash removeBtn' onclick = 'removeMemberDetail(this)' ></span >\
                            </div>";

    addNewItemDetailHtml += "</div>";

    addNewItemDetailHtml += "</div>";



    $("#itemmapWrapper" + rowNumber + "").attr("data-subrowNumber", subrowNumber);
    $(".memberContainer").append(addNewItemDetailHtml);
    intialize_memberDropDown(rowNumber, subrowNumber);
}

This code will take data from db to the member select menu.

function intialize_memberDropDown(rowNumber, subrowNumber) {
    $.ajax({
        type: "GET",
        url: "/Member/GetAllMember/",
        cache: false,
        success: function (data) {
            debugger
            var countryHTMLString = "<option value ='0'>Select Member</option>";
            if (data.isSucess) {
                $.each(data.data, function (index, item) {
                    countryHTMLString += "<option value ='" + item.memberid + "'>" + item.membername + "</option>";

                });
            }
            $("#memberid" + rowNumber + subrowNumber + "").html(countryHTMLString)
            calculateTotalHotailBill(rowNumber)
        }, error: function (err) {
            debugger
        }
    });
}

This final code will remove member one I click the trash button I implemented on the append member function.

function removeMemberDetail(buttonElement) {
    debugger
    $(buttonElement).parents(".itemmapAllocationWrapper").remove();

    if ($(".itemmapAllocationWrapper").html() == "") {
        $("#memberid").removeAttr("disabled");
    }
}

You can count the children of your memberContainer. If you only have one, I would give it an id instead of a classname. But it works both ways.

 console.log(document.getElementsByClassName("memberContainer")[0].children.length)
 <div class="memberContainer"> <div>child</div> <div>child</div> <div>child</div> </div>

simply add a hidden input with 0 value or whatever your initial value of objects is each time you use add member code increase it and when you use delete member decrease it your html

<input type='hidden' id='objectsCounter' value='0'>

java script increase function add this line at the end of it

var objectCounter = document.getElementById('objectsCounter').value;
objectCounter++;
document.getElementById('objectsCounter').value = objectCounter

and at the end of deleting members function add this

var objectCounter = document.getElementById('objectsCounter').value;
objectCounter--;
document.getElementById('objectsCounter').value = objectCounter

you can get the value of objects any time using

var objectCounter = document.getElementById('objectsCounter').value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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