laravel-admin解決表單select聯(lián)動(dòng)時(shí),編輯默認(rèn)沒選上的問題
今天在開發(fā)公司一個(gè)功能時(shí),公司開發(fā)環(huán)境用的是laravel-admin,因?yàn)樾枰蒙蟬elect聯(lián)動(dòng),所以根據(jù)文檔說明進(jìn)行開發(fā),并成功的使用上了,代碼我就不重復(fù),大家可以去參考laravel-admin官網(wǎng)的說明。
首先我們找到select的js,路徑:跟目錄/vendor/encore/laravel-admin/src/Form/Field下的Select.php文件,找到下面代碼:
$script = <<<EOT
$(document).on('change', "{$this->getElementClassSelector()}", function () {
var target = $(this).closest('.fields-group').find(".$class");
$.get("$sourceUrl?q="+this.value, function (data) {
target.find("option").remove();
$(target).select2({
data: $.map(data, function (d) {
d.id = d.$idField;
d.text = d.$textField;
return d;
})
}).trigger('change');
});
});
EOT;
并修改成以下代碼:
$script = <<<EOT
$(document).on('change', "{$this->getElementClassSelector()}", function () {
var target = $(this).closest('.fields-group').find(".$class");
$.get("$sourceUrl?q="+this.value, function (data) {
target.find("option").remove();
$(target).select2({
data: $.map(data, function (d) {
d.id = d.$idField;
d.text = d.$textField;
return d;
})
}).trigger('change');
});
});
$('{$this->getElementClassSelector()}').trigger('change');
EOT;
我們?cè)谠写a中加入這句:
$('{$this->getElementClassSelector()}').trigger('change');
作用就是在初始化的時(shí)候觸發(fā)一次聯(lián)動(dòng)。
然后在我們的表單中,我們?cè)賮矶x編輯初始時(shí)候的值,代碼如下:
$form->select('hezuo', "合作模式")->options(function () {
$record = request()->route()->parameters();
$record = $record["chanpin"];
$data = ChanpinModel::where('id', $record)->first();
$hezuoList = array(
"1" => '測試1',
"2" => '測試2',
"3" => '測試3',
);
$hezuo = $data->hezuo;
return [$hezuo => $hezuoList[$hezuo]];
});
這段代碼是根據(jù)當(dāng)前記錄的值,去獲取對(duì)應(yīng)所屬那個(gè)選項(xiàng),這樣便使select聯(lián)動(dòng)編輯時(shí),能夠默認(rèn)選上我們的值。
以上這篇laravel-admin解決表單select聯(lián)動(dòng)時(shí),編輯默認(rèn)沒選上的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 關(guān)于laravel后臺(tái)模板laravel-admin select框的使用詳解
- laravel-admin select框默認(rèn)選中的方法
- 在Laravel中使用DataTables插件的方法
- PHP框架Laravel插件Pagination實(shí)現(xiàn)自定義分頁
- 推薦幾款用 Sublime Text 開發(fā) Laravel 所用到的插件
- Laravel框架表單驗(yàn)證詳解
- Laravel中使用FormRequest進(jìn)行表單驗(yàn)證方法及問題匯總
- Laravel 5框架學(xué)習(xí)之表單
- Laravel實(shí)現(xiàn)表單提交
- Laravel 5框架學(xué)習(xí)之表單驗(yàn)證
- Laravel 5框架學(xué)習(xí)之子視圖和表單復(fù)用
- laravel框架select2多選插件初始化默認(rèn)選中項(xiàng)操作示例
相關(guān)文章
php使用get和post傳遞數(shù)據(jù)出現(xiàn)414?Request-URI?Too?Large的原因分析及解決方案
Request-URI Too Large(請(qǐng)求URI過長)是一個(gè)HTTP錯(cuò)誤狀態(tài)碼,表示所發(fā)送的HTTP請(qǐng)求中的URI(統(tǒng)一資源標(biāo)識(shí)符)長度超過了服務(wù)器能夠處理的限制,這篇文章主要介紹了php使用get和post傳遞數(shù)據(jù)出現(xiàn)414?Request-URI Too?Large的解決方案,需要的朋友可以參考下2023-08-08
PHP會(huì)員找回密碼功能的簡單實(shí)現(xiàn)
下面小編就為大家?guī)硪黄狿HP會(huì)員找回密碼功能的簡單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
PHP的error_reporting錯(cuò)誤級(jí)別變量對(duì)照表
這篇文章主要介紹了PHP的error_reporting錯(cuò)誤級(jí)別變量對(duì)照表,需要的朋友可以參考下2014-07-07
docker-compose部署php項(xiàng)目實(shí)例詳解
在本篇文章里小編給大家整理了關(guān)于docker-compose部署php項(xiàng)目的相關(guān)實(shí)例以及代碼內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2019-07-07
微信網(wǎng)頁授權(quán)(OAuth2.0) PHP 源碼簡單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了微信網(wǎng)頁授權(quán)(OAuth2.0) PHP 源碼簡單實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08

