问题描述
朋友,在我的银行新产品注册表格中,我有“类别”和“子类别”字段。仅在选择父类别后才填充子类别值。这完美地工作,但是当我要编辑产品时,子类别值不包含数据库值。它是空的。我想带来同样的!我的代码如下:
<?PHP
$categories_list = [];
$subcategorias_list = [];
foreach ($categories as $category) {
$categories_list[$category->id] = $category->name;
foreach ($category->subcategories as $subcategory) {
$subcategories_list[$category->id][$subcategory->id] = $subcategory->name;
}
}
?>
<small>Select Category</small>
<?= $this->Form->control('category_id',[
'class' => 'form-control form-control-user','id' => 'category','label' => false,'required' => true,'options' => $categories_list,'placeholder' => 'Category',]);
?>
<small>Select Subcategory</small>
<?= $this->Form->control('subcategory_id','id' => 'subcategory','options' => [],'placeholder' => 'SubCategory',]);
?>
我的js看起来像这样:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
var subCategories = <?= json_encode($subcategorias_list); ?>;
$(document).ready(function() {
$('#category').change(function() {
var categoryId = $(this).val();
var subCategoriesObject = subCategories[categoryId];
$('#subcategory option:gt(0)').remove();
var subCategoriesSelect = $('#subcategory');
$.each(subCategoriesObject,function(key,value) {
subCategoriesSelect.append($("<option></option>").attr("value",key).text(
value));
});
});
});
</script>
我感谢您的任何评论,如果有人可以提出想法以将其值带入编辑表单,则表示感谢。
更新:
<?= $this->Form->control('subcategory_id','options' => $subcategories_list,]);
?>
但是,通过这种方式,它将返回数据库中注册的所有类别,而无需考虑链接的类别。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)