目录
首先需要进入到编辑页面,在编辑页面需要渲染选中的想要编辑修改的数据或者全部数据到对应的input框
之所以渲染时的name属性值要加一个[],是因为在提交时这样会返回一个数组的形式
数组的长度是根据数据的条数来决定的,这样在接收到后进行转换再插入数据表即可:
在开发薪资管理时可能使用在线编辑功能一次性编辑多条数据会很方便,那么下面把实现思路以及demo分享出来:
首先需要进入到编辑页面,在编辑页面需要渲染选中的想要编辑修改的数据或者全部数据到对应的input框
/**
* 在线编辑列表页
* 我这里是获取的全部数据
* 在编辑页面中全部渲染到input
*/
public function editdata(){
if($this->request->isGet()){
$res = $this->model->alias('a')
->join('items b','a.item_id=b.id')
->join('staff c','a.staff_id=c.id')
->join('form d','b.form_id=d.id')
->field('a.*,b.name,c.staff_name,c.zjhm,d.form_name')->select();
$this->assign('res',$res);
return $this->view->fetch();
}
}
在编辑页面中进行渲染:
<div class="panel panel-default panel-intro">
{:build_heading()}
<div class="panel-body">
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade active in" id="one">
<div class="widget-body no-padding" style="overflow: scroll;">
<div id="toolbar" class="toolbar">
</div>
<table id="table" class="table table-striped table-bordered table-hover table-nowrap">
<thead><tr>
<th>ID</th>
<th>姓名</th>
<th>证件号码</th>
<th>一号</th>
<th>二号</th>
<th>三号</th>
<th>四号</th>
<th>五号</th>
<th>六号</th>
<th>七号</th>
<th>八号</th>
<th>九号</th>
<th>十号</th>
<th>十一号</th>
<th>十二号</th>
<th>十三号</th>
<th>十四号</th>
<th>十五号</th>
<th>十六号</th>
<th>十七号</th>
<th>十八号</th>
<th>十九号</th>
<th>二十号</th>
<th>二十一号</th>
<th>二十二号</th>
<th>二十三号</th>
<th>二十四号</th>
<th>二十五号</th>
<th>二十六号</th>
<th>二十七号</th>
<th>二十八号</th>
<th>二十九号</th>
<th>三十号</th>
<th>三十一号</th>
<th>项目部</th>
<th>公司</th>
</tr></thead>
<tbody>
<form action="editsub" method="post">
<button type="submit" class="btn btn-primary btn-embossed">{:__('OK')}</button>
{volist name="res" id="vo"}
<tr>
<td><input type="text" name="id[]" value="{$vo.id}" style="width: 50px;border: none;outline: none;" readonly></td>
<td><input type="text" value="{$vo.staff_name}" style="width: auto;border: none;outline: none;" readonly></td>
<td><input type="text" value="{$vo.zjhm}" style="width: auto;border: none;outline: none;" readonly></td>
<td><input type="text" name="first[]" value="{$vo.first}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="second[]" value="{$vo.second}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="third[]" value="{$vo.third}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="fourth[]" value="{$vo.fourth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="fifth[]" value="{$vo.fifth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="sixth[]" value="{$vo.sixth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="seventh[]" value="{$vo.seventh}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="eighth[]" value="{$vo.eighth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="ninth[]" value="{$vo.ninth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="tenth[]" value="{$vo.tenth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="eleventh[]" value="{$vo.eleventh}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="twelfth[]" value="{$vo.twelfth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="thirteenth[]" value="{$vo.thirteenth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="fourteenth[]" value="{$vo.fourteenth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="fifteenth[]" value="{$vo.fifteenth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="sixteenth[]" value="{$vo.sixteenth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="seventeenth[]" value="{$vo.seventeenth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="eighteenth[]" value="{$vo.eighteenth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="nineteenth[]" value="{$vo.nineteenth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="twentieth[]" value="{$vo.twentieth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="twenty_first[]" value="{$vo.twenty_first}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="twenty_second[]" value="{$vo.twenty_second}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="twenty_third[]" value="{$vo.twenty_third}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="twenty_fourth[]" value="{$vo.twenty_fourth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="twenty_fifth[]" value="{$vo.twenty_fifth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="twenty_sixth[]" value="{$vo.twenty_sixth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="twenty_seventh[]" value="{$vo.twenty_seventh}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="twenty_eighth[]" value="{$vo.twenty_eighth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="twenty_ninth[]" value="{$vo.twenty_ninth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="thirtieth[]" value="{$vo.thirtieth}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" name="thirty_first[]" value="{$vo.thirty_first}" style="width: 50px;border: none;outline: none;"></td>
<td><input type="text" value="{$vo.name}" style="width: 50px;border: none;outline: none;" readonly></td>
<td><input type="text" value="{$vo.form_name}" style="border: none;outline: none;" readonly></td>
</tr>
{/volist}
</form>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
我这里是自己写的行内样式,如果有更好的方法希望有大佬可以指教一下,在页面上是这样显示的:
之所以渲染时的name属性值要加一个[],是因为在提交时这样会返回一个数组的形式
数组的长度是根据数据的条数来决定的,这样在接收到后进行转换再插入数据表即可:
/**
* 在线编辑提交
*/
public function editsub(){
if($this->request->isPost()){
$datas = input('post.');// 获取到全部的数据
// 随便挑一个字段进行循环,将其进行转换,数组的长度是一样的
for($i = 0;$i < count($datas['id']);$i++){
$data[$i]['id'] = $datas['id'][$i];
$data[$i]['first'] = $datas['first'][$i];
$data[$i]['second'] = $datas['second'][$i];
$data[$i]['third'] = $datas['third'][$i];
$data[$i]['fourth'] = $datas['fourth'][$i];
$data[$i]['fifth'] = $datas['fifth'][$i];
$data[$i]['sixth'] = $datas['sixth'][$i];
$data[$i]['seventh'] = $datas['seventh'][$i];
$data[$i]['eighth'] = $datas['eighth'][$i];
$data[$i]['ninth'] = $datas['ninth'][$i];
$data[$i]['tenth'] = $datas['tenth'][$i];
$data[$i]['eleventh'] = $datas['eleventh'][$i];
$data[$i]['twelfth'] = $datas['twelfth'][$i];
$data[$i]['thirteenth'] = $datas['thirteenth'][$i];
$data[$i]['fourteenth'] = $datas['fourteenth'][$i];
$data[$i]['fifteenth'] = $datas['fifteenth'][$i];
$data[$i]['sixteenth'] = $datas['sixteenth'][$i];
$data[$i]['seventeenth'] = $datas['seventeenth'][$i];
$data[$i]['eighteenth'] = $datas['eighteenth'][$i];
$data[$i]['nineteenth'] = $datas['nineteenth'][$i];
$data[$i]['twentieth'] = $datas['twentieth'][$i];
$data[$i]['twenty_first'] = $datas['twenty_first'][$i];
$data[$i]['twenty_second'] = $datas['twenty_second'][$i];
$data[$i]['twenty_third'] = $datas['twenty_third'][$i];
$data[$i]['twenty_fourth'] = $datas['twenty_fourth'][$i];
$data[$i]['twenty_fifth'] = $datas['twenty_fifth'][$i];
$data[$i]['twenty_sixth'] = $datas['twenty_sixth'][$i];
$data[$i]['twenty_seventh'] = $datas['twenty_seventh'][$i];
$data[$i]['twenty_eighth'] = $datas['twenty_eighth'][$i];
$data[$i]['twenty_ninth'] = $datas['twenty_ninth'][$i];
$data[$i]['thirtieth'] = $datas['thirtieth'][$i];
$data[$i]['thirty_first'] = $datas['thirty_first'][$i];
}
try {
foreach($data as $k => $v){
$res = $this->model->where('id',$v['id'])->update($v);
if($res){//修改成功的数据
array_push($succ,$v);
}else{//修改失败的数据
array_push($erro,$v);
}
}
// 将具体的成功失败的数据放入,可以将其返回到前端
$datas = [
'success'=>$succ,
'error'=>$erro
];
$this->success('共'.count($data).'条数据,成功'.count($succ).'条数据,失败'.count($erro).'条数据','index');
} catch (\Throwable $th) {
throw $th;
}
}
}
我这里最后写了一个具体多少条数据,成功失败各多少条,看各自需求了,希望对大家有所帮助