在Web開辟中,利用jQuery EasyUI的DataGrid組件展示表格數據是一種罕見的做法。偶然間,為了進步用戶休會或滿意特定的展示須要,我們須要在表格中暗藏某些列。本文將揭秘jQuery EasyUI中暗藏列的實用技能,幫助妳輕鬆實現表格數據管理。
暗藏列的方法
在jQuery EasyUI中,重要有兩種方法可能暗藏列:
1. 設置hidden屬性
這是一種簡單直接的方法,經由過程設置列的hidden
屬性為true
來實現列的暗藏。
$('#datagrid').datagrid({
columns: [[
{field: 'id', title: 'ID', width: 50, hidden: true},
{field: 'name', title: '姓名', width: 100},
{field: 'age', title: '年紀', width: 50}
]]
});
鄙人面的示例中,id
列將被暗藏。
2. 重寫列頭
這種方法涉及重寫EasyUI的setColumnTitle
方法,經由過程操縱DOM元從來暗藏列。
$.fn.datagrid.methods.extend({
setColumnTitle: function(jq, option) {
if (option.field) {
return jq.each(function() {
var panel = $(this).datagrid("getPanel");
var field = $(panel).find("td[field='" + option.field + "']");
if (field.length) {
var span = field.find("span").eq(0);
span.html(option.text);
}
});
}
return jq;
}
});
$('#datagrid').datagrid({
columns: [[
{field: 'id', title: 'ID', width: 50},
{field: 'name', title: '姓名', width: 100},
{field: 'age', title: '年紀', width: 50}
]],
onBeforeLoad: function(param) {
$('#datagrid').datagrid('setColumnTitle', {field: 'id', text: ''});
}
});
鄙人面的示例中,id
列的標題將被重寫為空字符串,從而實現暗藏。
靜態暗藏列
在現實利用中,我們可能須要根據前提靜態地表現或暗藏列。以下是一個靜態暗藏列的示例:
function hideColumn(field) {
$('#datagrid').datagrid('setColumnTitle', {field: field, text: ''});
}
function showColumn(field) {
$('#datagrid').datagrid('setColumnTitle', {field: field, text: '姓名'});
}
// 根據前提靜態暗藏或表現列
hideColumn('age');
鄙人面的示例中,我們定義了hideColumn
跟showColumn
函數,用於靜態暗藏或表現指定列。
總結
本文介紹了jQuery EasyUI中暗藏列的實用技能,包含設置hidden
屬性跟重寫列頭兩種方法。經由過程這些技能,妳可能輕鬆實現表格數據管理,進步用戶休會。