我的数据表中的每一行都有两个按钮,编辑和删除。是否可以在“删除”或“编辑”按钮上单击并获取员工的ID或行ID,并将其传递给web方法,该方法需要一个ID参数来删除数据库中的记录?
到目前为止我的jquery代码:
$(document).ready(function () {
$.support.cors = true;
$.ajax({
url: '<%=ResolveUrl("GetEmployee.aspx") %>',
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
var table = $('#datatable').dataTable({
data: data,
columns: [
{ 'data': 'Id' },
{ 'data': 'image' },
{ 'data': 'lastName' },
{ 'data': 'firstName' },
{ 'data': 'jobTitle' },
{
'data': 'StartDate',
'render': function (jsonDate) {
var date = new Date(parseInt(jsonDate.substr(6)));
var month = date.getMonth() + 1;
return date.getDate() + "/" + month + "/" + date.getFullYear();
}
},
{
'data': 'EndDate',
'render': function (jsonDate) {
var date = new Date(parseInt(jsonDate.substr(6)));
var month = date.getMonth() + 1;
return date.getDate() + "/" + month + "/" + date.getFullYear();
}
},
{
'data': null,
'render': function (data, type, row) {
return '<button id="' + row.id +'" onclick="editClick()">Edit</button>'
}
},
{
'data': null,
'render': function (data, type, row) {
return '<button id="' + row.id + '" class="dodo" onclick="deleteClick()">Delete</button>'
}
}
]
});
}
});
$('#datatable').on('click', 'button', function () {
var id = $(this).data();
//var id = table.row($(this)).data();
alert(JSON.stringify(id));
});
});
现在,当我试图抓住id时,它返回我的未定义。
我的网络方法:
[WebMethod]
public static void DeleteRecord(string id)
{
var query = "DELETE FROM employee WHERE ID=?";
OdbcConnection myConn = new OdbcConnection(myConnection);
OdbcTransaction transaction = null;
myConn.Open();
transaction = myConn.BeginTransaction();
OdbcCommand command = new OdbcCommand(query, myConn, transaction);
command.Parameters.Add("ID", OdbcType.Int).Value = id;
command.ExecuteNonQuery();
transaction.Commit();
myConn.Close();
}
谢谢!
答案 0 :(得分:5)
是的,您可以在点击按钮时获取员工的ID或行的ID。为此,您需要在定义编辑和删除按钮时执行一些小的更改。
{
'data': null,
'render': function (data, type, row) {
return '<button id="' + row.id +'" onclick="editClick(this)">Edit</button>'
}
},
{
'data': null,
'render': function (data, type, row) {
return '<button id="' + row.id + '" class="dodo" onclick="deleteClick(this)">Delete</button>'
}
}
Javascript / jQuery
function editClick (obj) {
var rowID = $(obj).attr('id');
var employeeID = $(obj).closest('tr').find('td:first').html());
}
function deleteClick (obj) {
var rowID = $(obj).attr('id');
var employeeID = $(obj).closest('tr').find('td:first').html());
}
答案 1 :(得分:2)
您应该使用.attr()
方法查找id
属性。
$('#datatable').on('click', 'button.deleteButton', function () {
var id = $(this).attr('id');
});
$('#datatable').on('click', 'button.editButton', function () {
var id = $(this).attr('id');
});
数据表
{
'data': null,
'render': function (data, type, row) {
return '<button id="' + row.id +'" class="editButton">Edit</button>'
}
},
{
'data': null,
'render': function (data, type, row) {
return '<button id="' + row.id + '" class="deleteButton" >Delete</button>'
}
}
答案 2 :(得分:2)
这是可能的我知道三种方式。
一种不好的方法:例如,为backEnd输入数据缺点短路和搜索...功能已禁用。额外的回应。
<a data-id="<?=yourQueryResult["id"]?>" onclick="javascript:foo()"></a>
function foo (){ var id = $(this)attr("data-id")}
或
<a onclick="javascript:foo(<?=yourQueryResult["id"]?>)"></a>
function foo(id){ /*deleting*/}
2关于渲染函数转到javascript方法参数行id 例子
render': function (data, type, row) {
return '<a onclick="javascript:foo('+ row.id +')"></a>'
}
3我喜欢direck jquery事件监听器以获取行数据 优点:干净的html,没有内联函数调用和所有函数相同的文件调用。
var table = $('#datatable').dataTable({...})
$('#datatable tbody').on('click', 'a.delete', function () {
var tr = $(this).closest('tr');
var row = table.row(tr);
var id = row.data().yourColumnName;
});
$('#datatable tbody').on('click', 'a.edit', function () {
var tr = $(this).closest('tr');
var row = table.row(tr);
var rowData = row.data();
});
答案 3 :(得分:0)
您应该将data-id添加到按钮中,如下所示:
'<button data-id="' + row.id + '" id="' + row.id + '" class="dodo" onclick="deleteClick()">Delete</button>'
其他按钮相同,然后点击:
var id = $(this).data('id');
这将带来你的身份
答案 4 :(得分:0)
let tot_objects = 100;
let material = new THREE.MeshStandardMaterial( {color: 0x00ff00} );
let geometry = new THREE.BoxGeometry(5, 5, 5, 4, 4, 4);
let objs = populateGroup(geometry, material, tot_objects);
//let's merge all the objects in one geometry
let mergedGeometry = new THREE.Geometry();
for (let i = 0; i < objs.length; i++){
let mesh = objs[i];
mesh.updateMatrix();
mergedGeometry.merge(mesh.geometry, mesh.matrix);
}
let bufGeometry = new THREE.BufferGeometry().fromGeometry(mergedGeometry);
let totVerticesMergedGeometry = (mergedGeometry.vertices.length ) + (mergedGeometry.faces.length * 3);
console.log(bufGeometry.attributes.position.count); // 57600
console.log(totVerticesMergedGeometry); // it returns 67400 !!!
scene.add(new THREE.Mesh(bufGeometry, material));
function populateGroup(selected_geometry, selected_material, tot_objects) {
let objects = [];
for (var i = 0; i< tot_objects; i++) {
let coord = {x:i, y:i, z:i};
let object = new THREE.Mesh(selected_geometry, selected_material);
object.position.set(coord.x, coord.y, coord.z);
object.rotateY( (90 + 40 + i * 100/tot_objects) * -Math.PI/180.0 );
objects.push(object);
}
return objects;
}