javascript - 获取jsgrid中隐藏列的值
问题描述
我正在使用jsgrid
复选框进行行选择,如下所示
$(function() {
$("#jsGrid").jsGrid({
...
headerTemplate: function() {
return $("<input>").attr("type", "checkbox").attr("id", "selectAllCheckbox");
},
itemTemplate : function(_, item) {
return $("<input>").attr("type", "checkbox").attr("class", "singleCheckbox")
.prop("checked", $.inArray(item, selectedItems) > -1)
.on("change", function () {
$(this).is(":checked") ? selectItem(item) : unselectItem(item);
});
},
}
fields :[{
{ name: "unique_id", type: "text", width: 100, visible:false },
{ name: "some_name", type: "text", width: 100},
...
]
});
$("#selectAllCheckbox").click(selectAllCheckBox);
});
下面selectAllCheckBox
给出的函数。
var selectAllCheckBox = function(item) {
selectedItems = [];
if(this.checked) { // check select status
$('.singleCheckbox').each(function() {
this.checked = true;
selectItem($(this).parent().next().text());//line 1
});
}else {
$('.singleCheckbox').each(function() {
this.checked = false;
unselectItem(item);
});
selectedItems = [];
}
}
我想收集所有选择的unique_ids
并在服务器端处理它。但是,正如unique_id
隐藏的那样,代码line 1
总是返回 的值some_name
。我怎样才能得到 的值unique_id
?
解决方案
在jsGrid
文档中,我没有找到任何有关或获取隐藏列的方法,但您可以像下面手动实现一样执行以获取选定的记录/ID。
我已经将对象selections
内部的自定义配置jsGrid
作为数组存储到其中。
*我正在将特定记录的完整对象推selections
入jsGrid
.
演示
$("#jsGrid").jsGrid({
width: "100%",
paging: true,
selections: [],
data: [{
ClientId: 1,
Client: "Aaaa Joseph"
}, {
ClientId: 2,
Client: "Zzzz Brad"
}, {
ClientId: 3,
Client: "Mr Nice Guy"
}],
fields: [{
headerTemplate: function() {
var grid = this._grid;
return `<input class="all" type="checkbox" ${grid.selections.length==grid.data.length?'checked':''} />`;
},
itemTemplate: function(_, item) {
return `<input class="single" value=${item.ClientId} type="checkbox" _isall=${false} ${this._grid.selections.some(ClientId=>ClientId==item.ClientId)?'checked':''} />`
}
}, {
type: "text",
width: 80,
name: 'Client'
}]
});
$('#jsGrid input[type=checkbox]').on('change', function() {
var checkbox = $(this),
grid = checkbox.closest('#jsGrid').data().JSGrid;
if (checkbox.attr('class') == 'all') {
let checked = checkbox.is(":checked");
grid.selections = checked ? grid.data : [];
$.each($('#jsGrid input[class=single]'), (i, el) => $(el).prop('checked', checked))
} else {
if (checkbox.is(":checked")) {
grid.selections.push(grid.data.find(({
ClientId
}) => ClientId == checkbox.val()));
} else {
grid.selections = grid.selections.filter(({
ClientId
}) => ClientId != checkbox.val());
}
checkbox.closest('#jsGrid').find('input.all').prop('checked', grid.selections.length == grid.data.length);
}
});
$('button.getrecord').on('click', function() {
console.clear();
console.log($('#jsGrid').data().JSGrid.selections);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.css" />
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid-theme.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.js"></script>
<button class="getrecord">Get Selected record</button>
<div id="jsGrid"></div>
推荐阅读
- apache-spark - 遍历多个文件夹中的多个文件,如果名称与条件匹配,则将所有文件加载到 DF 中
- google-cloud-platform - Google Cloud Engine: I've attached 15 certificates(the maximum allowed) to a load balancer. How do I scale from there?
- java - 任务 ':compileJava' 执行失败。- 本系列第 3 期
- python-3.x - 解决“计数变化”的递归记忆解决方案
- leaflet - 如何从 WMS 中的leafletjs 中过滤所需数据
- javascript - 来自 Javascript 的 WebAssembly
- pandas - 在测试数据集上使用 pd.get_dummies 时出现错误“'DataFrame' 对象是可变的,因此它们不能被散列”
- java - 如何覆盖 compareto 方法来订购员工的薪水?
- mysql - 在 mysql 表中存储大量行(可能超过十亿)对于搜索引擎有什么影响?
- javascript - 如何在页面加载和自动点击时获取警报消息内容