Update other table when button pressed

Update other table when button pressed

milapmilap Posts: 40Questions: 13Answers: 2

Hello,
Is there any other way to update other table record using custom button (for instance setting value 1 when pressed) than direct ajax call from button it self?
Probably it could be made using postEdit but how to pass an event there using a button action?
Using kind of a dummy form field?

Answers

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    The answer is probably :smiley:

    Please describe in more detail what you are wanting to do.

    Is the button a custom button using the Buttons Extension or a button in the table rows. You can certainly use a jQuery ajax call from a button to send data to your server to update the DB. In the success function you can use ajax.reload() to reload the table with the updated data.

    You mention postEdit. Are you trying to do this in conjunction with doing an editor update?

    Kevin

  • colincolin Posts: 15,177Questions: 1Answers: 2,590

    You certainly can update a second table with a button press, but I'm not clear of your flow, as you're not discussing updating the first table.

    This example from this thread may help. It's using submitComplete to add additional records to the same table. That could just as easily be an Editor instance in a different table.

    Hopefully that will point you in the right direction. If not, as Kevin said, please can you supply more information.

    Colin

  • milapmilap Posts: 40Questions: 13Answers: 2

    Thanks for response.
    In my case I have table A that is related with data in datatables
    In the same time I have table B with row (I know in advance which one) that I need to update when my custom button is pressed.

    I don't need to do anything with table A when the mentioned button is pressed.

    I know that I can do sth similar to that:

    {
        text: 'BUTTON',
        action: function(e, dt, node, config) {
          $.ajax({
              "url": "/php/set-row-to-one.php>",
              "type": "GET",
              "datatype": 'json',
              "success": function (data) {
              }
          });
        }
    }
    

    I am just thinking Is it possible to achieve same goal just by ajax **editor **call but maybe it is pointless since I am not updating data in table A...

    I am thinking about that because I want to implement button status (text color red when value in table B is 1 and black when it is 0) and in that case I need make first ajax button call when page is loaded (to get table B value), and then second one when it is pressed (to update value) and change color.

    I am curious is it possible to achieve same goal by not implementing 2 calls (1st is getting data, 2nd is setting) in button it self and just use editor somehow...

    But like I said maybe it is pointless... ;)

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    Do you have the Editor configured for Table B? If so you can use the edit() API to make updates.

    If you don't have Editor configured for Table B then you will need to use ajax to send the updates for Table B.

    You can use the button().node() to update the button's color.

    Kevin

  • milapmilap Posts: 40Questions: 13Answers: 2

    I know that I can bind two tables in editor using for instance LEFT JOIN.
    I should make edit() on left joined tables then?

  • colincolin Posts: 15,177Questions: 1Answers: 2,590

    I think we might be talking cross purposes here - when you say second table, are you referring to a second table in the browser, or a second database table on the server? I thought you meant browser! If you mean the server, this page may help,

    Colin

  • milapmilap Posts: 40Questions: 13Answers: 2
    edited September 2021

    Second db table on the server.

    I am trying to find a way to use a datatables editor rather than writing my own simple ajax php script - but maybe this is wrong approach, I just want to use postCreate, postEdit, postRemove.

    Maybe I will explain You what I want to achieve on an example:

    I have table Customer with super simple two column structure: id (int), user_name (varchar)
    I also have second table Changes with 4 columns id (int), time (date_time), updated (bool), table_name (varchar)

    Table Customer is related with datatables.
    When I am adding, editing, deleting records I am interacting with Customer

    Column table_name in table Changes is related with table names in my db.
    In my example since I am having only one table (except Changes) I will have only one row:
    id, time, updated, table_name
    1; '2021-09-04 16:00:00', 0, 'Customer'

    Now I want to create an event on postCreate, postEdit, postRemove that updates value Update (to 1) in table Changes (each time there are changes in Customer) so I am having a code

        ->on( 'postCreate', function ( $editor, &$id, &$values ) {
            $editor->db()
                ->query( 'update', 'Changes' )
                ->set( '_updated_', 1, false )
                ->where( 'table_name', 'Customer' )
                ->exec();
        } )
    

    Same code is for postEdit and postRemove

    Now I want to create a button that

    1) is changing color to red if column updated in table Changes is set to 1 (WHERE table_name = 'Customer').

    2) when button is pressed value updated is updated to 0 (WHERE table_name = 'Customer') AND time is updated to NOW() AND button is changing color back to black (since updated is now 0) - and that is my problem.

    This idea informs my system user that there are some new updates in the table (button red color) and he needs to press the button to inform some 3rd party system that it need to reload data from the Customer table (that system watches time value). So it is kind of a manual execute signal for external system.

    I know that I can leftJoin table and I know how to do that (I've done it before)
    But Can I update a value in leftJoined table (Changes) using editor that is set for other table (in my case Customer) after button is pressed?

  • milapmilap Posts: 40Questions: 13Answers: 2
  • colincolin Posts: 15,177Questions: 1Answers: 2,590

    Excellent, let us know if you have problems,

    Colin

Sign In or Register to comment.