In web development, you often encounter a check box before each record in the list screen. Click and select the record to delete, modify, view and other operations.
Modification and viewing are to obtain the id value of a record and then pass it to the background for querying to obtain various attribute values of the record object, and then display it on the screen.
The key point I am talking about is to select multiple records and then delete them in batches. How to get the id value of multiple records is the key to the problem. The first is the method of selecting the checkbox all in the jsp page.
The code is as follows:
function checkEvent(name, allCheckId) { var allCk = document.getElementById(allCheckId); if (allCk.checked == true) checkAll(name); else checkAllNo(name); } //Select all function checkAll(name) { var names = document.getElementsByName(name); var len = names.length; if (len > 0) { var i = 0; for (i = 0; i < len; i++) if(!names[i].disabled){ names[i].checked = true; } } } //Don't select function checkAllNo(name) { var names = document.getElementsByName(name); var len = names.length; if (len > 0) { var i = 0; for (i = 0; i < len; i++) names[i].checked = false; } }The jsp code that calls this method:
<input name="checkAll" id ="checkAll" onclick="checkEvent('chooseFaqId','checkAll')" type="checkbox"/></td>where, chooseFaqId is the name attribute value of the check box, and checkAll is the name attribute value of the table header check box.
When the front desk executes batch js code, it needs to separate the selected record id values with commas, and delete the batch method code as follows:
function batchDeletechFaq(idStr){ var ids="'"; $("input[name='chooseFaqId']").each(function(){ if(this.checked){ ids+=this.value+"','" } }); ids+="'"; ids=ids.replace(/,''/g,''); if(ids=="''"){ jqDialog.alert("<s:text name='faqManage.pleaseSelectFaq'/>"); return; } $.ajax({ type:"POST", url:"admin/faqManageAction!batchDeleteFaq.action", data: {chooseFaqId: ids}, dataType: "text", success:function(html){ if(html=="success"){ jqDialog.alert("<s:text name='faqManage.OperationWasSuccessful'/>",function(){ window.location.href = window.location.href.replace(/#/g,''); }); } else { jqDialog.alert("<s:text name='faqManage.OperationFailedPleaseTryLater'/>"); } } }); }); }The background obtains a set of id values sent back by the front desk
final String ids = this.getRequest().getParameter("chooseFaqId");The original method of obtaining the selected group of ids is the following:
final String ids = this.getRequest().getParameterValues("chooseFaqId");This way you can get a string array directly without any editing operations. I can't remember what the reason I changed to the current method later.
Finally, the code that executes the deleted code in the background BO layer:
/** * Batch delete FAQ issue based on ID* @param faqAnswer * @return */ public void batchDeleteFaq(final String ids){ final String hql = "delete from FAQAnswer o where o.id in ("+ids+")"; faqManagerDao.execute(hql); }In summary, I use the ajax submission method. After clicking the batch delete button, first determine whether the user has selected at least the record, otherwise a prompt message will pop up. After selecting, click the Delete button to execute the background method. If the operation fails to execute, the operation failure message box pops up.
The above is the method of obtaining multiple id values after clicking the check box on the list screen. I hope it will be helpful to everyone. If you want to know more information, please pay attention to the Wulin.com website!