大家(jia)好(hao),這個不起眼的角落功(gong)能(neng)或許(xu)大家(jia)都沒怎么關注,有時候在做(zuo)網站的時候,發現(xian)文章模型里(li)面的字段(duan)一多,添加(jia)欄目(mu)的時候。都要每個點擊編(bian)輯去關聯欄目(mu),這個時候就比(bi)較費時間(jian), 這個方法就是比(bi)較節約時間(jian)能(neng)夠(gou)批(pi)量(liang)添加(jia)和靈(ling)活去關聯相應的欄目(mu)。也可以(yi)批(pi)量(liang)一鍵刪除所有字段(duan)。
當然這些(xie)功能(neng)實(shi)不實(shi)用看(kan)自己需求,本經驗(yan)分享,也希望(wang)大(da)家學習借鑒。有用則有意義!
本分享也有唯一不完(wan)美的地(di)方那就是批(pi)量刪除模型(xing)批(pi)量成功后不能刷新(xin) 需要(yao)自己刷新(xin)。后續會更新(xin)。
效果截圖:
欄目


模型頻道


好(hao)了以下是(shi)分享修改的(de)部分涉及文件有三個,一個是(shi)控制頁PHP 和(he)2個模板(ban)頁:
控制頁(\application\admin\controller)
Field.php
模板頁(ye):(\application\admin\template\field)
arctype_index.html
channel_index.html
首(shou)先(xian)我們(men)先(xian)打(da)開(kai)控制頁:
Field.php
查找:
/**
* 模型字段管理
*/
public function channel_index()
{
/*同步欄目綁定(ding)的自定(ding)義字(zi)段*/
$this->syn_channelfield_bind();
/*--end*/
$channel_id = input('param.channel_id/d', 0);
$assign_data = array();
$condition = array();
// 獲取到所有GET參數
$param = input('param.');
/*同步更新附(fu)加表字(zi)段到自定(ding)義模型字(zi)段表中*/
if (empty($param['searchopt']) && !empty($channel_id)) {
$this->fieldLogic->synChannelTableColumns($channel_id);
}
/*--end*/
// 應用(yong)搜索(suo)條件
foreach (['keywords'] as $key) {
if (isset($param[$key]) && $param[$key] !== '') {
if ($key == 'keywords') {
$condition['a.name|a.title'] = array('LIKE', "%{$param[$key]}%");
// 過(guo)濾(lv)指定字段
// $banFields = ['id'];
// $condition['a.name'] = array(
// array('LIKE', "%{$param[$key]}%"),
// array('notin', $banFields),
// );
} else {
$condition['a.' . $key] = array('eq', $param[$key]);
}
}
}
/*顯示主表與附加表*/
$condition['a.channel_id'] = !empty($channel_id) ? array('IN', [$channel_id]) : ['GT', 0];
// 排序(xu)
$orderby = 'a.sort_order asc, a.ifmain asc, a.ifcontrol asc, a.id desc';
if (empty($channel_id)) $orderby = 'a.id desc';
// 總的字段管理 -- 只讀自定(ding)義字段
if (empty($channel_id)) $condition['a.ifsystem'] = 0;
/*模(mo)型列表*/
$channeltype_list = model('Channeltype')->getAll('*', [], 'id');
$assign_data['channeltype_list'] = $channeltype_list;
/*--end*/
$condition['a.ifcontrol'] = 0;
$cfieldM = Db::name('channelfield');
$count = $cfieldM->alias('a')->where($condition)->count('a.id');// 查詢滿足(zu)要求的總記錄數
$Page = $pager = new Page($count, config('paginate.list_rows'));// 實例化分(fen)頁(ye)類 傳入(ru)總記錄數和每頁(ye)顯示(shi)的記錄數
$list = $cfieldM->field('a.*')
->alias('a')
->where($condition)
->order($orderby)
->limit($Page->firstRow . ',' . $Page->listRows)
->select();
$show = $Page->show();// 分頁顯示輸出(chu)
$assign_data['page'] = $show; // 賦值分頁輸出(chu)
$assign_data['list'] = $list; // 賦值數據(ju)集
$assign_data['pager'] = $Page; // 賦(fu)值分頁對象
/*字(zi)段類型列表*/
$assign_data['fieldtypeList'] = Db::name('field_type')->field('name,title')->getAllWithIndex('name');
/*--end*/
// 模型信息
$assign_data['channeltype_row'] = \think\Cache::get('extra_global_channeltype');
/*模型ID*/
$assign_data['channel_id'] = $channel_id;
/*--end*/
大概(gai)在116行 新增以下代碼(ma):
/*允許(xu)發布(bu)文檔列表的欄(lan)目by小秋*/
$typeids = Db::name('channelfield_bind')->where(['field_id' => $id])->column('typeid');
$select_html = allow_release_arctype($typeids, [$channel_id]);
$this->assign('select_html', $select_html);
$this->assign('typeids', $typeids);
/*--end*/
$this->assign($assign_data);
return $this->fetch();
}
增加完后再查找:
/**
* 欄目字段管理
*/
public function arctype_index()
{
$channel_id = $this->arctype_channel_id;
$assign_data = array();
$condition = array();
// 獲取到所有GET參數(shu)
$param = input('param.');
/*同步(bu)更新欄目主(zhu)表(biao)字(zi)段(duan)到自定義字(zi)段(duan)表(biao)中*/
if (empty($param['searchopt'])) {
$this->fieldLogic->synArctypeTableColumns($channel_id);
}
/*--end*/
// 應用搜索條(tiao)件(jian)
foreach (['keywords'] as $key) {
if (isset($param[$key]) && $param[$key] !== '') {
if ($key == 'keywords') {
$condition['name|title'] = array('LIKE', "%{$param[$key]}%");
} else {
$condition[$key] = array('eq', $param[$key]);
}
}
}
// 模型ID
$condition['channel_id'] = array('eq', $channel_id);
$condition['ifsystem'] = array('neq', 1);
$cfieldM = Db::name('channelfield');
$count = $cfieldM->where($condition)->count('id');// 查(cha)詢滿足要求的總記錄數
$Page = $pager = new Page($count, config('paginate.list_rows'));// 實例化(hua)分頁(ye)類 傳入(ru)總記錄數(shu)和每頁(ye)顯(xian)示的(de)記錄數(shu)
$list = $cfieldM->where($condition)->order('sort_order asc, ifsystem asc, id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
$show = $Page->show();// 分頁顯示輸出(chu)
$assign_data['page'] = $show; // 賦值分頁輸出
$assign_data['list'] = $list; // 賦值數據集
$assign_data['pager'] = $Page; // 賦值分頁對象
/*字段類型列表*/
$assign_data['fieldtypeList'] = Db::name('field_type')->field('name,title')->getAllWithIndex('name');
/*--end*/
大概在1043行(xing)(新(xin)增(zeng)后的行(xing)數)下面新(xin)增(zeng)以(yi)下內容(rong):
// 獲取欄目列表(biao)by小秋
$typeList = $this->getArctypeList();
$assign_data['typeList'] = $typeList; // 賦值到模板
$assign_data['channel_id'] = $channel_id;
$this->assign($assign_data);
return $this->fetch();
}
然后繼續拉到最底下最后一行的 " } " 符號 上面新增以下內容:
/** 批量獲取欄目BY小秋 */
private function getArctypeList() {
return Db::name('arctype')
->field('id, typename')
->where('status', 1)
->select();
}
/**
* 批量添加字段(duan)到欄(lan)目by小秋
*/
public function batch_add_to_channel()
{
$fieldIds = input('post.field_ids/a', []);
$typeId = input('post.typeid', null);
if (empty($fieldIds) || $typeId === null) {
return json(['code' => 0, 'msg' => '參數錯誤']);
}
$typeId = (int)$typeId;
$bindModel = Db::name('channelfield_bind');
$successCount = 0;
$systemFieldsProcessed = [];
foreach ($fieldIds as $fieldId) {
// 獲取(qu)字段信息,增(zeng)加對ifsystem的查(cha)詢
$fieldInfo = $this->getFieldInfo($fieldId);
// 如果是系統字(zi)段(duan)(ifsystem=1)
if ($fieldInfo && $fieldInfo['ifsystem'] == 1) {
// 確保存(cun)在typeid=0的記錄
$globalRecordExists = $bindModel->where([
'field_id' => $fieldId,
'typeid' => 0
])->find();
if (!$globalRecordExists) {
// 添(tian)加typeid=0的(de)記(ji)錄
$data = [
'typeid' => 0,
'field_id' => $fieldId,
'add_time' => time(),
'update_time' => time()
];
$bindModel->insert($data);
$systemFieldsProcessed[] = $fieldId;
$successCount++;
}
// 當typeid為0時(shi),刪除其他同field_id的非(fei)0綁定記錄(即使是系統(tong)字(zi)段)
if ($typeId === 0) {
$bindModel->where([
'field_id' => $fieldId,
'typeid' => ['neq', 0]
])->delete();
$successCount++; // 記錄刪除操(cao)作(zuo)
}
// 系統字(zi)段不進(jin)行其(qi)他操(cao)作,跳過后續(xu)邏(luo)輯
continue;
}
// 非系統(tong)字段(ifsystem=0)按(an)原邏輯(ji)處理
// 確保每條數據(ju)(field_id+typeid)只(zhi)保留一條記錄
$this->deleteDuplicateRecords($bindModel, $fieldId, $typeId);
// 當(dang)typeid為(wei)0時,刪除其他同field_id的非0綁定記(ji)錄
if ($typeId === 0) {
$bindModel->where([
'field_id' => $fieldId,
'typeid' => ['neq', 0]
])->delete();
}
// 當typeid不為0時,刪除該field_id下(xia)typeid=0的記錄
else {
$bindModel->where([
'field_id' => $fieldId,
'typeid' => 0
])->delete();
}
// 檢查(cha)是否已(yi)存在記錄
$exists = $bindModel->where([
'typeid' => $typeId,
'field_id' => $fieldId
])->find();
if (!$exists) {
$data = [
'typeid' => $typeId,
'field_id' => $fieldId,
'add_time' => time(),
'update_time' => time()
];
$result = $bindModel->insert($data);
if ($result) {
$successCount++;
}
} else {
// 更新(xin)現有記錄的時間戳(chuo)
$updateResult = $bindModel->where([
'typeid' => $typeId,
'field_id' => $fieldId
])->update([
'update_time' => time()
]);
if ($updateResult !== false) {
$successCount++;
}
}
}
// 構建(jian)返回消息
$message = "成(cheng)功處理 {$successCount} 個字段";
if (!empty($systemFieldsProcessed)) {
$message .= ",為 " . count($systemFieldsProcessed) . " 個系統字段添加了全局配(pei)置";
}
if ($successCount > 0) {
return json(['code' => 1, 'msg' => $message]);
} else {
return json(['code' => 0, 'msg' => '沒有(you)字(zi)段需要處理']);
}
}
/**
* 獲取字段(duan)信息并檢查ifsystem屬性by小秋
*/
private function getFieldInfo($fieldId)
{
static $fieldCache = [];
// 檢查緩存
if (isset($fieldCache[$fieldId])) {
return $fieldCache[$fieldId];
}
// 獲(huo)取字段(duan)信(xin)息,增加對ifsystem的查(cha)詢
$row = model('Channelfield')->getInfo($fieldId, 'channel_id,name,ifmain,ifsystem');
// 緩存(cun)結果
$fieldCache[$fieldId] = $row;
return $row;
}
/**
* 刪除重復記錄,保留最新的一條by小(xiao)秋
*/
private function deleteDuplicateRecords($model, $fieldId, $typeId)
{
// 獲取所有重復記錄(lu)(按更新時間降序排(pai)列)
$duplicates = $model->where([
'field_id' => $fieldId,
'typeid' => $typeId
])
->order('update_time DESC')
->select();
// 如果(guo)有重復記錄,刪除除了第一條之(zhi)外的(de)所有記錄
if (count($duplicates) > 1) {
$idsToDelete = [];
foreach ($duplicates as $key => $record) {
if ($key > 0) { // 跳過第(di)一條(最新的)
$idsToDelete[] = $record['id'];
}
}
if (!empty($idsToDelete)) {
$model->where('id', 'in', $idsToDelete)->delete();
}
}
}
/**
* 批量刪除-欄目字段by小秋
*/
public function arctype_delete()
{
$channel_id = $this->arctype_channel_id;
$id_list = input('del_id/a', []); // 獲取數組形式的ID列表
if (!empty($id_list)) {
$success_ids = [];
$fail_ids = [];
$fail_msgs = [];
foreach ($id_list as $id) {
/*刪除表字段*/
$row = $this->fieldLogic->delArctypeField($id);
/*--end*/
if (0 < $row['code']) {
$map = array(
'id' => array('eq', $id),
'channel_id' => $channel_id,
);
$result = Db::name('channelfield')->field('channel_id,name')->where($map)->select();
$name_list = get_arr_column($result, 'name');
Db::startTrans();
try {
Db::name('channelfield')->where($map)->delete(); // 刪除(chu)字(zi)段的記錄(lu)
Db::name('channelfield_bind')->where('field_id', $id)->delete(); // 刪(shan)除字段(duan)綁(bang)定記錄
Db::name('field_custom_param')->where('field_id', $id)->delete(); // 刪除(chu)多選(xuan)項字段記(ji)錄
Db::commit();
$success_ids[] = $id;
adminLog('刪除欄目字段:' . implode(',', $name_list));
} catch (\Exception $e) {
Db::rollback();
$fail_ids[] = $id;
$fail_msgs[] = "ID({$id})刪除失敗:" . $e->getMessage();
}
} else {
$fail_ids[] = $id;
$fail_msgs[] = "ID({$id})刪除(chu)失敗(bai):" . $row['msg'];
}
}
\think\Cache::clear('channelfield');
\think\Cache::clear("arctype");
if (!empty($success_ids) && empty($fail_ids)) {
$this->success('全(quan)部刪除成功');
} elseif (!empty($success_ids) && !empty($fail_ids)) {
$this->error('部(bu)分刪除成功,失敗ID:' . implode(',', $fail_ids) . ',原(yuan)因:' . implode(';', $fail_msgs));
} else {
$this->error('全部(bu)刪除失敗,原因:' . implode(';', $fail_msgs));
}
} else {
$this->error('參(can)數有誤');
}
}
/**
* 批(pi)量刪(shan)除-模型字段by小秋
*/
public function channel_delete()
{
$channel_id = input('channel_id/d', 0);
$id_list = input('del_id/a', []); // 獲取數組形式的(de)ID列表(biao)
if (!empty($id_list)) {
$success_ids = [];
$fail_ids = [];
$fail_msgs = [];
// 批量查詢字段(duan)信息(提高效率(lv))
$fieldInfos = Db::name('channelfield')
->where('id', 'in', $id_list)
->where('channel_id', $channel_id)
->column('id,name,title', 'id');
foreach ($id_list as $id) {
$id = (int)$id;
// 獲(huo)取字(zi)段信息
$fieldInfo = $fieldInfos[$id] ?? null;
$fieldTitle = "ID:{$id}:" .Db::name('channelfield')->where('id', $id)->value('title');
// 調(diao)用刪除(chu)邏輯
$result = $this->fieldLogic->delChannelField($id);
if ($result['code'] > 0) {
$success_ids[] = $id;
// 記錄操作日志
adminLog('刪除(chu)欄(lan)目字(zi)段(duan):' . $fieldTitle);
} else {
$fail_ids[] = $fieldTitle;
$fail_msgs[] = "字(zi)段[{$fieldTitle}]刪(shan)除失敗(bai):" . $result['msg'];
}
}
// 清理相關緩存
\think\Cache::clear('channelfield');
\think\Cache::clear("arctype");
// 返回(hui)處理結果
if (empty($fail_ids)) {
$this->success('全(quan)部刪除成功,共刪除' . count($success_ids) . '個字段');
} elseif (!empty($success_ids)) {
$errorMsg = '成功刪除' . count($success_ids) . '個字(zi)段,失敗' . count($fail_ids) . '個';
if (!empty($fail_msgs)) {
$errorMsg .= '<br>失敗(bai)詳情:' . implode('<br>', array_unique($fail_msgs));
}
$this->error($errorMsg);
} else {
$this->error('刪除詳情:<br>' . implode('<br>', $fail_msgs));
}
} else {
$this->error('請(qing)選(xuan)擇要刪除的字段');
}
}
控制頁就修改完成。
第二步打開:arctype_index.html
在第(di)二行。添(tian)加(jia)一下CSS代(dai)碼:
<style>
.sDiva {
display: inline-flex;
align-items: center;
margin-left: 20px;
}
.form-group {
margin-right: 15px;
}
.form-groupsawe {
margin-right: 30px;
}
.sDiv {
float: right;
display: inline-flex;
align-items: center;
}
.sDiv2:first-child {
margin-right: 15px;
}
@media (max-width: 992px) {
.sDiva {
margin-left: 10px;
margin-top: 10px;
}
.sDiv {
float: none;
margin-top: 10px;
}
}
.trSelected {
background-color: #f0f9ff !important;
}
.footer-oper .ml15 {
margin-left: 7px;
display: inline-flex;
align-items: center;
}
</style>
繼續查(cha)找(zhao)(zhao):大概77行下面 找(zhao)(zhao)到這段(duan):
<form class="navbar-form form-inline" action="{:url('Field/arctype_index')}" method="get" on-submit="layer_loading('正在處(chu)理');">
{$searchform.hidden|default=''}
<div class="sDiv">
<div class="sDiv2">
<input type="hidden" name="searchopt" value="1">
<input type="text" size="30" name="keywords" value="{$Request.param.keywords}" class="qsbox" placeholder="字段搜索...">
<input type="submit" class="btn" value="搜索">
<i class="iconfont e-sousuo"></i>
</div>
</div>
</form>
</div>
替換為(wei):
<form id="batchAddForm" on-submit="return validateForm();" class="navbar-form form-inline" action="{:url('Field/channel_index')}" method="post" >
{$searchform.hidden|default=''}
<!-- 批(pi)量欄目管理 --><div class="sDiv">
<div class="sDiva">
<div class="form-group">
<label for="batch_channel">選擇欄目(mu):</label>
<select name="typeid" id="batch_channel" class="form-control">
<option value="0">—選擇全部欄目—</option>
{foreach $typeList as $type}
<option value="{$type.id}">{$type.typename}</option>
{/foreach}
</select>
</div>
<div class="form-groupsawe">
<button type="button" id="batchAddBtn" class="btn btn-primary">
<i class="fa fa-plus"></i> 批量(liang)添(tian)加到欄(lan)目
</button>
</div></div>
<div class="sDiv2">
<div class="sDiv">
<div class="sDiv2">
<input type="hidden" name="searchopt" value="1">
<input type="text" size="30" name="keywords" value="{$Request.param.keywords}" class="qsbox" placeholder="字段(duan)搜索(suo)...">
<input type="submit" class="btn" value="搜索">
<i class="iconfont e-sousuo"></i>
</div>
</div> </div> </div>
</form>
</div>
繼(ji)續(xu)查找:大概112行
<div class="hDiv">
<div class="hDivBox">
<table cellspacing="0" cellpadding="0" style="width: 100%">
<thead>
<tr>
在下面新增以下內容:
<th class="sign w30" axis="col0">
<div class="tc">
<!-- 頂部全選按鈕 -->
<input type="checkbox" id="checkAllTop">
</div>
</th>
繼續查找(zhao):大概(gai)163行
{empty name="list"}
<tr>
<td class="no-data" align="center" axis="col0" colspan="50">
<div class="no_row">
<div class="no_pic"><img src="__SKIN__/images/null-data.png"></div>
</div>
</td>
</tr>
{else/}
{foreach name="list" item="vo" key="k" }
<tr>
在下面新增以下內容:
<td class="sign">
<input type="checkbox" name="field_ids[]" value="{$vo.id}" class="field-check">
</td>
再繼續查找:大概244行
<div class="iDiv" style="display: none;"></div>
</div>
{notempty name="list"}
<div class="tDiv">
<div class="tDiv2">
替換為以下代碼:
<div class="iDiv" style="display: none;"></div>
</div>
{notempty name="list"}
<div class="footer-oper">
<span class="ml15">
<!-- 底部(bu)全(quan)選(xuan)按鈕 -->
<input type="checkbox" class="checkAll" id="checkAllBottom">
<label for="checkAllBottom">全選</label>
</span>
{eq name="'Field@arctype_delete'|is_check_access" value="1"}
<a href="javascript:void(0);" on-click="batch_del(this, 'field_ids');" data-url="{:url('Field/arctype_delete')}" {eq name="$global['web_recycle_switch']" value='1'} data-deltype="del" {else /} data-deltype="pseudo" {/eq} class="layui-btn layui-btn-primary" title="批(pi)量(liang)刪除">批(pi)量(liang)刪除</a>
{/eq}
最后 在最底下的倒數第二行插入以下JS代碼:
<script>
$(function() {
// 全選(xuan)(xuan)/取(qu)消全選(xuan)(xuan)(頂部和(he)底部同步)
var checkAllTop = $("#checkAllTop");
var checkAllBottom = $("#checkAllBottom");
var fieldChecks = $(".field-check");
// 頂部全選控制(zhi)
checkAllTop.click(function() {
var isChecked = $(this).prop("checked");
fieldChecks.prop("checked", isChecked);
checkAllBottom.prop("checked", isChecked);
});
// 底部全選控(kong)制
checkAllBottom.click(function() {
var isChecked = $(this).prop("checked");
fieldChecks.prop("checked", isChecked);
checkAllTop.prop("checked", isChecked);
});
// 單個(ge)復選(xuan)框變(bian)化時更新(xin)全選(xuan)狀(zhuang)態
fieldChecks.click(function() {
var allChecked = fieldChecks.length === fieldChecks.filter(":checked").length;
checkAllTop.prop("checked", allChecked);
checkAllBottom.prop("checked", allChecked);
});
// 批量添加到欄(lan)目
$("#batchAddBtn").click(function() {
var typeId = $("#batch_channel").val();
var fieldIds = [];
$(".field-check:checked").each(function() {
fieldIds.push($(this).val());
});
if (typeId == '') {
layer.msg('請選擇欄目', {icon: 5});
return false;
}
if (fieldIds.length == 0) {
layer.msg('請選(xuan)擇要添加的(de)字段', {icon: 5});
return false;
}
layer.confirm('確定(ding)要將(jiang)選(xuan)中的 ' + fieldIds.length + ' 個字段(duan)添(tian)加到該欄目(mu)嗎?', {
icon: 3,
title: '確認操(cao)作'
}, function(index) {
$.ajax({
url: "{:url('batch_add_to_channel')}",
type: "POST",
data: {
field_ids: fieldIds,
typeid: typeId
},
dataType: "json",
success: function(res) {
if (res.code == 1) {
layer.msg(res.msg, {icon: 1});
setTimeout(function() {
location.reload();
}, 1500);
} else {
layer.msg(res.msg, {icon: 5});
}
},
error: function() {
layer.msg('網絡錯誤,請重試', {icon: 5});
}
});
layer.close(index);
});
});
});
</script>
以上是欄目的字段(duan)模(mo)板頁完成。
以下是頻道字段的修改:
打開 channel_index.html 文件
在第二行插入以下CSS代碼:
<style>
.sDiva {
display: inline-flex;
align-items: center;
margin-left: 20px;
}
.form-group {
margin-right: 30px;
}
.form-groupsawe {
margin-right: 30px;
}
.sDiv {
float: right;
display: inline-flex;
align-items: center;
}
.sDiv2:first-child {
margin-right: 15px;
}
@media (max-width: 992px) {
.sDiva {
margin-left: 10px;
margin-top: 10px;
}
.sDiv {
float: none;
margin-top: 10px;
}
}
</style>
接的查找:大概79行
<form id="searchForm" class="navbar-form form-inline" action="{:url('Field/channel_index')}" method="get" on-submit="layer_loading('正在處理');">
{$searchform.hidden|default=''}
<div class="sDiv">
<div class="sDiv2">
<select id="select_channel" class="select" style="margin:0px 5px;">
<option value="">--全部模型--</option>
{volist name="$channeltype_row" id="vo"}
<option value="{$vo.id}" {eq name="$channel_id" value="$vo.id"}selected{/eq}>{$vo.title}</option>
{/volist}
</select>
</div>
<div class="sDiv2">
<input type="hidden" name="searchopt" value="1">
<input type="hidden" name="channel_id" id="channel_id" value="{$channel_id|default='1'}">
<input type="text" size="30" name="keywords" value="{$Request.param.keywords}" class="qsbox" placeholder="標題/名(ming)稱搜索(suo)...">
<input type="submit" class="btn" value="搜索">
<i class="iconfont e-sousuo"></i>
</div>
</div>
</form>
</div>
替換為以下代碼:
<form id="batchAddForm" on-submit="return validateForm();" class="navbar-form form-inline" action="{:url('Field/channel_index')}" method="post" >
{$searchform.hidden|default=''}
<!-- 批量欄(lan)目管(guan)理 --><div class="sDiv">{notempty name="$channel_id"}
<div class="sDiva">
<div class="form-group">
<label for="batch_channel">選擇欄目(mu):</label>
<select name="typeid" id="batch_channel" class="form-control">
<option value="0">—選擇全部(bu)欄目—</option>
{$select_html}
</select>
</div>
<div class="form-groupsawe">
<button type="button" id="batchAddBtn" class="btn btn-primary">
<i class="fa fa-plus"></i> 批量(liang)添加到欄目
</button>
</div></div>{/notempty}
<div class="sDiv2">
<select id="select_channel" class="select" style="margin:0px 5px;">
<option value="">--全(quan)部模型--</option>
{volist name="$channeltype_row" id="vo"}
<option value="{$vo.id}" {eq name="$channel_id" value="$vo.id"}selected{/eq}>{$vo.title}</option>
{/volist}
</select>
</div>
<div class="sDiv2">
<input type="hidden" name="searchopt" value="1">
<input type="hidden" name="channel_id" id="channel_id" value="{$channel_id|default='1'}">
<input type="text" size="30" name="keywords" value="{$Request.param.keywords}" class="qsbox" placeholder="標題/名稱搜(sou)索...">
<input type="submit" class="btn" value="搜索">
<i class="iconfont e-sousuo"></i>
</div>
</div>
</form>
</div>
再繼續查找(zhao):大概117行
<div class="hDiv">
<div class="hDivBox">
<table cellspacing="0" cellpadding="0" style="width: 100%">
<thead>
<tr>
下面新增(zeng)以下代(dai)碼:
<th class="sign w40" axis="col0">
<div class="tc"><input type="checkbox" id="checkAll"></div>
</th>
在接的查找:大概158行
<div class="bDiv" style="height: auto;">
<div id="flexigrid" cellpadding="0" cellspacing="0" border="0">
<table style="width: 100%;">
<tbody>
{empty name="list"}
<tr>
在下(xia)面新增以下(xia)代(dai)碼:
<td class="no-data" align="center" axis="col0" colspan="50">
<div class="no_row">
<div class="no_pic"><img src="__SKIN__/images/null-data.png"></div>
</div>
</td>
接的在查找:大概275行:
<div class="iDiv" style="display: none;"></div>
</div>
<div class="tDiv">
<div class="tDiv2">
{include file="public/page" /}
<div style="clear:both"></div>
</div>
</div>
</div>
替(ti)換(huan)為(wei)以下代碼:
<div class="iDiv" style="display: none;"></div>
</div>
<div class="footer-oper">
<span class="ml15">
<!-- 底部全選(xuan)按鈕 -->
<input type="checkbox" class="checkAll" id="checkAllBottom">
<label for="checkAllBottom">全選</label>
</span>
{eq name="'Field@channel_delete'|is_check_access" value="1"}
<!-- 原代碼中可能缺少channel_id參(can)數 -->
<a href="javascript:void(0);" on-click="batch_del(this, 'del_ids');" data-url="{:url('Field/channel_delete', ['channel_id'=>$channel_id])}" {eq name="$global['web_recycle_switch']" value='1'} data-deltype="del" {else /} data-deltype="pseudo" {/eq}class="layui-btn layui-btn-primary" title="批(pi)量(liang)刪除">批(pi)量(liang)刪除</a>
{/eq}
{include file="public/page" /}
</div>
</div>
最后將以下JS部分(fen)的代碼添加到最底下的倒數第二(er)行即可:
<script>
$(function() {
const checkAllTop = $("#checkAll");
const checkAllBottom = $("#checkAllBottom");
const fieldCheckboxes = $(".field-check");
checkAllTop.on("click", function() {
const isChecked = $(this).prop("checked");
fieldCheckboxes.prop("checked", isChecked);
checkAllBottom.prop("checked", isChecked);
});
checkAllBottom.on("click", function() {
const isChecked = $(this).prop("checked");
fieldCheckboxes.prop("checked", isChecked);
checkAllTop.prop("checked", isChecked);
});
fieldCheckboxes.on("click", function() {
const allChecked = fieldCheckboxes.length === fieldCheckboxes.filter(":checked").length;
checkAllTop.prop("checked", allChecked);
checkAllBottom.prop("checked", allChecked);
});
$("#batchAddBtn").click(function() {
const typeId = $("#batch_channel").val();
const fieldIds = fieldCheckboxes.filter(":checked").map(function() {
return $(this).val();
}).get();
if (typeId === '') {
layer.msg('請選擇欄目', {icon: 5});
return false;
}
if (fieldIds.length === 0) {
layer.msg('請選擇(ze)要添加的字段', {icon: 5});
return false;
}
layer.confirm('確定(ding)要將選中的 ' + fieldIds.length + ' 個(ge)字段(duan)添加到該欄目嗎?', {
icon: 3,
title: '確認(ren)操作'
}, function(index) {
$.ajax({
url: "{:url('batch_add_to_channel')}",
type: "POST",
data: {
field_ids: fieldIds,
typeid: typeId
},
dataType: "json",
success: function(res) {
if (res.code == 1) {
layer.msg(res.msg, {icon: 1});
setTimeout(function() {
location.reload();
}, 1500);
} else {
layer.msg(res.msg, {icon: 5});
}
},
error: function() {
layer.msg('網(wang)絡錯誤,請重試', {icon: 5});
}
});
layer.close(index);
});
});
});
</script>
加(jia)(jia)完以(yi)后 所有的(de)(de)工作都完成了(le) 本(ben)次(ci)分享就到此結(jie)束(shu),希望(wang)能幫助到大(da)家。如果需求多(duo)的(de)(de)話(hua) 會考慮加(jia)(jia)到內置版本(ben)里面不多(duo)就臨時這樣。