javascript - How to remove duplicate row ids in array -
hi need array, when add item array need remove first otherwise becomes duplicate, have looked on site @ similar questions cant find right approach, have function adds item array if item selected in list seen below.
var updateidsofselectedrows = function (id) { var index = $.inarray(id, idsofselectedrows); var rowdata = $("#unlinkedsamplestable").getrowdata(id); var selrowids = $("#unlinkedsamplestable").jqgrid("getgridparam", "selarrrow"); if ($.inarray(id, selrowids) >= 0) { idsofselectedrows.push(id); // add id statusesofselectedrows.push(rowdata.statusenum); // add status sampleidsofselectedrows.push(rowdata.sampleid); // ad sample id } else { //else remove idsofselectedrows.splice(index, 1); // remove id list statusesofselectedrows.splice(index, 1); // remove status list sampleidsofselectedrows.splice(index, 1); // remove sampleid list } };
the array built using selected rows in jqgrid, call update function when user selects row onselectrow , when user edits cell on aftersavecell shown below.
onselectrow: function(rowid){ updateidsofselectedrows(rowid); } aftersavecell: function (rowid, cellname, value, irow, icol) { highlight(rowid, 'sampleid'); if($.inarray(rowid, idsofselectedrows) == 0) updateidsofselectedrows(rowid); }
so question how can remove current item array before add new item stop duplicates. thank issue.
i figured out, use inarray(id, idsofselectedrows)
check if item exists in list shown below.
var updateidsofselectedrows = function (id) { var index = $.inarray(id, idsofselectedrows); var rowdata = $("#unlinkedsamplestable").getrowdata(id); var selrowids = $("#unlinkedsamplestable").jqgrid("getgridparam", "selarrrow"); // check row selected if ($.inarray(id, selrowids) >= 0) { //check row not exist in array , if remove - necessery avoid duplicates if($.inarray(id, idsofselectedrows) > -1) { idsofselectedrows.splice(index, 1); // remove id list statusesofselectedrows.splice(index, 1); // remove status list sampleidsofselectedrows.splice(index, 1); // remove sampleid list } idsofselectedrows.push(id); // add id statusesofselectedrows.push(rowdata.statusenum); sampleidsofselectedrows.push(rowdata.sampleid); } else { //else remove idsofselectedrows.splice(index, 1); // remove id list statusesofselectedrows.splice(index, 1); // remove status list sampleidsofselectedrows.splice(index, 1); // remove sampleid list }
inarray returns index of element found if index more -1 element exists , can removed, -1 indicates element not found.
Comments
Post a Comment