我正在尝试在表内实现一个内联按钮,该按钮立即更改/将今天的日期写入数据库。单击按钮“更改”应在数据库中写入实际日期。
我已经找到了一种创建内联按钮以更改数据库内部的布尔值的方法。例如,要激活/停用产品。
现在,我几乎只需要一个事实,就是当我按下按钮时,它应该在数据库中写入今天的日期。
创建按钮
function doCustomRenderColumn($fieldName, $fieldData, $rowData, &$customText, &$handled)
{
if ($fieldName == 'active') {
$dataAttributes = sprintf('data-id="%s" data-active="%s"', $rowData['id'], $fieldData);
$customText = '<span class="product-info" style="display: none;" ' . $dataAttributes. '></span>' . $customText;
$customText .= '<button class="btn btn-default inline-button" style="margin-left: 25px;">Change</button>';
$handled = true;
}
}
处理参数并执行查询
function DoPrepare() {
if (GetApplication()->IsGETValueSet('id') && GetApplication()->IsGETValueSet('active')) {
$id = GetApplication()->GetGETValue('id');
$active = GetApplication()->GetGETValue('active');
$sql = "UPDATE product SET active=$active WHERE id=$id";
$this->GetConnection()->ExecSQL($sql);
echo json_encode(array("active" => $active));
exit;
}
处理按钮单击并调用AJAX
// OnAfterPageLoad event body
function prepareInlineButtons() {
$('button.inline-button').click(function() {
var self = $(this);
var checkboxControl = self.siblings('.pg-row-checkbox');
var productId = self.siblings('.product-info').data('id');
var activity = self.siblings('.product-info').data('active');
$.getJSON(location.href, {id: productId, active: activity == 1 ? 0 : 1}, function (data) {
self.siblings('.product-info').data('active', data.active);
if (data.active == 1) {
checkboxControl.addClass('checked')
}
else {
checkboxControl.removeClass('checked')
}
})
})
}
prepareInlineButtons();