修改缘由:如果一个JS某个字段的searchList要经常变化,那要修改很多地方。
修改方案:改成后台只需要添加一次即可。
application/common/model/Website/Website.php 模型文件:
public function getCountryList()
{
return ['0' => __('Country 0'), '1' => __('Country 1'), '2' => __('Country 2'), '3' => __('Country 3'), '4' => __('Country 4'), '5' => __('Country 5'), '6' => __('Country 6'), '7' => __('Country 7'), '8' => __('Country 8'), '9' => __('Country 9')];
}
application/lang/zh-cn/website.php 翻译文件:
<?php
return [
'Id' => 'ID',
'Name' => '名称',
'Url' => '域名',
'Avatar' => '头像',
'Resource' => '来源',
'Remarks' => '备注',
'Status' => '状态',
'Status 0' => '隐藏',
'Status 1' => '显示',
'Createtime' => '添加时间',
'Updatetime' => '更新时间',
'Deletetime' => '删除时间',
'Country' => '所属国家',
'Country 0' => '未归类',
'Country 1' => '越南',
'Country 2' => '印尼',
'Country 3' => '泰国',
'Country 4' => '墨西哥',
'Country 5' => '埃及',
'Country 6' => '印度',
'Country 7' => '巴西',
'Country 8' => '中国',
'Country 9' => '美国',
];
application/admin/controller/Website.php 控制器文件:
public function _initialize()
{
parent::_initialize();
$this->model = new \app\common\model\Website;
$this->view->assign("countryList", $this->model->getCountryList());
}
application/admin/view/website/index.html HTML文件:
<script> var countryList=<?php echo json_encode($countryList); ?>; </script>
public/assets/js/backend/website.js JS文件:
{field: 'country', title: __('Country'), searchList: countryList, formatter: Table.api.formatter.normal},
完成。