This article mainly shares with you the implementation method of jquery easyui dataGrid dynamically changing the sorting field name. 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 is JAVA as an example. , the backend entity class fields may be inconsistent with the database fields;
For example: the attribute in the entity class is userName, the frontend filed="userName" and the database field is user_name. At this time, if userName is set to Sort the column and then click, an exception will be thrown, because the dataGrid will sort the fields with the field name in filed="userName" when sorting;
Question:
How Mapping userName and user_name in the database
Solution:
1: Judge the sorting field passed in from the frontend on the background server side, and manually map it to a field in the database Name;
advantage: security, database fields will not be exposed to the front desk HTML page;
Disadvantages: There will be many field mapping judgments in the background code;
2: Make a judgment when clicking the sort field column in the front desk, and use the JS script to judge on the front desk page and map it to the field name in the database; : Not safe. The real field names of the database will be exposed in the HTML page; The method is implemented as follows:
/** *在点击排序字段时,改变传入后台的字段 *param对应onBeforeLoad事件的参数 *map自定义的字段映射Map */ onSortColumn=function(param,map){ //取出map中字段的映射关系值 var fieldSort=map[param.sort]; if(fieldSort!='' && fieldSort!=undefined){ //设置新的排序字段名,设置完之后,发送请求时一并会发送到服务端 param.sort=fieldSort; } }
Usage method:
//创建Map var map = new Map(); //为map添加值;key:对应filed="userName"中的字段名;value:对应数据库的字段 map['userName']='user_name'; $('#datagrid').datagrid({ onBeforeLoad:function(param){ onSortColumn(param,map); } });
Related recommendations:
mybatis dynamically calls the table name and field name
php gets field name example sharing_PHP tutorial
The above is the detailed content of Implementation method of dynamically changing sorting field names in jquery easyui dataGrid. For more information, please follow other related articles on the PHP Chinese website!