Jquery+bootstrap實(shí)現(xiàn)表格行置頂置底上移下移操作詳解
最近接到產(chǎn)品的一個(gè)需求,它是要對(duì)數(shù)據(jù)排序,實(shí)際操作中我們要實(shí)現(xiàn)表格行置頂置底上移下移操作。項(xiàng)目框架是GUNS框架。
如下圖:
我是怎么用Jquery+bootstrap進(jìn)行實(shí)現(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">報(bào)表管理</a>>><a href="" onclick="getHrefUrl(this)">報(bào)表版本</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寫(xiě)法稍有不同。
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) { ? ? ? ? //點(diǎn)擊上移 ? ? ? ? var $tr = $(this).parents("tr"); ? ? ? ? if ($tr.index() == 0){ ? ? ? ? ? ? Feng.success("首行數(shù)據(jù)不可上移!"); ? ? ? ? }else{ ? ? ? ? ? ? $tr.fadeOut().fadeIn(); ? ? ? ? ? ? //交換后臺(tái)數(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) { ? ? ? ? //點(diǎn)擊下移 ? ? ? ? var $tr = $(this).parents("tr"); ? ? ? ? //獲取table所有數(shù)據(jù)行 ?QuotaVerTable跟html頁(yè)面的table id對(duì)應(yīng) ? ? ? ? var len = $('#QuotaVerTable').bootstrapTable('getData').length; ? ? ? ? if ($tr.index() == len - 1) { ? ? ? ? ? ? Feng.success("尾行數(shù)據(jù)不可下移!"); ? ? ? ? }else { ? ? ? ? ? ? $tr.fadeOut().fadeIn(); ? ? ? ? ? ? //交換后臺(tái)數(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); ? ? ? ? } ? ? } }
在實(shí)現(xiàn)上移下移的同時(shí),做了數(shù)據(jù)的順序交換。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery UI Grid 模態(tài)框中的表格實(shí)例代碼
這篇文章主要介紹了jQuery UI Grid 模態(tài)框中的表格實(shí)例代碼講解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-04-04Jquery工作常用實(shí)例 使用AJAX使網(wǎng)頁(yè)進(jìn)行異步更新
AJAX 通過(guò)在后臺(tái)與服務(wù)器交換少量數(shù)據(jù)的方式,允許網(wǎng)頁(yè)進(jìn)行異步更新。這意味著有可能在不重載整個(gè)頁(yè)面的情況下,對(duì)網(wǎng)頁(yè)的一部分進(jìn)行更新。2011-07-07jquery刪除table當(dāng)前行的實(shí)例代碼
下面小編就為大家?guī)?lái)一篇jquery刪除table當(dāng)前行的實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10Jquery ajax不能解析json對(duì)象,報(bào)Invalid JSON錯(cuò)誤的原因和解決方法
我們知道Invalid JSON錯(cuò)誤導(dǎo)致的json對(duì)象不能解析,一般都是服務(wù)器返回的json字符串的語(yǔ)法有錯(cuò)誤。這種情況下,我們只需要仔細(xì)的檢查一下json就可以解決問(wèn)題。2010-03-03Jquery設(shè)置attr的disabled屬性控制某行顯示或者隱藏
這篇文章主要與大家分享Jquery設(shè)置attr的disabled屬性控制某行顯示或者隱藏的具體實(shí)現(xiàn),喜歡的朋友可以參考下2014-09-09jqGrid 學(xué)習(xí)筆記整理——進(jìn)階篇(一 )
這篇文章主要介紹了jqGrid 學(xué)習(xí)筆記整理——進(jìn)階篇(一 )的相關(guān)資料,需要的朋友可以參考下2016-04-04jquery 多行滾動(dòng)代碼(附詳細(xì)解釋)
在網(wǎng)上可以隨處找到這段代碼,但是沒(méi)有任何人解釋這段代碼,只要自己研究好久。2010-06-06lyhucSelect基于Jquery的Select數(shù)據(jù)聯(lián)動(dòng)插件
lyhucSelect基于Jquery的Select數(shù)據(jù)聯(lián)動(dòng)插件,需要的朋友可以參考下。2011-03-03