前端页面查询之前
前端页面点击查询之后
前端代码,目的是查询的多条数据填充到tbody标签内
<button class="btn btn-success" id="bt_pod">查询pod</button>
<span style="color: red" >{{ msglist }}</span>
<button class="btn btn-success" id="bt_service">查询service</button>
<span style="color: red" >{{ msglist }}</span>
<div class="table-responsive" id="pod_div" style="display:none;">
<table id="basicExampl" class="table no-margin">
<thead>
<tr>
<th>名称</th>
<th>命名空间</th>
<th>状态</th>
<th>Podip</th>
<th>主机节点</th>
<th>重启次数</th>
<th>启动时间</th>
</tr>
</thead>
<tbody id="pod_tbody">
</tbody>
</table>
</div>
<script type="text/javascript">
jQuery(function($) {
$("#bt_pod").click(function(){
$("#pod_div").show();
$("#service_div").hide();
$.ajax({
url:"/yw/k8s_list/",
traditional:true,
type:"POST",
success:function (data) {
$('#pod_tbody').empty(); //清空标签内数据
$(data).each(function (i, v) { //添加标签内新的数据
$('#pod_tbody').append('<tr><td style="width:230px">' + v[0] + '</td>' +
'<td style="width:190px">' + v[1] + '</td>' +
'<td style="width:190px">' + v[2] + '</td>' +
'<td style="width:190px">' + v[3] + '</td>' +
'<td style="width:190px">' + v[4] + '</td>' +
'<td style="width:190px">' + v[5] + '</td>' +
'<td style="width:190px">' + v[6] + '</td></tr>');
});
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{alert('查询失败');}
});
});
})
</script>
后端代码:
def post(self, request):
nick = request.session.get('nick')
if request.session.get('k_login', None):
k8s_url = request.session['k8surl']
k8s_url = 'https://' + k8s_url
# ktitle = '已登录' + k8s_url
k8s_token = request.session['k8stoken']
configuration = client.Configuration()
configuration.host = k8s_url
configuration.verify_ssl = False
configuration.api_key = {"authorization": "Bearer " + k8s_token}
client1 = api_client.ApiClient(configuration=configuration)
api = core_v1_api.CoreV1Api(client1)
ret = api.list_pod_for_all_namespaces(watch=False)
# ret是object类型,所以需要转换
data = []
x = 0
for i in ret.items:
data.append([])
data[x].append(i.metadata.name)
data[x].append(i.metadata.namespace)
data[x].append(i.status.phase )
data[x].append(i.status.pod_ip)
data[x].append(i.status.host_ip)
data[x].append(i.status.container_statuses[0].restart_count)
data[x].append(i.status.conditions[0].last_transition_time)
x += 1
# return HttpResponse(json.dumps({'nick': nick, 'ktitle': ktitle}))
return JsonResponse(data,safe=False)
如果是查询数据库更简单,不用转换数据了,直接json到前端
1572

被折叠的 条评论
为什么被折叠?



