2 changed files with 148 additions and 2 deletions
@ -0,0 +1,143 @@ |
|||
<?php |
|||
|
|||
namespace App\Admin\Extensions\Form; |
|||
use Dcat\Admin\Form\Field\Radio; |
|||
use Dcat\Admin\Widgets\Radio as WidgetRadio; |
|||
|
|||
/** |
|||
* 由于信息收集表单中,hasMany中的radio存在多个时出现问题,故重写覆盖 |
|||
* Class Radio2 |
|||
* @package App\Admin\Extensions\Form |
|||
*/ |
|||
class Radio2 extends Radio |
|||
{ |
|||
protected $view = 'admin::form.radio'; |
|||
|
|||
/** |
|||
* {@inheritdoc} |
|||
*/ |
|||
public function render() |
|||
{ |
|||
if ($this->options instanceof \Closure) { |
|||
$this->options( |
|||
$this->options->call($this->values(), $this->value(), $this) |
|||
); |
|||
} |
|||
|
|||
$this->addCascadeScript(); |
|||
|
|||
$radio = WidgetRadio::make($this->getElementName(), $this->options, $this->style); |
|||
|
|||
if ($this->attributes['disabled'] ?? false) { |
|||
$radio->disable(); |
|||
} |
|||
|
|||
$radio |
|||
->inline($this->inline) |
|||
->check($this->value()) |
|||
->class($this->getElementClassString()) |
|||
->size($this->size); |
|||
|
|||
$this->addVariables([ |
|||
'radio' => $radio, |
|||
]); |
|||
|
|||
return parent::render(); |
|||
} |
|||
|
|||
/** |
|||
* Add cascade scripts to contents. |
|||
* |
|||
* @return string |
|||
*/ |
|||
protected function getCascadeScript() |
|||
{ |
|||
if (empty($this->conditions)) { |
|||
return; |
|||
} |
|||
|
|||
$cascadeGroups = collect($this->conditions)->map(function ($condition) { |
|||
return [ |
|||
'class' => $this->getCascadeClass($condition['value'], $condition['operator']), |
|||
'operator' => $condition['operator'], |
|||
'value' => $condition['value'], |
|||
]; |
|||
})->toJson(); |
|||
|
|||
return <<<JS |
|||
(function () { |
|||
var compare = function (a, b, o) { |
|||
if (! $.isArray(b)) { |
|||
return operator_table[o](a, b) |
|||
} |
|||
|
|||
if (o === '!=') { |
|||
var result = true; |
|||
for (var i in b) { |
|||
if (! operator_table[o](a, b[i])) { |
|||
result = false; |
|||
|
|||
break; |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
for (var i in b) { |
|||
if (operator_table[o](a, b[i])) { |
|||
return true; |
|||
} |
|||
} |
|||
}; |
|||
|
|||
var operator_table = { |
|||
'=': function(a, b) { |
|||
if ($.isArray(a) && $.isArray(b)) { |
|||
return $(a).not(b).length === 0 && $(b).not(a).length === 0; |
|||
} |
|||
|
|||
return String(a) === String(b); |
|||
}, |
|||
'>': function(a, b) { |
|||
return a > b; |
|||
}, |
|||
'<': function(a, b) { |
|||
return a < b; |
|||
}, |
|||
'>=': function(a, b) { return a >= b; }, |
|||
'<=': function(a, b) { return a <= b; }, |
|||
'!=': function(a, b) { |
|||
return ! operator_table['='](a, b); |
|||
}, |
|||
'in': function(a, b) { return Dcat.helpers.inObject(a, String(b), true); }, |
|||
'notIn': function(a, b) { return ! Dcat.helpers.inObject(a, String(b), true); }, |
|||
'has': function(a, b) { return Dcat.helpers.inObject(b, String(b), true); }, |
|||
}; |
|||
var cascade_groups = {$cascadeGroups}, event = '{$this->cascadeEvent}'; |
|||
|
|||
\$this.on(event, function (e) { |
|||
{$this->getFormFrontValue()} |
|||
|
|||
cascade_groups.forEach(function (event) { |
|||
var group = $(e.target).closest('.form-group.form-field').next('div.cascade-group.'+event.class); |
|||
if (compare(checked, event.value, event.operator)) { |
|||
group.removeClass('d-none'); |
|||
} else { |
|||
group.addClass('d-none'); |
|||
} |
|||
}); |
|||
}).trigger(event); |
|||
})(); |
|||
JS; |
|||
} |
|||
|
|||
/** |
|||
* @return string |
|||
*/ |
|||
protected function getFormFrontValue() |
|||
{ |
|||
return <<<JS |
|||
var checked = $('{$this->getElementClassSelector()}:checked').val(); |
|||
JS; |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue