Set value to field and post it for editing to server...?

Set value to field and post it for editing to server...?

ztevieztevie Posts: 101Questions: 23Answers: 5
edited May 2017 in Free community support

I'm totally stumped, can't get any further.
I have a field that I need to be able to edit. This field is a true/false in a join table so it seems I need to get the primary id of that table sent along with it.
The id is in the Inst Field on the server side, set(false). I've included it in the editor on the client side. I don't use a form, bubble or inline. I rendered the column to show a icon, and when clicked on, it will jsut change to the other boolean value. Look at this code below, the "redrill" field will be sent as parameter, the id will not, no matter what I do. The redrill field works as intended cause if I set the client Editor to submit: "all", it will change as expected. The problem is I need to have it set to changed, since there are other fields that can only be edited when they are clicked.
The short question is: Why won't the holes.hole_id be submitted in the same way as holes.hole_redrill is? I've even tried to hardcode a value in data-id-val below, but nothing... What am I not seeing, or are there other ways to get the id sent along?
THe parameters sent are:
action:edit
data[row_2][holes][hole_redrill]:1
I would need also data[row_2][holes][hole_id]: 'some value'

hEditor = new $.fn.dataTable.Editor( {
            ajax: {
                url: "../_includes/process_shiftholes.php",
                type: "POST",
                data: {sid: shiftActive}
            },
            table: "#tbl-shiftholes",
            formOptions: {
                main: {
                    submit: 'changed'
                }
            },
            fields: [ {
                    label: "#",
                    name:  "holes.hole_nr"
                },
                {
                    label: "Salva:",
                    name:  "blasts.blast_name"
                },
                {
                    label: "Uppborrning:",
                    name:  "holes.hole_redrill"
                },
                {
                    label: "Hålid:",
                    name:  "holes.hole_id",
                    type: "hidden"
                }
            ]
        } );
        
        $('#tbl-shiftholes').on( 'click', 'a.hRedrill', function () {
        hEditor
            .edit( this.parentNode, null, null, false )
            .set('holes.hole_id', $(this).attr('data-id-val'))
            .set('holes.hole_redrill', $(this).attr('data-red-val'))
            
            .submit();
        } );

//And in the table, rendering columns:
{ data: null,
                    render: function(data, type, row){
                        if(data.holes.hole_redrill === "1"){
                            return '<a class="hRedrill" data-id-val="'+data.holes.hole_id+'" data-red-val="0" style="cursor:pointer;"><i class="fa fa-circle-o" style="color:red;"></i></a>';
                        }
                        else{
                            holeid = data.holes.hole_id;
                            return '<a class="hRedrill" data-id-val="'+data.holes.hole_id+'" data-red-val="1" style="cursor:pointer;"><i class="fa fa-circle-o" style="color:#c0c0c0;"></i></a>';
                        }
                    },
                    responsivePriority: 4,
                    className: "all"
                },

This question has an accepted answers - jump to answer

Answers

  • ztevieztevie Posts: 101Questions: 23Answers: 5

    I guess the reason for this is that I have the editor set to submit: "changed" so my method will only send those fields that are actually changed.
    I will have problems with other fields if I set it to all or allIfChanged though...
    I have another field that will change status of the whole row, and need to check that for being clicked, so thought I would check with if(isset) in preEdit on the server side. That's why I need to only submit changed.
    Is there no way to attach an id of a joined row together with other values when editor is set to changed?

  • allanallan Posts: 63,892Questions: 1Answers: 10,530 Site admin
    Answer ✓

    I guess the reason for this is that I have the editor set to submit: "changed" so my method will only send those fields that are actually changed.

    Spot on. Since the id isn't being changed, then it won't be submitted since the options tell it not to.

    What you could do if you don't want to use allIfChanged is use preSubmit or ajax.data to modify the data being sent to the server-side. Check if the redrill parameter is being sent, and if so then get the value from the id field and attach that to the data object being submitted.

    Allan

  • ztevieztevie Posts: 101Questions: 23Answers: 5

    Ok, I'll try to find a way. I was trying to add the id in the ajax data, I already have other additional data there. But the problem is I couldn't work out how to get the rowid, to make it look like data[rowid][holes][hole_id].
    I guess another option is to send the hole_id as a normal varaiable in ajax data, and then try to do the update on the server side instead on preEdit?

  • ztevieztevie Posts: 101Questions: 23Answers: 5
    edited May 2017

    Sorry, I can't get my head around how I'm supposed to fix this.
    I gave up on trying to make the client side send the variable wrapped in the right format ( data["rowid"]["holes"]["hole_id"]: "id".)
    I instead tried to send the hole_id as a parameter "hid" to the server. It's getting sent, but what can I do with it on the server side? If I receive it in preEdit, how can I make the server side editor understand I want to use this id to update the holes table? I tried to do this below, but of course it's not that simple...
    How can I create a field ["holes"]["hole_id"] in $values and assign it the post value?
    Or do I need to make a sql update in the preEdit function? Then I'd also need to send the value it should get...

    ->on( 'preEdit', function ( $editor, $id, $values ) {
            if(isset($values["holes"]["hole_redrill"])){
                $values["holes"]["hole_id"] = $_POST["hid"];
            }
        })
    
  • ztevieztevie Posts: 101Questions: 23Answers: 5

    Forget it, I was so into the complexity so the obvious was:

    ->on( 'preEdit', function ( $editor, $id, $values ) {
            if(isset($values["holes"]["hole_redrill"])){
                $editor
                    ->field('holes.hole_id')
                    ->setValue($_POST["hid"]);
            }
        })
    

    Did you mean something like this or was there a way doing it oin the client side without the need for preEdit on the server?
    This works anyway...

  • allanallan Posts: 63,892Questions: 1Answers: 10,530 Site admin

    I'd actually been meaning to use the preSubmit event on the client-side to set the value directly in the object, so you wouldn't need to modify the server-side at all. But that looks fine to me as well!

    Allan

This discussion has been closed.