Jquery+bootstrap實現(xiàn)表格行置頂置底上移下移操作詳解
最近接到產(chǎn)品的一個需求,它是要對數(shù)據(jù)排序,實際操作中我們要實現(xiàn)表格行置頂置底上移下移操作。項目框架是GUNS框架。
如下圖:
我是怎么用Jquery+bootstrap進行實現(xiàn)這些功能的呢?往下看就知道了。
1.html
@layout("/common/_container.html"){ <div class="row"> ? ? <div class="col-sm-12"> ? ? ? ? <div class="ibox float-e-margins"> ? ? ? ? ? ? <div class="ibox-title"> ? ? ? ? ? ? ? ? <a href="/report">報表管理</a>>><a href="" onclick="getHrefUrl(this)">報表版本</a>>>配置指標(biāo) ? ? ? ? ? ? </div> ? ? ? ? ? ? <div class="ibox-content"> ? ? ? ? ? ? ? ? <div class="row row-lg"> ? ? ? ? ? ? ? ? ? ? <div class="col-sm-12"> ? ? ? ? ? ? ? ? ? ? ? ? <div class="row"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <input type="hidden" id="reportId" value="${reportId}"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <input type="hidden" id="verId" value="${verId}"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="col-sm-3"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <#NameCon id="condition" name="名稱" /> ? ? ? ? ? ? ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="col-sm-3"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <#button name="搜索" icon="fa-search" clickFun="QuotaVer.search()"/> ? ? ? ? ? ? ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? ? ? ? ? ? ? <div class="hidden-xs" id="QuotaVerTableToolbar" role="group"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? @if(shiro.hasPermission("/quotaVer/addIndex")){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? <#button name="添加指標(biāo)" icon="fa-plus" clickFun="QuotaVer.openAddQuota()"/> ? ? ? ? ? ? ? ? ? ? ? ? ? ? @} ? ? ? ? ? ? ? ? ? ? ? ? ? ? @if(shiro.hasPermission("/quotaVer/save")){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? <#button name="保存數(shù)據(jù)" icon="fa-plus" clickFun="QuotaVer.saveQuotaVer()"/> ? ? ? ? ? ? ? ? ? ? ? ? ? ? @} ? ? ? ? ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? ? ? ? ? ? ? <#table id="QuotaVerTable"/> ? ? ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? ? ? </div> ? ? ? ? ? ? </div> ? ? ? ? </div> ? ? </div> </div> <script src="${ctxPath}/static/modular/quotaVer/quotaVer/quotaVer.js"></script> <script> ? ? function getHrefUrl(a){ ? ? ? ? a.href = "/reportVer?reportId=" + document.getElementById("reportId").value; ? ? } </script> @}
注意:這里使用的是GUNS框架,所以代碼風(fēng)格跟一般的html寫法稍有不同。
2.JS代碼
{title: '操作', visible: true, align: 'center', valign: 'middle',events: operateEvents, ? ? ? ? ? ? ? ? formatter: operateFormatter}
function operateFormatter(value, row, index) { ? ? return [ ? ? ? ? '<a class="up" href="javascript:void(0)" title="Up">', ? ? ? ? '<i >上移</i>', ? ? ? ? '</a> ?', ? ? ? ? '<a class="down" href="javascript:void(0)" title="Down">', ? ? ? ? '<i >下移</i>', ? ? ? ? '</a> ?', ? ? ? ? '<a class="del" href="javascript:void(0)" title="Del">', ? ? ? ? '<i >刪除</i>', ? ? ? ? '</a> ?', ? ? ].join('') }
window.operateEvents = { ? ? 'click .up': function (e, value, row, index) { ? ? ? ? //點擊上移 ? ? ? ? var $tr = $(this).parents("tr"); ? ? ? ? if ($tr.index() == 0){ ? ? ? ? ? ? Feng.success("首行數(shù)據(jù)不可上移!"); ? ? ? ? }else{ ? ? ? ? ? ? $tr.fadeOut().fadeIn(); ? ? ? ? ? ? //交換后臺數(shù)組數(shù)據(jù) ? ? ? ? ? ? var array = $('#QuotaVerTable').bootstrapTable('getData'); ? ? ? ? ? ? //行在table中的位置 ? ? ? ? ? ? var idx = $tr.index(); ? ? ? ? ? ? //交換元素 ? ? ? ? ? ? var temp = array[idx]; ? ? ? ? ? ? array[idx] = array[idx - 1]; ? ? ? ? ? ? array[idx - 1] = temp; ? ? ? ? ? ? $tr.prev().before($tr); ? ? ? ? } ? ? }, ? ? 'click .down': function (e, value, row, index) { ? ? ? ? //點擊下移 ? ? ? ? var $tr = $(this).parents("tr"); ? ? ? ? //獲取table所有數(shù)據(jù)行 ?QuotaVerTable跟html頁面的table id對應(yīng) ? ? ? ? var len = $('#QuotaVerTable').bootstrapTable('getData').length; ? ? ? ? if ($tr.index() == len - 1) { ? ? ? ? ? ? Feng.success("尾行數(shù)據(jù)不可下移!"); ? ? ? ? }else { ? ? ? ? ? ? $tr.fadeOut().fadeIn(); ? ? ? ? ? ? //交換后臺數(shù)組數(shù)據(jù) ? ? ? ? ? ? var array = $('#QuotaVerTable').bootstrapTable('getData'); ? ? ? ? ? ? //行在table中的位置 ? ? ? ? ? ? var idx = $tr.index(); ? ? ? ? ? ? //交換元素 ? ? ? ? ? ? var temp = array[idx]; ? ? ? ? ? ? array[idx] = array[idx + 1]; ? ? ? ? ? ? array[idx + 1] = temp; ? ? ? ? ? ? $tr.next().after($tr); ? ? ? ? } ? ? } }
在實現(xiàn)上移下移的同時,做了數(shù)據(jù)的順序交換。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery UI Grid 模態(tài)框中的表格實例代碼
這篇文章主要介紹了jQuery UI Grid 模態(tài)框中的表格實例代碼講解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-04-04Jquery工作常用實例 使用AJAX使網(wǎng)頁進行異步更新
AJAX 通過在后臺與服務(wù)器交換少量數(shù)據(jù)的方式,允許網(wǎng)頁進行異步更新。這意味著有可能在不重載整個頁面的情況下,對網(wǎng)頁的一部分進行更新。2011-07-07Jquery ajax不能解析json對象,報Invalid JSON錯誤的原因和解決方法
我們知道Invalid JSON錯誤導(dǎo)致的json對象不能解析,一般都是服務(wù)器返回的json字符串的語法有錯誤。這種情況下,我們只需要仔細的檢查一下json就可以解決問題。2010-03-03Jquery設(shè)置attr的disabled屬性控制某行顯示或者隱藏
這篇文章主要與大家分享Jquery設(shè)置attr的disabled屬性控制某行顯示或者隱藏的具體實現(xiàn),喜歡的朋友可以參考下2014-09-09jqGrid 學(xué)習(xí)筆記整理——進階篇(一 )
這篇文章主要介紹了jqGrid 學(xué)習(xí)筆記整理——進階篇(一 )的相關(guān)資料,需要的朋友可以參考下2016-04-04lyhucSelect基于Jquery的Select數(shù)據(jù)聯(lián)動插件
lyhucSelect基于Jquery的Select數(shù)據(jù)聯(lián)動插件,需要的朋友可以參考下。2011-03-03