我创建了一个通用函数,所以我可以在不同的页面上使用相同的代码。
我正在使用 Ajax 加载数据,当 DropDown 的值发生更改时,我会刷新我的表。当我单击一行时,值为空:
这是我的数据表函数
function dataTable() {
var self = this;
self.url = "";
self.gridObject = null;
self.Initilize = function (tableDiv, url) {
self.url = url;
self.gridObject = $("#" + tableDiv).bootgrid({
formatters: {
"commands": function (column, row) {
return "<button type=\"button\" class=\"btn btn-sm btn-success command-edit\" data-row-id=\"" + row.Id + "\"><span class=\"fa fa-pencil\"></span></button> " +
"<button type=\"button\" class=\"btn btn-sm btn-danger command-delete\" data-row-id=\"" + row.Id + "\"><span class=\"fa fa-trash-o\"></span></button>";
}
},
requestHandler: function (request) {
var model = fleetMaintenance.filterModel.GetModel();
model.Current = request.current;
model.RowCount = request.rowCount;
model.Search = request.searchPhrase;
return JSON.stringify(model);
},
ajaxSettings: {
method: "POST",
contentType: "application/json"
},
ajax: true,
url: self.url,
selection: true,
}).on("loaded.rs.jquery.bootgrid", function () {
self.gridObject.find(".command-edit").on("click", function (e) {
// e.stopPropagation();
alert("You pressed edit on row: " + $(this).data("row-id"));
}).end().find(".command-delete").on("click", function (e) {
// e.stopPropagation();
alert("You pressed delete on row: " + $(this).data("row-id"));
});
self.gridObject.on("click.rs.jquery.bootgrid", function (e, columns, row) {
console.log(row);
debugger;
});
});
},
self.RefreshTable = function () {
self.gridObject.bootgrid('reload');
}
}
HTML:
<div class="box">
<div class="box-header">
<div class="col-md-6">
<h3 class="box-title">Sites</h3>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-md-12">
<table id="sitesList" class="table table-condensed table-hover table-striped">
<thead>
<tr>
<th data-column-id="iSiteId" data-identifier="true">Id</th>
<th data-column-id="sSiteName" data-order="desc">Site Name</th>
<th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
然后我用它来创建 JQGrid 表:
<script type="text/javascript">
var tble = new dataTable();
$(function () {
tble.Initialize('sitesList', '/Sites/SitesList')
$('*[data-action="RefreshTable"]').change(function (e) {
var dropDown = $(this);
tble.RefreshTable();
});
});
</script>
当用户单击此表中的一行时,我必须重新加载另一个表:但 Row 为 NULL
注意:当我没有使用 Ajax 加载数据时,这并没有发生: