第一步
$grid->column('m_status', '状态')->switch();
第二步
$form->switch('column')->
customFormat(function ($value) {
// 将开启状态的值映射为 10,关闭状态的值映射为 20
$modifiedValue = $value ? 10 : 20;
return $modifiedValue;
})
->saving(function ($value) {
// 对开关字段的值进行修改和处理
return $value;
});
第三步,文件目录
bagak >vendor >dcat > laravel admin > src >Grid ) Displayers >SwitchDisplay.php
public function display(string $color = '', $refresh = false)
{
if ($color instanceof \Closure) {
$color->call($this->row, $this);
} else {
$this->color($color);
}
$column = $this->column->getName();
$checked = $this->value == 20 ? 'checked' : '';
$color = $this->color ?: Admin::color()->primary();
$url = $this->url();
return Admin::view(
'admin::grid.displayer.switch',
compact('column', 'color', 'refresh', 'checked', 'url')
);
}
第四步:文件目录
vendor->dcat->laravel-admin->resources->vlews->gnd->displayer->switch.bladephp
swt.off('change').on('change', function (e) {
var that = $(this),
url = that.data('url'),
reload = that.data('reload'),
checked = that.is(':checked'),
name = that.attr('name'),
data = {},
value = checked ? 20 : 10;
if (name.indexOf('.') === -1) {
data[name] = value;
} else {
name = name.split('.');
data[name[0]] = {};
data[name[0]][name[1]] = value;
}
Dcat.NP.start();
console.log(data);
$.put({
url: url,
data: data,
success: function (d) {
Dcat.NP.done();
var msg = d.data.message || d.message;
if (d.status) {
Dcat.success(msg);
reload && Dcat.reload();
} else {
Dcat.error(msg);
}
}
});
});
第五步 文件目录
vendor >laravel-admin ->dcat->Form->Feld->src->SwitchField.php
*/
protected function prepareInputValue($value)
{
return $value == 20 ? 20 : 10;
}
public function render()
{
if (empty($this->attributes['data-size'])) {
$this->small();
}
if (empty($this->attributes['data-color'])) {
$this->primary();
}
$this->attribute('name', $this->getElementName());
$this->attribute('value', 20);
$this->attribute('type', 'checkbox');
$this->attribute('data-plugin', $this->getFormElementId() . 'switchery');
return parent::render();
}