如何从复选框列表字段返回键值,该复选框列表字段会自动更新另一个字段 [英] How to return the key value from a checkbox list field which automatically updates another field
问题描述
我希望这样做,以便选中复选框列表中的一个或多个选项时,该选项的值将更新金额"字段.
I would like it so that when one or more of the options in my checkboxlist has been ticked, the value of said option will then update the amount field.
我有点作弊,因为我使用键值作为字段的价格,然后我想用该值更新金额字段.
I'm sort of cheating as I'm using the key value to be the price of the field which I would then like to update the amount field with.
这已经很好地与下拉字段配合使用,并且确实根据选择的内容来计算金额字段.
This already works perfectly with the dropdown fields and this does calculate the amount field depending on what has been selected.
由于必须使复选框列表合理,因此返回值的方式不同.
As I've had to make the checkboxlist jasonable, the way in which this returns the value is different.
工作模式:
type_website:
label: Website Package
span: right
type: dropdown
placeholder: '-- Select Website Package --'
trigger:
action: show
condition: value[1]
field: type
type_add_pages:
label: 'Additional Pages'
span: right
type: dropdown
trigger:
action: show
condition: value[1]
field: type
type_addons_get:
label: 'Addons'
span: right
type: checkboxlist
trigger:
action: show
condition: value[1]
field: type
amount:
label: 'Total Amount'
span: left
type: text
readOnly: true
dependsOn:
- type_add_pages
- type_website
- type_addons_get
Jobs.php
protected $jsonable = [
'type_addons_get'
];
public function getTypeAddonsGetOptions()
{
return [
'30' => 'Blog',
'50' => 'Editable Website'
];
}
// Get the value for the amounts field
public function filterFields($fields, $context = null)
{
$typePages = $this->attributes['type_add_pages'];
$typeAddons = array_get($this->attributes, 'type_addons_get');
$typeWebsite = array_get($this->attributes, 'type_website');
return $fields->amount->value = $typeAddons + $typePages + $typeWebsite;
}
作为测试,如果我只是在Jobs.php中返回以下内容:
As a test, if I just return the following in Jobs.php:
return $fields->amount->value = $typeAddons;
然后我得到以下结果:
任何帮助都将非常有帮助,在此先感谢您!
Any help would be extremely helpful, thanks in advance!
推荐答案
坦白地说,我不经常使用后端表单.我实际上为客户和我自己生成了前端系统.因此,我通过在控制器页面(创建和更新)中使用javascript/jquery解决了此类问题.这就是我想您正在尝试做的.这是一个示例:
I will be honest and admit I don't work with the back end forms very often. I actually generate front end systems for clients and myself. So I solved an issue like this with using javascript/jquery in the controller pages (create and update). This is what I think you are trying to do. Here is an example:
以表格形式设置的后端,其中我将总价设置为只读数字输入字段:
Backend in form where I have total price set to read only number input field:
<script>
$('#Form-field-Products-price').change(doThing);
$('#Form-field-Products-set_size').change(doThing);
$('#Form-field-Products-set_color').change(doThing);
$('#checkbox_Form-field-Products-add_ons_1').change(doThing);
$('#checkbox_Form-field-Products-add_ons_2').change(doThing);
$('#checkbox_Form-field-Products-add_ons_3').change(doThing);
function doThing(){
var x = $('#Form-field-Products-price').val();
var y = $('#Form-field-Products-set_color').val();
switch (y) {
case 'silver':
color = 0;
break;
case 'bronze':
color = ".50";
break;
case 'gold':
color = "1.00";
break;
default:
color = 0;
}
var z = $('#Form-field-Products-set_size').val();
switch (z) {
case 'fullset':
size = 0;
break;
case 'partialset':
size = "1.00";
break;
default:
size = 0;
}
if ($('#checkbox_Form-field-Products-add_ons_1').prop('checked') == true)
{
var a = "1.00";
} else
{
var a = 0;
}
if ($('#checkbox_Form-field-Products-add_ons_2').prop('checked') == true)
{
var b = "1.00";
} else
{
var b = 0;
}
if ($('#checkbox_Form-field-Products-add_ons_3').prop('checked') == true)
{
var c = "1.50";
} else
{
var c = 0;
}
$("#Form-field-Products-total_price").val(Number(x) + Number(color) + Number(a) + Number(b) + Number(c) - Number(size));
console.log( Number(x) + ' ' + Number(color) + ' ' + Number(a) + ' ' + Number(b) + ' ' + Number(c) + ' ' + Number(size) );
}
</script>
在这里您可以看到我正在搜索文档中的这些ID,然后获得它们的值.将这些值转换为数字,然后将其推入总价"字段.
Here you can see that I am searching the document for these ID's then getting their value. Turning those values into numbers I then push them into the Total Price field.
以下是填写表格时的调整后价格:
Here is an adjusted price while filling out the form:
这篇关于如何从复选框列表字段返回键值,该复选框列表字段会自动更新另一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!