ajax即点即改

/**
 * HTML5代码
*/
<table width="100%" cellspacing="1" cellpadding="2" id="list-table">
	<tbody>
				<tr>
					<th>节点名称</th>					
					<th>路由规则</th>
					<th>操作</th>
				</tr>
        	@foreach($nodes as $v)
				<tr align="center" class="0" node_id="{{$v->node_id}}">
                    <td>{{str_repeat('  ',$v->level)}}<span id="node_title" title="点击修改内容">{{$v->title}}</span></td>
                    <td><span title="点击修改内容">{{$v->route}}</span></td>
					<td width="24%" align="center">
						<a href="{{url('/admin/node/edit?node_id='.$v->node_id)}}">编辑</a> |
						<a href="{{url('/admin/node/del?node_id='.$v->node_id)}}" "return confirm('您确认要删除这条记录吗?')" title="移除">移除</a>
					</td>
				</tr>
			@endforeach
	</tbody>
</table>
/**
 * JavaScript代码
*/
<script>
	$(function () {
	    //将span标签更改为input标签
	    $(document).on("dblclick",'#node_title',function () {
            var con = $(this).text().trim();
            $(this).parent().html("<input style='text-align: center;' type=\"text\" value=" + con + " />");
            $("input").val("").focus().val(con);
        })
        //将input标签更改为span标签
        $(document).on("blur", "input", function () {
            var node_id = $(this).parents('tr').attr("node_id");
            var newCon = $(this).val();
            var obj = $(this);
            var con = $("span").text();
            $.post("{{url('/admin/node/edit')}}",{newCon,node_id,'_token':'{{csrf_token()}}'},function(res){
                if(res.code == 1){
                    if(res.parent_id==0){
                        obj.parent().html("<span id=\"node_title\" title=\"点击修改内容\">" + newCon + "</span>");
                    }else{
                        obj.parent().html("<?php echo str_repeat('  ',$v->level);?><span id=\"node_title\" title=\"点击修改内容\">" + newCon + "</span>");
                    }
                }else{
                    obj.parent().html("<?php echo str_repeat('  ',$v->level);?><span id=\"node_title\" title=\"点击修改内容\">" + con + "</span>");
                }
            })
        });
    })	
</script>
/**
 * PHP代码
*/
public function edit(Request $request)
{
    $nodes = Node::where('node_id',$request->input('node_id'))->first();
    $newCon = $request->input('newCon');

    $nodes->title = trim($newCon);
    if($nodes->save()){
        return ['code'=>1,'parent_id'=>$nodes->parent_id];
    }else{
        return ['code'=>0];
    }
}

猜你喜欢

转载自blog.csdn.net/JiaZhaoMeng/article/details/89225558