In Section 8, we have completed the functions of querying and deleting product categories, so it is easy to implement the functions of querying and deleting products. The principle is exactly the same as in Section 8, just modify some parameters, such as requesting different actions, etc. Since querying and deleting products does not require a new UI window to pop up, we just need to complete the corresponding part in query.jsp and the corresponding background.
1. Implementation of product query function
The query function is mainly implemented in the query box. From the previous section, we can see that the query box uses a text: "<input id='ss' name='ss' />". We implement it by converting the ordinary text box into a query search text box. Below we add the corresponding part of the code in query.jsp:
$('#ss').searchbox({ //Trigger query event searcher:function(value,name){ //Value represents the input value//Add trigger code $('#dg').datagrid('load',{//Reload, specify the parameter name as user input value name: value }); }, prompt:'Please enter the search keyword' });The test results are as follows:
Query is very simple, just like loading all products in the previous section, except that when querying, the parameters are set to the value entered by the user, and when loading all the parameters are set to empty.
2. Implementation of product deletion function
Next, we will complete the function of deleting products. First, we will complete the corresponding code in query.jsp:
{ iconCls: 'icon-remove', text:'Delete product', handler: function(){ //Add trigger code var rows = $("#dg").datagrid("getSelections"); //Defend whether there is a selected row record, use getSelections to get all selected rows//Return the selected row. If no rows are selected, return an empty array if(rows.length == 0) { //Pop up the prompt message $.messager.show({ //The syntax is similar to the static method in java. Direct object calls title:'Error prompt', msg:'at least one record must be selected', timeout:2000, showType:'slide', }); } else { // Prompt whether to confirm the deletion. If confirmed, the logic of deletion is executed $.messager.confirm('Delete Confirm dialog', 'Are you sure you want to delete this item?', function(r){ if (r){ //1. Get the corresponding id from the obtained record, splice the value of the id, and then send the background 1,2,3,4 var ids = ""; for(var i = 0; i < rows.length; i ++) { ids += rows[i].id + ","; } ids = ids.substr(0, ids.lastIndexOf(",")); //2. Send ajax request $.post("product_deleteByIds.action",{ids:ids},function(result){ if(result == "true") { //Delete the just selected record, otherwise it will affect the subsequent update operation $("#dg").datagrid("uncheckAll"); //Refresh the current page, when querying, we use load, refresh the first page, and reload is refreshing the current page $("#dg").datagrid("reload"); //Default is the queryParams above without parameters } else { $.messager.show({ title:'Delete exception', msg:'Delete failed, please check the operation', timeout:2000, showType:'slide', }); } },"text"); } }); } } } } } } } As can be seen from the above code, the deletion operation requires at least one record to be selected first. After selecting, when confirming the deletion (that is, r is true), first obtain which records have been checked by the user, spliced together the id numbers of these records, and then want to send ajax request in the background, request the deleteByIds method in productAction, and take the spliced id as a parameter. If the deletion is successful, return a string "true" to the foreground, and then the foreground will clear the just-checked record to avoid affecting the subsequent update operation, because the update also needs to check the record, then refresh the current page and reload all product information in the database.
The process is very clear. Let’s write the background program, starting with the service layer:
public interface ProductService extends BaseService<Product> { //Query product information, cascading categories public List<Product> queryJoinCategory(String type, int page, int size); //Query using the product name//Query the total number of records based on keywords public Long getCount(String type); //Delete multiple records based on ids public void deleteByIds(String ids); } @SuppressWarnings("unchecked") @Service("productService") public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService { //Omit other codes... @Override public void deleteByIds(String ids) { String hql = "delete from Product p where p.id in (" + ids + ")"; getSession().createQuery(hql).executeUpdate(); } } Next, complete the deleteByIds method in productAction:
@Controller("productAction") @Scope("prototype") public class ProductAction extends BaseAction<Product> { //Omit other codes... public String deleteByIds() { System.out.println(ids); productService.deleteByIds(ids); //If the deletion is successful, it will be executed downwards. We will pass "true" to the foreground in the form of a stream inputStream = new ByteArrayInputStream("true".getBytes()); return "stream"; } }The same idea as before for deleting product classes, the following is configured in struts.xml:
<action name="product_*" method="{1}"> <!-- Omit other configurations--> <result name="stream" type="stream"> <param name="inputName">inputStream</param> </result> </action>In this way, the string "true" will be passed to the front desk and the delete will be successful after receiving the instructions. Check out the effect:
The test was successful, and at this point, the search and deletion functions of the product have been completed.
This article address: http://blog.csdn.net/eson_15/article/details/51360804
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.