Button deletes other columns values

Button deletes other columns values

joseoliveiraborlasjoseoliveiraborlas Posts: 80Questions: 8Answers: 3
edited March 2023 in Free community support

Link to test case: https://live.datatables.net/mimifosi/1/edit ( I tried to provide a test case, but for some reason it's giving me an error on line 7!? Can you tell me what is wrong with the test case? ) Page: https://bit.ly/423kGvi
Error messages shown: None
Description of problem: I have a button to toggle the value of a column in the select row. But when I click on it, the other columns are changed as well. the "Concluir" button, make the change i want, but also deletes the value on "Marca" column. :neutral:

Replies

  • kthorngrenkthorngren Posts: 19,915Questions: 26Answers: 4,696

    You are getting two errors:

    Uncaught Cannot extend unknown button type: selected

    You need to load the select extension.

    Failed to load resource: the server responded with a status of 404 ()

    Use the browser's network inspector to see the ajax response, which is:

    {
      "error": "Could not find bin with ID \"includes\" and revision table.sells.php"
    }
    

    the "Concluir" button, make the change i want, but also deletes the value on "Marca" column

    Use the browser's network inspector tool to see the request and response when when clicking this button. Please post both the request and response. I suspect the response will have the changed Marca data which means the server script will need debugging to determine why the value is changing if the change is not expected.

    Kevin

  • joseoliveiraborlasjoseoliveiraborlas Posts: 80Questions: 8Answers: 3
    edited March 2023

    I didn't remember that I could use the browser's console to check for errors. So it was easy to fix: https://live.datatables.net/mimifosi/2/edit

    Yes, the response is empy the "Marca" field:
    Payload:

    data[row_147][marca]: 
    data[row_147][estado]: Concluído
    action: edit
    

    Response:

        {
          "data": [
            {
              "DT_RowId": "row_147",
              "estado": "Concluído",
              "marca": ""
            }
          ]
        }
    

    How to make the php lib detect this situation and not edit the other fields?
    The code is the example given on generator:

        (...)
    $edi = Editor::inst( $db, 'sells', 'id' )
             ->fields(
                  Field::inst( 'estado' ),
                  Field::inst( 'marca' )
             );
        $edi->process( $_POST )
             ->json();
    
  • allanallan Posts: 61,005Questions: 1Answers: 9,949 Site admin

    Just to confirm - are you saying that it is fixed now?

    Allan

  • joseoliveiraborlasjoseoliveiraborlas Posts: 80Questions: 8Answers: 3
    edited March 2023

    Hi,
    No.
    I was just replying to see if the problem was there.
    But I discovered a new situation. When the button is clicked 1 time, it clears the other input values ​​filled with whit Select2. But if you press the button again, it restores the data again (if you don't refresh the page).

    And if select one row and click the button, when select other row and click the button, the value puts the same as first row.

    this seems to me to be any command that should be executed before the button makes changes, but I've wasted so much time I don't even know where to look anymore.
    Can you help me to find out the problem?

  • allanallan Posts: 61,005Questions: 1Answers: 9,949 Site admin

    Right - the issue with the "Marca" value going missing is that:

    editor
      .edit( table.row( { selected: true } ).index(), false )
      .set( 'estado', ('Pendente' ))
      .submit();
    

    is synchronous. But the value for the Select2 field is being Ajax got from the server and is thus asynchronous! It has no value for when the submit() happens, because the Ajax request is still happening.

    Here is a hacky way to work about it - wait for 1 second before sending submit().

    What we really need it so know when the Select2 processing is complete. I had thought that using:

                              editor.field('marca').input().one('change', function () {
                                editor.submit();
                              });
    

    would work, but it only appears to work on the first instance. I don't understand why that would be the case!

    Select2 Ajax always makes things difficult!

    Allan

  • joseoliveiraborlasjoseoliveiraborlas Posts: 80Questions: 8Answers: 3
    edited March 2023

    Ooohh yes, it really works. But if there are several inputs (select2), maybe the time will need to be longer. Or, for example, if the server takes a long time to respond, maybe the time is not enough for that. It doesn't seem viable in the long run.

    Is there a way, for example, before edit(), to copy the entire row, manipulate only the information I need (in this case the 'state') and then submit the complete row? that way avoid missing data and avoid having to wait for Select2's response? Am I thinking right? It's possible Allan?

    it seems to me that this is more or less what the editor does, right? Otherwise, it doesn't make much sense to have to re-enter all the values os the inputs, when the intention is only to edit 1.

  • joseoliveiraborlasjoseoliveiraborlas Posts: 80Questions: 8Answers: 3

    And Alan, do you have better suggestion, than works better whit Editor, like I use Select2, to be used as autocomplete directly from ajax to check db for already options in that column in db?

  • joseoliveiraborlasjoseoliveiraborlas Posts: 80Questions: 8Answers: 3

    Any news or analysis forecast? :blush:

  • allanallan Posts: 61,005Questions: 1Answers: 9,949 Site admin

    Apologies for the delay - this is one I need to actually spend some time on rather than just dashing off an answer that might or might not help. I'll hopefully be able to get back to you later today.

    Allan

  • joseoliveiraborlasjoseoliveiraborlas Posts: 80Questions: 8Answers: 3

    I will be very grateful, Allan!

  • allanallan Posts: 61,005Questions: 1Answers: 9,949 Site admin

    Right, I've been looking into this in more detail and reminding myself how Select2 operates. When we want to set a value that is not in the list of options that are currently available for the control we need to add such an option.

    I've experimented a bit and this is my current solution. There are two key changes:

    1. I've updated the plug-in for Select2 to use the field().processing() method for when it is doing making an Ajax call. This informs the submit that it should delay until the processing is finished. Note that it will cause a warning to appear on the browser's console, but you can safely ignore that.
    2. You were checking the value of the estado field before calling the edit() method, which was causing issues (it would read the previous value!).

    With those changes it looks like it is working okay.

    Personally I like breaking this sort of editing down into parent child editing also I can see the advantage of being able to just type a new tag.

    Regards,
    Allan

  • joseoliveiraborlasjoseoliveiraborlas Posts: 80Questions: 8Answers: 3

    PERFECT! Thank you Allan, you are fantastic! And thankyou Kevin!

  • joseoliveiraborlasjoseoliveiraborlas Posts: 80Questions: 8Answers: 3

    But... :( don't work whit multilple tags. When enabling multiple:true, don't send data /request.
    https://live.datatables.net/mimifosi/11/edit

  • allanallan Posts: 61,005Questions: 1Answers: 9,949 Site admin

    Is marca just a string field in the database? Or is it something more complex than that? It looks like PHP is giving an error because the data is being submitted as an array, and the database expects a string.

    I'm not clear if it should be a comma separated string, or a JS error, or something else. The data appears to have Array as a string in it at the moment, so there is something odd somewhere.

    Allan

  • joseoliveiraborlasjoseoliveiraborlas Posts: 80Questions: 8Answers: 3
    edited March 2023

    Can you try now, Allan? https://live.datatables.net/mimifosi/14/edit
    When activated the multiple:true, the lib respond whit an error:

    and reply whit an array.

Sign In or Register to comment.