<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ include file="/common/global.jsp"%>
<div class="rightinfo">
<div>
<form id="searchForm" name="searchForm" method="post">
<label>车辆状态描述:</label><input type="text" id="carStatus" name="carStatus" class="txtSearch">
<input type="button" id="carStatus" class="btn btn-info btn-round" value="查询" onclick="dataChange()">
<input type="button" class="btn btn-warning btn-round" value="重置" onclick="$('#searchForm')[0].reset();">
</form>
</div>
<div id="toolbar" class="btn-group">
<shiro:hasPermission name="Mj_car_statusManager:add">
<button class="btn btn-info btn-round" onclick="toAdd();">
<i class="glyphicon glyphicon-plus"></i>添加
</button>
</shiro:hasPermission>
<shiro:hasPermission name="Mj_car_statusManager:edit">
<button class="btn btn-warning btn-round" onclick="toEdit();">
<i class="glyphicon glyphicon-pencil"></i> 修改
</button>
</shiro:hasPermission>
<shiro:hasPermission name="Mj_car_statusManager:info">
<button class="btn btn-success btn-round" onclick="toInfo()">
<i class="glyphicon glyphicon-list-alt"></i>详情
</button>
</shiro:hasPermission>
<shiro:hasPermission name="Mj_car_statusManager:remove">
<button class="btn btn-danger btn-round" onclick="toRemove()">
<i class="glyphicon glyphicon-trash"></i>删除
</button>
</shiro:hasPermission>
</div>
<table id="Mj_car_statusManagerList" data-toggle="table"
data-url="${path}/mj_car_statusManagerController/list.do" data-pagination="true"
data-side-pagination="server" data-cache="false" data-query-params="postQueryParams"
data-page-list="[10, 15, 20, 30, 50,100]" data-method="post"
data-show-refresh="true" data-show-toggle="true"
data-show-columns="true" data-toolbar="#toolbar"
data-click-to-select="true" data-single-select="false"
data-striped="true" data-content-type="application/x-www-form-urlencoded">
<thead>
<tr>
<th data-field="" data-checkbox="true"></th>
<th data-field="carName">车辆名称概述</th>
<th data-field="carStatus" data-formatter="statusFormatters">车辆状态描述</th>
<th data-field="operator" data-formatter="operatorFormatter">操作</th>
</tr>
</thead>
</table>
</div>
我想做一个查询,但是前台是显示文字:正常,异常,,,数据库存的是0,1代号,
问题是我现在提交的文字后台数据库也提取不了啊,如何做转换
在你查询的地方,进行转换, 正常,异常,,,数据库存的是0,1代号, 转换成相应数字在查询就可以了。
//添加 function toAdd(){ window.location='${path}/mj_car_statusManagerController/toAdd.do'; } //删除 function toRemove(){ var ids=getSelectedRowsIds('Mj_car_statusManagerList'); if(ids){ top.showConfirmDiaglog('提示','删除数据不可恢复,确定要删除吗?',function(){ //关闭事件 refleshData('Mj_car_statusManagerList'); },function(){ //确认事件 $.post('${path}/mj_car_statusManagerController/deleteById.do?ids='+ids,function(data){ var json=$.parseJSON(data); if(json.success){ top.showArtDiaglog('提示','删除成功',function(){ //关闭事件 refleshData('Mj_car_statusManagerList'); },function(){ //确定事件 top.closeDialog(); }); }else{ top.showArtDiaglog('提示','删除失败',function(){ //关闭事件 },function(){ //确定事件 top.closeDialog(); }); } }); }); }else{ top.showArtDiaglog('提示','请选择一条数据进行操作',null,function(){ top.closeDialog(); }); } } //编辑 function toEdit(){ var selected=getSelectedRowsArr('Mj_car_statusManagerList'); if(selected.length>0&&selected.length<2){ window.location='${path}/mj_car_statusManagerController/editById.do?id='+selected; }else{ //提示信息 top.showArtDiaglog('提示','请选择一条数据进行操作',null,function(){ top.closeDialog(); }); } } //查看 function toInfo(){ var selected=getSelectedRowsArr('Mj_car_statusManagerList'); if(selected.length>0&&selected.length<2){ window.location='${path}/mj_car_statusManagerController/findById.do?id='+selected; }else{ top.showArtDiaglog('提示','请选择一条数据进行操作',null,function(){ top.closeDialog(); }); } } //设置查询参数 function postQueryParams(params) { var queryParams = $("#searchForm").serializeObject(); queryParams.limit=params.limit; queryParams.offset=params.offset; return queryParams; } //查询列表 function queryList(){ $('#Mj_car_statusManagerList').bootstrapTable('refresh'); } function dataChange(){ var data; var carState=$('#carStatus').val(); if(carState=="正常"){ data=1; }else{ data=0; } queryList(); } function editById(id){ window.location='${path}/mj_car_statusManagerController/editById.do?id='+id; } //根据id删除 function deleteById(id){ top.showConfirmDiaglog('提示','删除数据不可恢复,确定要删除吗?',function(){ //关闭事件 refleshData('Mj_car_statusManagerList'); },function(){ //确认事件 $.post('${path}/mj_car_statusManagerController/deleteById.do?ids='+id,function(data){ var json=$.parseJSON(data); if(json.success){ top.showArtDiaglog('提示','删除成功',function(){ //关闭事件 refleshData('Mj_car_statusManagerList'); },function(){ //确定事件 top.closeDialog(); }); }else{ top.showArtDiaglog('提示','删除失败',function(){ //关闭事件 },function(){ //确定事件 top.closeDialog(); }); } }); }); } //根据id查看 function viewById(id){ window.location='${path}/mj_car_statusManagerController/findById.do?id='+id; } //操作工具栏 function operatorFormatter(value, row, index) { var operator=""; <shiro:hasPermission name="Mj_car_statusManager:edit"> operator+='<button class="btn btn-warning btn-round btn-xs" onclick="editById(\''+row.id+'\');"><i class="glyphicon glyphicon-pencil"></i> 修改</button> '; </shiro:hasPermission> <shiro:hasPermission name="Mj_car_statusManager:info"> operator+='<button class="btn btn-success btn-round btn-xs" onclick="viewById(\''+row.id+'\')"><i class="glyphicon glyphicon-list-alt"></i>详情</button> '; </shiro:hasPermission> <shiro:hasPermission name="Mj_car_statusManager:remove"> operator+='<button class="btn btn-danger btn-round btn-xs" onclick="deleteById(\''+row.id+'\')"><i class="glyphicon glyphicon-trash"></i>删除</button>'; </shiro:hasPermission> return operator; } //状态 function statusFormatters(value,row,index){ if(value=='0'){ return '<span>异常</span>'; }else if(value=='1'){ return '<span>正常</span>'; }else{ return ""; } }前面script部分我重新贴一下
代码太乱就不看了。你可以在数据库判断下
select case 车辆状态 when 0 then '不正常' when 1 then 正常 end from 表
int X =(Integer.parse(request.geparamenter(name)))
数据库新建一张状态表,字段有id。statusName等,这样id和名称就能相对应了,查询出来,就可以显示中文啦
有两种解决方案
1 前台解决,接收 0, 1之后判断,显示相应的中文含义
2 后台解决,后台查询出结果之后,将0,1转换成相应的中文返回返回到前台,前台直接拿来用就好
我比较推荐后台处理,可以定义全局变量,方便以后的修改
用 ajax 可以将数据设为 data 传到后台,然后在后台就可以随意转换了
提供三个办法:
1、在dao层;case when else end
2、在逻辑层;对从数据库取出的值进行处理,随便一个if(xx.equals("1")) 判断
3、在前台页面foreach时,使用标签判断。
用 ajax 可以将数据设为 data 传到后台,然后在后台就可以随意转换了
var test = parseInt(“blue”); //returns NaN
var test = parseInt(“1234blue”); //returns 1234
var test = parseInt(“22.5”); //returns 22
var test = parseFloat(“1234blue”); //returns 1234
var test = parseFloat(“22.5”); //returns 22.5
如果是Oracle数据库,可以用到decode函数,若是其他的数据库,可以用select case 车辆状态 when 0 then '不正常' when 1 then 正常 end from 表
语法格式:
DECODE(value,if1,then1,if2,then2,if3,then3,…,else),表示如果value 等于if1时,DECODE函数的结果返回then1,…,如果不等于任何一个if值,则返回else。
案例:
假设我们想给智星职员加工资,其标准是:工资在8000元以下的将加20%;工资在8000元以上的加15%。
通常的做法是,先选出记录 中的工资字段值? select salary into var-salary from employee,然后对变量var-salary用if-then-else或choose case之类的流控制语句进行判断。
如果用DECODE函数,那么我们就可以把这些流控制语句省略,通过SQL语句就可以直接完成。
select decode(sign(salary - 8000),1,salary*1.15,-1,salary*1.2,salary from employee
可以使用枚举去转化代码与值