jquery easyui dataGrid Method for Dynamic Change of Sorting Field Names

  • 2021-07-26 06:08:12
  • OfStack

jQuery easyui dataGrid dynamically changes the sorting field name. Under normal circumstances, when using it, we will click on the corresponding field to sort. Here, taking JAVA as an example, the back-end entity class field may not be the same as the database field;

For example, the attribute in the entity class is userName, and the foreground filed="userName" The field of the database is user_name. At this time, if userName is set to the sorting sequence and then clicked, an exception will be thrown, because dataGrid will be sorted with the field name in filed= "userName";

Question:

How to map userName to user_name in the database

Solution:

1: Judge the sorting fields passed from the foreground in the background server, and map them to the field names in the database manually;

Advantages: Security, database fields will not be exposed in the foreground HTML page;

Disadvantages: There will be a lot of field mapping judgments in the background code;

2: Make judgment when clicking on the sorting field column in the foreground, and map it to the field name in the database through JS script in the foreground page judgment;

Advantages: Convenient, no need to modify server-side code

Disadvantages: Unsafe, the real field name of the database will be exposed in the HTML page;

Note: If the safety requirements are relatively high, it is recommended to choose the first one;

The second method is adopted here, which is implemented as follows:


 /** 
 * When you click the sort field, change the field passed into the background  
 *param Correspondence onBeforeLoad Parameters of the event  
 *map Custom field mapping Map 
 */ 
onSortColumn=function(param,map){ 
  // Take out map Mapping value of fields in  
  var fieldSort=map[param.sort]; 
  if(fieldSort!='' && fieldSort!=undefined){ 
    // Set the new sort field name. After setting, when sending the request, 1 And will be sent to the server  
    param.sort=fieldSort; 
  } 
} 

Usage:


  // Create Map 
var map = new Map(); 
// For map Add a value; key: Correspondence filed="userName" The field name in; value: Fields corresponding to the database  
map['userName']='user_name'; 
 
$('#datagrid').datagrid({ 
  onBeforeLoad:function(param){ 
    onSortColumn(param,map); 
  } 
});

Related articles: