以下是一个示例代码,可以实现你所需的功能:
1.在zz_category表中添加一个name字段;
2.在Count控制器中添加以下代码:
public function index()
{
$categories = Db::name('category')->select();
$this->assign('categories', $categories);
// 在Backend的index页面中渲染列表
return $this->fetch('index');
}
public function getCount()
{
$categoryId = $this->request->post('category_id');
$categoryName = $this->request->post('category_name');
$count = Db::name('category')->where('id', $categoryId)->value('count');
// 基于你的需求,在此处进行数量统计
// 返回JSON数据
return $this->success('success',null,$count);
}
3.在index.html页面中在需要展示下拉菜单的位置加入以下代码:
<select name="category_id" id="category-select">
{% for category in categories %}
<option value="{{category.id}}">{{category.name}}</option>
{% endfor %}
</select>
<button onclick="getCount()">获取数量</button>
<script>
// 获取数量
function getCount() {
var categoryId = $('#category-select').val();
var categoryName = $('#category-select option:selected').text();
$.ajax({
type: 'POST',
url: '{:url('getCount')}',
data: {
'category_id': categoryId,
'category_name': categoryName
},
dataType: 'json',
success: function (res) {
if (res.code == 1) {
alert(res.msg);
} else {
alert(res.data);
}
}
});
}
</script>
4.以上代码仅供参考,根据你的具体需求需要进行调整。