In the table, I often need to adjust the order of the data in the table, I use Jqgrid, the principle is to save the number of rows in the table to the database, the data are sorted by row
1, move up, Move Down button
<a href="javascript:void (0)" onclick="Operatewithonerowbyid (UP) " class="linkButton"> Move up </a> <a href="javascript: void (0)" onclick="Operatewithonerowbyid (down)"class ="linkButton"> Move Down </a>
2. Move Down function
function Operatewithonerowbyid (callback) {varSelected = Tableobj.jqgrid ('Getgridparam','Selrow'); if(Selected = =NULL) {alert ("Please click to select a row with the mouse and then perform the operation!"); return; } returncallback (selected); }
3. The callback here is the merge of the up and down functions, so let's look at these two functions
function Up (selected) { if1return; Else { gridhelper.moverow ("up", Tableobj); } } function Down (selected) { gridhelper.moverow ("down", Tableobj); }
4, in this function, we have all called a function movrow () Let's take a look at this function, the principle of this function is the current selected line and I want to move to the line to exchange it.
//move a row This. Moverow =function (Movemethod, grid) {if(grid) Tableobj =Grid; varID; //if (selrow) Id=selrow; //Else id = getselrow ();ID = This. Getselrow (); Tableobj.restorerow (ID); if(id = =NULL)return; varTargetid = This. Gettargetid (ID, Movemethod)if(Targetid = =-1)return; varTemp1 =tableobj.getrowdata (ID); varTemp2 =Tableobj.getrowdata (Targetid); //Swap Line Numbers varTEMPRN =Temp1.rn; Temp1.rn=Temp2.rn; Temp2.rn=temprn; //Swap Datatableobj.setrowdata (ID, TEMP2); Tableobj.setrowdata (Targetid, TEMP1); Tableobj.setselection (Targetid); }
5, call the Gettargetid () method in 4, we will look at this method
//gets the ID of the previous line when it was moved up, or the ID of the next line when it is moved down This. Gettargetid =function (Selid, method, grid) {if(grid) Tableobj =Grid; varids =Tableobj.getdataids (); for(vari =0; i < ids.length; i++) { if(Selid = = Ids[i] && method = =" up") { if(i = =0)return-1; Else returnIds[i-1]; } if(Selid = = Ids[i] && method = =" Down") { if(i = = ids.length-1)return-1; Else returnIds[i +1]; } } }
6, increase the database field Sequence I use the nhibernate also to be modified in the configuration file, add a line <property name= "Order" column= "Sequence" ></property> Add a field order to the entity class, save the row number in the table when you save the table
Save Data Description: Save the table to save all the data, there is already in the database data, there is no data in the database, according to Idj whether or not to judge.
Public voidUpdateplan (Plantoreport plan, list<planperson>list) {nhibernate.isession session=NHibernateSessionManager.Instance.GetSession (); Try{Plantoreportservice.updateplan (plan); for(inti =0; I < list. Count; i++) {Planperson Item=List[i]; if(Item.id! =0) {Planperson itemnew=Plantoreportservice.getplanpersonbyid (item.id); Itemnew. JobName=item. JobName; Itemnew. Approvalresults=item. Approvalresults; Itemnew. Attachments=item. Attachments; Itemnew. CountryCode=item. CountryCode; Itemnew. CountryName=item. CountryName; Itemnew. Missiontype=item. Missiontype; Itemnew. Position=item. Position; Itemnew. Remark=item. Remark; Itemnew. StartDate=item. StartDate; Itemnew. Status=item. Status; Itemnew. Explain=item. Explain; Itemnew. Order=i; Plantoreportservice.addnewplanperson (itemnew); } Else{item. PlanID=plan.id; Item. Order=i; Plantoreportservice.addnewplanperson (item); }} session.Transaction.Commit (); } Catch(Exception EP) {session. Transaction.rollback (); ThrowEP; } }
7. Sort by order field when fetching data
PublicList<planpersonshowingrid> Getshowpersoninplan (intPlanID) {ISession session=NHibernateSessionManager.Instance.GetSession (); Icriteria Criteria= Session. Createcriteria (typeof(Planpersonshowingrid)); Criteria. ADD (Expression.eq ("PlanID", PlanID)). AddOrder (ORDER.ASC ("Order")); List<PlanPersonShowInGrid> list =NewList<planpersonshowingrid>(); Try{IList L=criteria. List (); List= plantoreportdao.ilisttolist<planpersonshowingrid>(l); } Catch { } returnlist; }
At this point, the data in the table is moved up and down to completion.
Jqgrid move the cell up and down