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


模型頻道


好了以下是(shi)分(fen)享修改的(de)部分(fen)涉及文(wen)件有三(san)個(ge),一個(ge)是(shi)控(kong)制頁PHP 和2個(ge)模板頁:
控(kong)制頁(ye)(\application\admin\controller)
Field.php
模板(ban)頁:(\application\admin\template\field)
arctype_index.html
channel_index.html
首(shou)先我們先打開控制頁(ye):
Field.php
查找:
/**
* 模(mo)型字(zi)段管理
*/
public function channel_index()
{
/*同步欄目綁定(ding)的自定(ding)義字段*/
$this->syn_channelfield_bind();
/*--end*/
$channel_id = input('param.channel_id/d', 0);
$assign_data = array();
$condition = array();
// 獲取到所有GET參數(shu)
$param = input('param.');
/*同步更新(xin)附加表(biao)字段(duan)到自(zi)定(ding)義(yi)模型字段(duan)表(biao)中(zhong)*/
if (empty($param['searchopt']) && !empty($channel_id)) {
$this->fieldLogic->synChannelTableColumns($channel_id);
}
/*--end*/
// 應用搜索條(tiao)件(jian)
foreach (['keywords'] as $key) {
if (isset($param[$key]) && $param[$key] !== '') {
if ($key == 'keywords') {
$condition['a.name|a.title'] = array('LIKE', "%{$param[$key]}%");
// 過(guo)濾指定(ding)字段
// $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];
// 排序
$orderby = 'a.sort_order asc, a.ifmain asc, a.ifcontrol asc, a.id desc';
if (empty($channel_id)) $orderby = 'a.id desc';
// 總的字段管理 -- 只讀自定義字段
if (empty($channel_id)) $condition['a.ifsystem'] = 0;
/*模型(xing)列(lie)表*/
$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');// 查詢滿(man)足要求的(de)總記錄數
$Page = $pager = new Page($count, config('paginate.list_rows'));// 實例化分頁類 傳入總記錄(lu)數(shu)和每頁顯示的記錄(lu)數(shu)
$list = $cfieldM->field('a.*')
->alias('a')
->where($condition)
->order($orderby)
->limit($Page->firstRow . ',' . $Page->listRows)
->select();
$show = $Page->show();// 分頁顯示輸出
$assign_data['page'] = $show; // 賦(fu)值分(fen)頁輸(shu)出
$assign_data['list'] = $list; // 賦(fu)值數據集
$assign_data['pager'] = $Page; // 賦值(zhi)分(fen)頁對象
/*字段類型列表*/
$assign_data['fieldtypeList'] = Db::name('field_type')->field('name,title')->getAllWithIndex('name');
/*--end*/
// 模(mo)型信息
$assign_data['channeltype_row'] = \think\Cache::get('extra_global_channeltype');
/*模型ID*/
$assign_data['channel_id'] = $channel_id;
/*--end*/
大概在116行(xing) 新增以下代碼:
/*允許發布文檔列表的欄目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();
}
增加完后再(zai)查找:
/**
* 欄(lan)目字段管理
*/
public function arctype_index()
{
$channel_id = $this->arctype_channel_id;
$assign_data = array();
$condition = array();
// 獲取到所有GET參(can)數
$param = input('param.');
/*同(tong)步(bu)更新欄目主表(biao)字(zi)段到自定義字(zi)段表(biao)中*/
if (empty($param['searchopt'])) {
$this->fieldLogic->synArctypeTableColumns($channel_id);
}
/*--end*/
// 應用搜索條件
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]);
}
}
}
// 模(mo)型(xing)ID
$condition['channel_id'] = array('eq', $channel_id);
$condition['ifsystem'] = array('neq', 1);
$cfieldM = Db::name('channelfield');
$count = $cfieldM->where($condition)->count('id');// 查詢滿足(zu)要(yao)求的總記錄數
$Page = $pager = new Page($count, config('paginate.list_rows'));// 實例化(hua)分頁類 傳(chuan)入總記(ji)錄(lu)數和每頁顯(xian)示的記(ji)錄(lu)數
$list = $cfieldM->where($condition)->order('sort_order asc, ifsystem asc, id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
$show = $Page->show();// 分頁顯示(shi)輸(shu)出
$assign_data['page'] = $show; // 賦(fu)值分頁(ye)輸出(chu)
$assign_data['list'] = $list; // 賦值數(shu)據集(ji)
$assign_data['pager'] = $Page; // 賦值分頁(ye)對象
/*字段類型列表*/
$assign_data['fieldtypeList'] = Db::name('field_type')->field('name,title')->getAllWithIndex('name');
/*--end*/
大概在1043行(xing)(新(xin)增(zeng)后的(de)行(xing)數)下面新(xin)增(zeng)以下內容:
// 獲取欄目列(lie)表by小秋(qiu)
$typeList = $this->getArctypeList();
$assign_data['typeList'] = $typeList; // 賦值(zhi)到模板(ban)
$assign_data['channel_id'] = $channel_id;
$this->assign($assign_data);
return $this->fetch();
}
然后繼續拉到最底下最后一行的 " } " 符號 上面新增以下內容:
/** 批量獲取(qu)欄目BY小(xiao)秋 */
private function getArctypeList() {
return Db::name('arctype')
->field('id, typename')
->where('status', 1)
->select();
}
/**
* 批量添(tian)加字段到(dao)欄目by小秋(qiu)
*/
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) {
// 獲取字段信息(xi),增加對ifsystem的查詢
$fieldInfo = $this->getFieldInfo($fieldId);
// 如(ru)果(guo)是系統字段(ifsystem=1)
if ($fieldInfo && $fieldInfo['ifsystem'] == 1) {
// 確保存在typeid=0的記錄
$globalRecordExists = $bindModel->where([
'field_id' => $fieldId,
'typeid' => 0
])->find();
if (!$globalRecordExists) {
// 添加typeid=0的記(ji)錄
$data = [
'typeid' => 0,
'field_id' => $fieldId,
'add_time' => time(),
'update_time' => time()
];
$bindModel->insert($data);
$systemFieldsProcessed[] = $fieldId;
$successCount++;
}
// 當typeid為(wei)0時,刪(shan)除(chu)其他同field_id的(de)非0綁定(ding)記錄(即(ji)使(shi)是系統(tong)字(zi)段(duan))
if ($typeId === 0) {
$bindModel->where([
'field_id' => $fieldId,
'typeid' => ['neq', 0]
])->delete();
$successCount++; // 記錄刪除操(cao)作(zuo)
}
// 系統字段不進行其他操作,跳過(guo)后(hou)續邏輯(ji)
continue;
}
// 非系(xi)統字段(ifsystem=0)按原邏(luo)輯(ji)處理
// 確保(bao)(bao)每條(tiao)數據(field_id+typeid)只(zhi)保(bao)(bao)留一條(tiao)記錄
$this->deleteDuplicateRecords($bindModel, $fieldId, $typeId);
// 當typeid為(wei)0時,刪(shan)除其(qi)他同field_id的非0綁定(ding)記錄
if ($typeId === 0) {
$bindModel->where([
'field_id' => $fieldId,
'typeid' => ['neq', 0]
])->delete();
}
// 當typeid不(bu)為0時,刪除該(gai)field_id下typeid=0的記錄(lu)
else {
$bindModel->where([
'field_id' => $fieldId,
'typeid' => 0
])->delete();
}
// 檢查是(shi)否已存在記(ji)錄
$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 {
// 更新現有記錄的時間戳(chuo)
$updateResult = $bindModel->where([
'typeid' => $typeId,
'field_id' => $fieldId
])->update([
'update_time' => time()
]);
if ($updateResult !== false) {
$successCount++;
}
}
}
// 構建返回消息
$message = "成功處理 {$successCount} 個(ge)字段";
if (!empty($systemFieldsProcessed)) {
$message .= ",為 " . count($systemFieldsProcessed) . " 個(ge)系統字段添加了(le)全局配置";
}
if ($successCount > 0) {
return json(['code' => 1, 'msg' => $message]);
} else {
return json(['code' => 0, 'msg' => '沒(mei)有字段需要處理']);
}
}
/**
* 獲取字段信息并檢(jian)查(cha)ifsystem屬性by小秋
*/
private function getFieldInfo($fieldId)
{
static $fieldCache = [];
// 檢查緩存
if (isset($fieldCache[$fieldId])) {
return $fieldCache[$fieldId];
}
// 獲取(qu)字(zi)段信(xin)息,增加對ifsystem的(de)查詢(xun)
$row = model('Channelfield')->getInfo($fieldId, 'channel_id,name,ifmain,ifsystem');
// 緩存結果
$fieldCache[$fieldId] = $row;
return $row;
}
/**
* 刪除重復(fu)記錄,保留最新的一條by小秋(qiu)
*/
private function deleteDuplicateRecords($model, $fieldId, $typeId)
{
// 獲取所有(you)重復記錄(按更(geng)新時間降序排列(lie))
$duplicates = $model->where([
'field_id' => $fieldId,
'typeid' => $typeId
])
->order('update_time DESC')
->select();
// 如果有(you)重復記(ji)錄,刪(shan)除除了第一(yi)條之(zhi)外的(de)所有(you)記(ji)錄
if (count($duplicates) > 1) {
$idsToDelete = [];
foreach ($duplicates as $key => $record) {
if ($key > 0) { // 跳過第(di)一條(最(zui)新的)
$idsToDelete[] = $record['id'];
}
}
if (!empty($idsToDelete)) {
$model->where('id', 'in', $idsToDelete)->delete();
}
}
}
/**
* 批量(liang)刪(shan)除-欄(lan)目(mu)字段(duan)by小(xiao)秋(qiu)
*/
public function arctype_delete()
{
$channel_id = $this->arctype_channel_id;
$id_list = input('del_id/a', []); // 獲取(qu)數組(zu)形(xing)式的ID列表
if (!empty($id_list)) {
$success_ids = [];
$fail_ids = [];
$fail_msgs = [];
foreach ($id_list as $id) {
/*刪(shan)除表字段*/
$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(); // 刪除字(zi)段(duan)的(de)記錄
Db::name('channelfield_bind')->where('field_id', $id)->delete(); // 刪(shan)除字段綁(bang)定記錄(lu)
Db::name('field_custom_param')->where('field_id', $id)->delete(); // 刪除多選(xuan)項(xiang)字段記錄(lu)
Db::commit();
$success_ids[] = $id;
adminLog('刪(shan)除欄目字段:' . implode(',', $name_list));
} catch (\Exception $e) {
Db::rollback();
$fail_ids[] = $id;
$fail_msgs[] = "ID({$id})刪(shan)除失敗(bai):" . $e->getMessage();
}
} else {
$fail_ids[] = $id;
$fail_msgs[] = "ID({$id})刪除失敗:" . $row['msg'];
}
}
\think\Cache::clear('channelfield');
\think\Cache::clear("arctype");
if (!empty($success_ids) && empty($fail_ids)) {
$this->success('全部刪除成功');
} elseif (!empty($success_ids) && !empty($fail_ids)) {
$this->error('部(bu)分刪(shan)除(chu)成功,失敗ID:' . implode(',', $fail_ids) . ',原因(yin):' . implode(';', $fail_msgs));
} else {
$this->error('全部刪(shan)除失(shi)敗,原因:' . implode(';', $fail_msgs));
}
} else {
$this->error('參數有誤(wu)');
}
}
/**
* 批量刪除-模型字(zi)段by小秋
*/
public function channel_delete()
{
$channel_id = input('channel_id/d', 0);
$id_list = input('del_id/a', []); // 獲取數組形(xing)式的ID列表
if (!empty($id_list)) {
$success_ids = [];
$fail_ids = [];
$fail_msgs = [];
// 批量查詢字(zi)段信(xin)息(xi)(提高效率(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;
// 獲取字段信息(xi)
$fieldInfo = $fieldInfos[$id] ?? null;
$fieldTitle = "ID:{$id}:" .Db::name('channelfield')->where('id', $id)->value('title');
// 調用刪(shan)除(chu)邏輯
$result = $this->fieldLogic->delChannelField($id);
if ($result['code'] > 0) {
$success_ids[] = $id;
// 記錄操作(zuo)日志(zhi)
adminLog('刪(shan)除欄目字段:' . $fieldTitle);
} else {
$fail_ids[] = $fieldTitle;
$fail_msgs[] = "字段[{$fieldTitle}]刪除失敗:" . $result['msg'];
}
}
// 清理(li)相關(guan)緩(huan)存
\think\Cache::clear('channelfield');
\think\Cache::clear("arctype");
// 返回處(chu)理結果
if (empty($fail_ids)) {
$this->success('全部刪除(chu)成功(gong),共刪除(chu)' . count($success_ids) . '個字段');
} elseif (!empty($success_ids)) {
$errorMsg = '成功刪除' . count($success_ids) . '個(ge)字段,失敗' . count($fail_ids) . '個(ge)';
if (!empty($fail_msgs)) {
$errorMsg .= '<br>失敗詳情:' . implode('<br>', array_unique($fail_msgs));
}
$this->error($errorMsg);
} else {
$this->error('刪除(chu)詳情:<br>' . implode('<br>', $fail_msgs));
}
} else {
$this->error('請選(xuan)擇要刪除的(de)字段');
}
}
控制頁就修改完成。
第二步打開:arctype_index.html
在(zai)第(di)二行。添(tian)加(jia)一下CSS代碼:
<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>
繼續查找:大概77行下面 找到(dao)這段:
<form class="navbar-form form-inline" action="{:url('Field/arctype_index')}" method="get" on-submit="layer_loading('正在處理');">
{$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="字段(duan)搜索...">
<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=''}
<!-- 批量(liang)欄目管理 --><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> 批(pi)量添加到(dao)欄目
</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="字段搜索...">
<input type="submit" class="btn" value="搜索">
<i class="iconfont e-sousuo"></i>
</div>
</div> </div> </div>
</form>
</div>
繼(ji)續查找:大概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">
<!-- 頂部全選(xuan)按鈕 -->
<input type="checkbox" id="checkAllTop">
</div>
</th>
繼續查找:大概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">
<!-- 底部全選按(an)鈕(niu) -->
<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="批量刪(shan)除(chu)">批量刪(shan)除(chu)</a>
{/eq}
最后 在最底下的倒數第二行插入以下JS代碼:
<script>
$(function() {
// 全選/取消全選(頂部和底(di)部同步)
var checkAllTop = $("#checkAllTop");
var checkAllBottom = $("#checkAllBottom");
var fieldChecks = $(".field-check");
// 頂部(bu)全(quan)選控制
checkAllTop.click(function() {
var isChecked = $(this).prop("checked");
fieldChecks.prop("checked", isChecked);
checkAllBottom.prop("checked", isChecked);
});
// 底(di)部全選控制(zhi)
checkAllBottom.click(function() {
var isChecked = $(this).prop("checked");
fieldChecks.prop("checked", isChecked);
checkAllTop.prop("checked", isChecked);
});
// 單(dan)個復選框(kuang)變化時(shi)更(geng)新全選狀態
fieldChecks.click(function() {
var allChecked = fieldChecks.length === fieldChecks.filter(":checked").length;
checkAllTop.prop("checked", allChecked);
checkAllBottom.prop("checked", allChecked);
});
// 批量添加到欄目
$("#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('請(qing)選擇(ze)要(yao)添加的字段', {icon: 5});
return false;
}
layer.confirm('確定要將(jiang)選中(zhong)的 ' + fieldIds.length + ' 個字段添加到該欄目嗎?', {
icon: 3,
title: '確認操作'
}, 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('網絡(luo)錯(cuo)誤,請重試', {icon: 5});
}
});
layer.close(index);
});
});
});
</script>
以上是欄目的字段模板頁完成。
以下是頻道字段的修改:
打開 channel_index.html 文件(jian)
在(zai)第二行(xing)插入(ru)以(yi)下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="">--全部(bu)模型--</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="標(biao)題/名(ming)稱(cheng)搜索...">
<input type="submit" class="btn" value="搜索(suo)">
<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=''}
<!-- 批量欄目(mu)管理 --><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">—選擇全(quan)部欄目—</option>
{$select_html}
</select>
</div>
<div class="form-groupsawe">
<button type="button" id="batchAddBtn" class="btn btn-primary">
<i class="fa fa-plus"></i> 批量添加(jia)到欄(lan)目
</button>
</div></div>{/notempty}
<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="標(biao)題(ti)/名稱搜(sou)索...">
<input type="submit" class="btn" value="搜索">
<i class="iconfont e-sousuo"></i>
</div>
</div>
</form>
</div>
再繼續查(cha)找:大概117行
<div class="hDiv">
<div class="hDivBox">
<table cellspacing="0" cellpadding="0" style="width: 100%">
<thead>
<tr>
下(xia)面新增以下(xia)代碼:
<th class="sign w40" axis="col0">
<div class="tc"><input type="checkbox" id="checkAll"></div>
</th>
在接的查找(zhao):大概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>
在下面新增以下代碼:
<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)為以下代碼(ma):
<div class="iDiv" style="display: none;"></div>
</div>
<div class="footer-oper">
<span class="ml15">
<!-- 底部(bu)全(quan)選按鈕 -->
<input type="checkbox" class="checkAll" id="checkAllBottom">
<label for="checkAllBottom">全(quan)選</label>
</span>
{eq name="'Field@channel_delete'|is_check_access" value="1"}
<!-- 原(yuan)代碼(ma)中可能(neng)缺少channel_id參數(shu) -->
<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="批量刪除(chu)">批量刪除(chu)</a>
{/eq}
{include file="public/page" /}
</div>
</div>
最后將以下(xia)JS部(bu)分的代碼添加到最底下(xia)的倒數第二行即可:
<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('請選(xuan)擇欄目', {icon: 5});
return false;
}
if (fieldIds.length === 0) {
layer.msg('請選擇要添加的(de)字段(duan)', {icon: 5});
return false;
}
layer.confirm('確定要(yao)將選中(zhong)的(de) ' + fieldIds.length + ' 個(ge)字(zi)段(duan)添加(jia)到該欄目嗎?', {
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('網(wang)絡錯(cuo)誤,請重試', {icon: 5});
}
});
layer.close(index);
});
});
});
</script>
加完以(yi)后 所有(you)的工作都完成了 本次分享(xiang)就到此(ci)結束(shu),希望能幫(bang)助到大家(jia)。如果需求多的話 會考(kao)慮加到內置版本里面不多就臨時這樣。