今天在開發(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" => '測(cè)試1', "2" => '測(cè)試2', "3" => '測(cè)試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è)參考,也希望大家多多支持腳本之家。
標(biāo)簽:濟(jì)寧 烏魯木齊 迪慶 南寧 山南 鷹潭 三亞 赤峰
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《laravel-admin解決表單select聯(lián)動(dòng)時(shí),編輯默認(rèn)沒選上的問題》,本文關(guān)鍵詞 laravel-admin,解決,表單,select,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。