Data change before update

Data change before update

helpdeskhelpdesk Posts: 20Questions: 3Answers: 0
edited May 2015 in Free community support

Hi,
is there a chance so I can manipulate data before submitting, f ex. I have 1 field Date, second Day No which I'd like to calculate from some function. Or if someone choose value 1, I'd like to update DB with this value*$x.
Same for Table SELECT query, where can I edit it to add where clause

Thanks for your help!,
Jakub

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,356Questions: 1Answers: 10,447 Site admin
    Answer ✓

    Is this using Editor? If so, then the preSubmit method can be used to modify the data submitted to the server.

    Allan

  • helpdeskhelpdesk Posts: 20Questions: 3Answers: 0
    edited September 2015

    Hi Allan, thanks a lot for your help,
    I managed to manipulate the data but don't know how to get a current row_id.

    example:

    editor.on('preSubmit', function (e, o, action) {

    var area = editor.field('area');

    o.data.row_22.area = "test"; // this will work - but i don;t know how to access current row_id

    o.data.area = "test" ; // unfortunatelly doesnt as Im not accessing the right element.

    });

    Any suggestions? :) I'll be very grateful - Thanks Jakub

  • allanallan Posts: 63,356Questions: 1Answers: 10,447 Site admin
    Answer ✓

    Hi Jakub,

    You would need to use $.each( o.data, function ( key, value ) { ... } ); where key then the row id (row_22 in the above case). This is because of Editor multi row editing abilities.

    Thanks,
    Allan

  • helpdeskhelpdesk Posts: 20Questions: 3Answers: 0

    Thank you,
    that solved my problem :)

    Jakub

  • helpdeskhelpdesk Posts: 20Questions: 3Answers: 0
    edited September 2015

    Hi Allan,
    I'm facing a new problem now and can't find a solution :)

    I need to make a few calculations before sending data to the server, I wanted to make it with ajax call but it returning with an error. Code below:

    editor.on('preSubmit', function (e, o, action) {

                    $.each(o.data, function (key, value) {
    
    
                        $.ajax({
                            url: "pull_calc.php",
                            data: {pull_no: editor.field('pull')},
                            type: "POST",
                            success: function (infos) {
                                  value.pull = infos;
                            }
                        });
    
                    });
    
                });
    

    Error that I'm receiving in the browser is:
    Uncaught TypeError: Cannot read property 'id' of undefined

    I'll be very grateful for any tips :)

  • allanallan Posts: 63,356Questions: 1Answers: 10,447 Site admin

    Your only option to use Ajax there would be to disable the asynchronous aspect of Ajax which is very much not recommended.

    Given that you are submitting data to the server and you need the server to do the calculation, why not just have the server do the calculation on the submitted data? That would save the additional complexity on the client-side.

    Allan

  • helpdeskhelpdesk Posts: 20Questions: 3Answers: 0
    edited September 2015

    Yeah, you're right,
    I'm trying to do this way as I don't how to change this data on the server :) - where and how actually calculate all of them before query?

    Th only method i know for changing data is presubmit suggested by you ;)

  • allanallan Posts: 63,356Questions: 1Answers: 10,447 Site admin

    If you are using the Editor PHP or .NET libraries, then you could use the server-side events that were introduced in 1.5. I suspect that will be easier and ultimately more reliable.

    Allan

  • helpdeskhelpdesk Posts: 20Questions: 3Answers: 0
    edited October 2015

    Thanks for your help :)

  • helpdeskhelpdesk Posts: 20Questions: 3Answers: 0
    edited October 2015

    It's working fine, but i believe I will need one more thing to finish my project, hope you will help again :)

    I'm able to change values like this:

    ->on( 'preCreate', function ( $editor, $values ) {
            $editor
                ->field( 'week_no' )
                ->setValue( '12345' );
              } )
    

    But don't know how to access data currently sent by the editor - example, I'd like to calculate week number regarding to the date sent in particular field in the editor 'my_date'.

    So something like this,

    $calculated_week_no = date("W",my_date);
    
    ->on( 'preCreate', function ( $editor, $values ) {
            $editor
                ->field( 'week_no' )
                ->setValue($calculated_week_no );
              } )
    

    Thx
    Jakub

  • ThomDThomD Posts: 334Questions: 11Answers: 43

    I use the editor.dependent() method. It gives access to the loaded and changed values in the data set. The advantage of this over the other event items is that the dependent fields, like you calculated week number, can be displayed on the form before submission.

  • helpdeskhelpdesk Posts: 20Questions: 3Answers: 0

    But editor.dependent is a client side javascript, isn't it? I'm using php on the server side so the last necessary part is how to access data that should be submitted

  • ThomDThomD Posts: 334Questions: 11Answers: 43

    Yes, editor.dependent is client side. When you tinker with stuff client side, by the time it gets to the server, the db doesn't know how the data was manipulated. As long as you are sending all the fields (AllIfChanged), your input field and any calculated fields will get to the db.

    Now, you would be at the mercy of client side data integrity issues so that may not be an acceptable risk for your environment.

    Back to your server side question, shouldn't you be able to get all the current (submitted) values with the $values array parameter of that preCreate function?

  • helpdeskhelpdesk Posts: 20Questions: 3Answers: 0
    edited October 2015

    Hi thanks for a quick reply,
    Indeed this could solve the problem but then I'll need to add week_no to the editor itself so it will be visible on adding / editing rows and I'd like to avoid that.

    And yes $valules array has data that I need but I don't know how to address to get it.

    Data Example:

    {"data":
    [{"DT_RowId":"row_20",
    "conv_org":"Done! :)",
    "crop":"Red Gem",
    "area":"Barway",
    "farm":"Old McDonalds",
    "sup_code":"Darby Methwold",
    "pull":"10",
    "planting_area":"asdadasdast",
    "propagator":"Premier Plants",
    "plant_date":"2015-05-07",
    "day_no":"",
    "week_no":"",
    "period":"",
    "variety":"123123123123",
    "field_name":"",
    "batch_no":"283126",
    "quant_planted":"655703f",
    "quant_delivered":"69520",
    "spacing":"20",
    "del_date":"2015-05-22",
    "del_note":"E5766",
    "prog_harv_date":"",
    "prog_harv_day":"",
    "prog_week_date":"21",
    "prog_harv_days":"",
    "planned_uncover":"2015-10-22",
    "actual_uncover":"",
    "refleece":"",
    "fleece_off":""}
    ]}
    
  • ThomDThomD Posts: 334Questions: 11Answers: 43
    Answer ✓
  • helpdeskhelpdesk Posts: 20Questions: 3Answers: 0

    Thank you very much! :)

    I think now I'll be able to finally sort it out :)

    J

This discussion has been closed.