gpt4 book ai didi

jquery - 使 DataTables 中的所有单元格都可编辑

转载 作者:行者123 更新时间:2023-12-05 09:13:53 25 4
gpt4 key购买 nike

我正在使用数据表 1.10.12,我正在绘制一个简单的表格:

projectRevenue = $('#projectRevenue').DataTable({
serverSide: true,
processing: true,
scrollX: true,
stateSave: true,
ajax: {
url: "{!! route('listOfProjectsRevenueAjax',$project->id) !!}",
type: "GET",
dataType: "JSON"
},
columns: [
{ name: 'id', data: 'id' , searchable: false , visible: false },
{ name: 'year', data: 'year' , searchable: true , visible: true },
{ name: 'product_code', data: 'product_code' , searchable: true , visible: true },
{ name: 'jan', data: 'jan' , searchable: true , visible: true },
{ name: 'feb', data: 'feb' , searchable: true , visible: true },
{ name: 'mar', data: 'mar' , searchable: true , visible: true },
{ name: 'apr', data: 'apr' , searchable: true , visible: true },
{ name: 'may', data: 'may' , searchable: true , visible: true },
{ name: 'jun', data: 'jun' , searchable: true , visible: true },
{ name: 'jul', data: 'jul' , searchable: true , visible: true },
{ name: 'aug', data: 'aug' , searchable: true , visible: true },
{ name: 'sep', data: 'sep' , searchable: true , visible: true },
{ name: 'oct', data: 'oct' , searchable: true , visible: true },
{ name: 'nov', data: 'nov' , searchable: true , visible: true },
{ name: 'dec', data: 'dec' , searchable: true , visible: true },

我希望能够编辑单元格,以便它在数据库中得到更新。为此,我需要将每个 contenteditable 设置为 true,并有一个类,例如 update => 为此,我添加了 className:“update”,但我不知道如何使其内容可编辑。

最佳答案

您可以通过这种方式使 DataTable 中的所有单元格都可编辑:

const createdCell = function(cell) {
let original

cell.setAttribute('contenteditable', true)
cell.setAttribute('spellcheck', false)

cell.addEventListener('focus', function(e) {
original = e.target.textContent
})

cell.addEventListener('blur', function(e) {
if (original !== e.target.textContent) {
const row = table.row(e.target.parentElement)
row.invalidate()
console.log('Row changed: ', row.data())
}
})
}

table = $('#example').DataTable({
columnDefs: [{
targets: '_all',
createdCell: createdCell
}]
})

如您在此演示中所见 -> http://jsfiddle.net/w9hrnf63

它们的关键部分是row.invalidate()。当且仅当您正在使用 DOM 表或其他静态资源时,这会在内部更新行数据。如果您正在使用 serverSide 处理 invalidate() 只会将单元格内容重置为原始内容。因此,执行您对服务器的更新请求,而不是上面的 invalidate():

cell.addEventListener('blur', function(e) {
if (original !== e.target.textContent) {
const row = table.row(e.target.parentElement)
$.ajax({
url: '/updateScript/',
data: row.data()
})
}
})

关于jquery - 使 DataTables 中的所有单元格都可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55386847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com