导语:实现界面上搜索项目的功能
html处修改
加入5-22行
{{template "../public/page_header.html" .}}
<div class="container-fluid">
<div class="row">
<!-- 搜索 -->
<div class="panel panel-default">
<div class="panel-heading">
搜索
</div>
<div class="panel-body">
<form role="form" class="form-inline" method="get" action="/{{config "String" "adminPath" ""}}/job">
<div class="form-group">
<label for="name">输入关键词</label>
<input type="text" class="form-control" value="{{.keyword}}" id="keyword" name="keyword" placeholder="请输入job名称">
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">开始搜索</button>
</div>
</form>
</div>
</div>
<!-- 列表展示 -->
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr class="th">
<th>job名称</th>
<th>job更新/创建时间</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody>
{{range $key,$value := .jobinfo}}
<tr>
<td>{{$value.Name}}</td>
<td>{{$value.AddTime | unixToDate}}</td>
<td class="text-center">
<a href="/{{config "String" "adminPath" ""}}/job/test/deploy?id={{$value.Id}}">测试环境发布</a>
<a href="/{{config "String" "adminPath" ""}}/job/test/deploy?id={{$value.Id}}"><button class="btn btn-primary">测试环境发布</button></a>
<!-- <a href="/{{config "String" "adminPath" ""}}/job/edit?id={{$value.Name}}">当前测试环境发布日志</a> -->
<a href="/{{config "String" "adminPath" ""}}/job/test/toProdDirect?id={{$value.Id}}">申请直接上线</a>
<a href="/{{config "String" "adminPath" ""}}/job/test/toProdDirect?id={{$value.Id}}"><button class="btn btn-danger">申请直接上线</button></a>
<!-- <a href="/{{config "String" "adminPath" ""}}/job/edit?id={{$value.Name}}">当前生产环境发布日志</a> -->
<!-- <a href="/{{config "String" "adminPath" ""}}/job/cancelDeploy?id={{$value.Id}}">取消上线申请</a> -->
<!-- <a href="/{{config "String" "adminPath" ""}}/job/edit?id={{$value.Id}}">取消上线</a> -->
<!-- <a class="delete" href="/{{config "String" "adminPath" ""}}/job/delete?id={{$value.Name}}">删除</a> -->
</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
go代码调整
原先
func (c *JobListController) JobList() {
// 这里应该需要调整where参数吧?
jobinfo := []models.Deploypara{}
models.DB.Find(&jobinfo)
c.Data["jobinfo"] = jobinfo
// 输出各个job及对应的分支信息
//beego.Info(jobinfo)
c.TplName = "admin/job/index.html"
}
修改后 通过调整查询的sql 实现搜索功能
func (c *JobListController) JobList() {
keyword := c.GetString("keyword")
beego.Info("keyword", keyword)
where := "1=1"
if len(keyword) > 0 {
where += " AND name like \"%" + keyword + "%\""
}
// 这里应该需要调整where参数吧?
jobinfo := []models.Deploypara{}
models.DB.Where(where).Find(&jobinfo)
c.Data["jobinfo"] = jobinfo
// 输出各个job及对应的分支信息
//beego.Info(jobinfo)
c.TplName = "admin/job/index.html"
}