通过减去选项计算总数 - 动态形式 - Yii2
Calculate the total by subtracting options - Dynamics forms - Yii2
我想计算 "qualification" 输入字段中嵌套表单的 "score" 个字段的总数。目前我可以添加字段 "score" 并在 "qualification" 中显示总数,但问题是当我删除字段 "score" 我没有更新 "qualification" 字段但它仍然存在结果相同。额外的可以设置一些限制以避免通过等级,例如这不大于 10,如果你能帮助我,我将不胜感激。
表格_
<?php
$form = ActiveForm::begin([
'id' => 'dynamic-form',
"enableAjaxValidation" => true,
]);
?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($model, 'title')->textInput(['maxlength' => true])->input('title', ['placeholder' => "Title Evaluation"]) ?>
<div class="row">
<div class="col-sm-6">
<?=
$form->field($model, 'date_start')->widget(DateTimePicker::className(), [
'options' => ['placeholder' => '-- Select Date --'],
'pluginOptions' => [
'language' => 'fr',
'autoclose' => true,
'calendarWeeks' => true,
'daysOfWeekDisabled' => [0, 5],
'todayHighlight' => true,
'todayBtn' => true,
]
]);
?>
</div>
<div class="col-sm-6">
<?=
$form->field($model, 'date_end')->widget(DateTimePicker::className(), [
'options' => ['placeholder' => '-- Select Date --'],
'pluginOptions' => [
'language' => 'fr',
'autoclose' => true,
]
]);
?>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<?=
$form->field($model, "type_evaluation_id")->dropDownList(
ArrayHelper::map(EvalTipo::find()->all(), 'id', 'description'), [
'prompt' => '-- Select Type --'
]);
?>
</div>
<div class="col-sm-3">
<?=
$form->field($model, 'duration')->widget(TimePicker::className(), [
'pluginOptions' => [
'showSeconds' => true,
'showMeridian' => false,
'minuteStep' => 1,
'secondStep' => 5,
]
]);
?>
</div>
<div class="col-sm-3">
<?= $form->field($model, 'qualification')->textInput(['placeholder' => 'Qualification']) ?>
</div>
</div>
</div>
<div class="col-sm-6">
<?= $form->field($model, 'description')->textarea(['rows' => 9, 'placeholder' => 'Enter evaluation description']) ?>
</div>
</div>
<div class="content">
<div class="box box-success box-solid">
<div class="box-header with-border">
<h3 class="box-title">Questions Evaluation</h3>
</div>
<div class="panel-body">
<?php
DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 30, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelQuestion[0],
'formId' => 'dynamic-form',
'formFields' => [
'item',
'puntuacion',
],
]);
?>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Question</th>
<th style="width: 500px;">Options</th>
<th class="text-center" style="width: 90px;">
<button type="button" class="add-item btn btn-success btn-xs"><span class="glyphicon glyphicon-plus"></span></button>
</th>
</tr>
</thead>
<tbody class="container-items">
<?php foreach ($modelQuestion as $indexQuestion => $modelQuestions): ?>
<tr class="item">
<td class="vcenter">
<table class="table table-bordered table-striped">
<td class="vcenter">1.</td>
<td class="vcenter">
<?php
// necessary for update action.
if (!$modelQuestions->isNewRecord) {
echo Html::activeHiddenInput($modelQuestions, "[{$indexQuestion}]id");
}
?>
<?= $form->field ( $modelQuestion , "[{$indexQuestion}]question" )->dropDownList (
ArrayHelper::map(QuestType::find()->all() , 'id' , 'description' ) , [
'prompt' => '-- Select Question --'
] ); ?>
<?= $form->field($modelQuestions, "[{$indexQuestion}]question")->label(false)->textInput(['maxlength' => true, 'placeholder' => 'Title question']) ?>
<?= $form->field($modelQuestions, "[{$indexQuestion}]score")->label(false)->textInput(['maxlength' => true, 'placeholder' => 'Score']) ?><?= $form->field($modelQuestions,"[{$indexQuestion}]justified ")->checkbox(array('label'=>'','labelOptions'=>array('style'=>'padding:5px;'), ))->label('You want a response justified ?');?>
</td>
</table>
</td>
<td>
<?=
$this->render('_form-opc', [
'form' => $form,
'indexEvaPr' => $indexQuestion,
'modelsOpc'=>$modelsOpc[$indexQuestion],
])
?>
</td>
<td class="text-center vcenter" style="width: 90px; verti">
<button type="button" class="remove-item btn btn-danger btn-xs"><span class="glyphicon glyphicon-minus"></span></button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? '<span class="fa fa-plus"></span> Crear' : '<span class="fa fa-edit"></span> Modificar', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
Js
$(document).on('keyup', 'input[id^="questions-"]', function() {
var sum = 0;
jQuery(".dynamicform_wrapper .remove-item").each(function(indexQuestion) {
var amount = $('#questions-' + indexQuestion + '-score').val().replace(/,/g , "");
sum += parseFloat(amount);
});
$('#question-qualification').val(sum);
});
您需要添加一个新的 Javascript Events afterDelete 事件,您的 JS 将是:
$(document).on('keyup', 'input[id^="questions-"]', function() {
var sum = 0;
jQuery(".dynamicform_wrapper .remove-item").each(function(indexQuestion) {
var amount = $('#questions-' + indexQuestion + '-score').val().replace(/,/g, "");
sum += parseFloat(amount);
});
$('#question-qualification').val(sum);
});
$(".dynamicform_wrapper").on("afterDelete", function(e) {
var sum = 0;
jQuery(".dynamicform_wrapper .remove-item").each(function(indexQuestion) {
var amount = $('#questions-' + indexQuestion + '-score').val().replace(/,/g, "");
sum += parseFloat(amount);
});
$('#question-qualification').val(sum);
});
我想计算 "qualification" 输入字段中嵌套表单的 "score" 个字段的总数。目前我可以添加字段 "score" 并在 "qualification" 中显示总数,但问题是当我删除字段 "score" 我没有更新 "qualification" 字段但它仍然存在结果相同。额外的可以设置一些限制以避免通过等级,例如这不大于 10,如果你能帮助我,我将不胜感激。
表格_
<?php
$form = ActiveForm::begin([
'id' => 'dynamic-form',
"enableAjaxValidation" => true,
]);
?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($model, 'title')->textInput(['maxlength' => true])->input('title', ['placeholder' => "Title Evaluation"]) ?>
<div class="row">
<div class="col-sm-6">
<?=
$form->field($model, 'date_start')->widget(DateTimePicker::className(), [
'options' => ['placeholder' => '-- Select Date --'],
'pluginOptions' => [
'language' => 'fr',
'autoclose' => true,
'calendarWeeks' => true,
'daysOfWeekDisabled' => [0, 5],
'todayHighlight' => true,
'todayBtn' => true,
]
]);
?>
</div>
<div class="col-sm-6">
<?=
$form->field($model, 'date_end')->widget(DateTimePicker::className(), [
'options' => ['placeholder' => '-- Select Date --'],
'pluginOptions' => [
'language' => 'fr',
'autoclose' => true,
]
]);
?>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<?=
$form->field($model, "type_evaluation_id")->dropDownList(
ArrayHelper::map(EvalTipo::find()->all(), 'id', 'description'), [
'prompt' => '-- Select Type --'
]);
?>
</div>
<div class="col-sm-3">
<?=
$form->field($model, 'duration')->widget(TimePicker::className(), [
'pluginOptions' => [
'showSeconds' => true,
'showMeridian' => false,
'minuteStep' => 1,
'secondStep' => 5,
]
]);
?>
</div>
<div class="col-sm-3">
<?= $form->field($model, 'qualification')->textInput(['placeholder' => 'Qualification']) ?>
</div>
</div>
</div>
<div class="col-sm-6">
<?= $form->field($model, 'description')->textarea(['rows' => 9, 'placeholder' => 'Enter evaluation description']) ?>
</div>
</div>
<div class="content">
<div class="box box-success box-solid">
<div class="box-header with-border">
<h3 class="box-title">Questions Evaluation</h3>
</div>
<div class="panel-body">
<?php
DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 30, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelQuestion[0],
'formId' => 'dynamic-form',
'formFields' => [
'item',
'puntuacion',
],
]);
?>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Question</th>
<th style="width: 500px;">Options</th>
<th class="text-center" style="width: 90px;">
<button type="button" class="add-item btn btn-success btn-xs"><span class="glyphicon glyphicon-plus"></span></button>
</th>
</tr>
</thead>
<tbody class="container-items">
<?php foreach ($modelQuestion as $indexQuestion => $modelQuestions): ?>
<tr class="item">
<td class="vcenter">
<table class="table table-bordered table-striped">
<td class="vcenter">1.</td>
<td class="vcenter">
<?php
// necessary for update action.
if (!$modelQuestions->isNewRecord) {
echo Html::activeHiddenInput($modelQuestions, "[{$indexQuestion}]id");
}
?>
<?= $form->field ( $modelQuestion , "[{$indexQuestion}]question" )->dropDownList (
ArrayHelper::map(QuestType::find()->all() , 'id' , 'description' ) , [
'prompt' => '-- Select Question --'
] ); ?>
<?= $form->field($modelQuestions, "[{$indexQuestion}]question")->label(false)->textInput(['maxlength' => true, 'placeholder' => 'Title question']) ?>
<?= $form->field($modelQuestions, "[{$indexQuestion}]score")->label(false)->textInput(['maxlength' => true, 'placeholder' => 'Score']) ?><?= $form->field($modelQuestions,"[{$indexQuestion}]justified ")->checkbox(array('label'=>'','labelOptions'=>array('style'=>'padding:5px;'), ))->label('You want a response justified ?');?>
</td>
</table>
</td>
<td>
<?=
$this->render('_form-opc', [
'form' => $form,
'indexEvaPr' => $indexQuestion,
'modelsOpc'=>$modelsOpc[$indexQuestion],
])
?>
</td>
<td class="text-center vcenter" style="width: 90px; verti">
<button type="button" class="remove-item btn btn-danger btn-xs"><span class="glyphicon glyphicon-minus"></span></button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? '<span class="fa fa-plus"></span> Crear' : '<span class="fa fa-edit"></span> Modificar', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
Js
$(document).on('keyup', 'input[id^="questions-"]', function() {
var sum = 0;
jQuery(".dynamicform_wrapper .remove-item").each(function(indexQuestion) {
var amount = $('#questions-' + indexQuestion + '-score').val().replace(/,/g , "");
sum += parseFloat(amount);
});
$('#question-qualification').val(sum);
});
您需要添加一个新的 Javascript Events afterDelete 事件,您的 JS 将是:
$(document).on('keyup', 'input[id^="questions-"]', function() {
var sum = 0;
jQuery(".dynamicform_wrapper .remove-item").each(function(indexQuestion) {
var amount = $('#questions-' + indexQuestion + '-score').val().replace(/,/g, "");
sum += parseFloat(amount);
});
$('#question-qualification').val(sum);
});
$(".dynamicform_wrapper").on("afterDelete", function(e) {
var sum = 0;
jQuery(".dynamicform_wrapper .remove-item").each(function(indexQuestion) {
var amount = $('#questions-' + indexQuestion + '-score').val().replace(/,/g, "");
sum += parseFloat(amount);
});
$('#question-qualification').val(sum);
});