【问题标题】:InlineButton should change value (date) in databaseInlineButton 应该更改数据库中的值(日期)
【发布时间】:2020-05-02 16:20:45
【问题描述】:

我试图在表中实现一个内联按钮,它会立即更改/写入数据库中的今天日期。 单击“更改”按钮应在数据库中写入实际日期。

我已经找到了一种方法来创建一个内联按钮来更改数据库中的布尔值。例如激活/停用产品。

现在我几乎只需要相同的事实,即当我按下按钮时,它应该在数据库中写入今天的日期。

创建按钮

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();

【问题讨论】:

    标签: php jquery mysql json ajax


    【解决方案1】:

    您使用click(function(),但这不起作用。 这仅适用于加载时已在屏幕上的对象。

    尝试使用.on() jquery 函数,如$('button.inline-button').on('click', function()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 2017-03-06
      相关资源
      最近更新 更多