Custom field type: how to get current editing row data?

Custom field type: how to get current editing row data?

marcelvmarcelv Posts: 27Questions: 6Answers: 0
edited June 2015 in Editor

Hi,

A figurative example of my situation: my server returns data like this:
"data":[{"DT_RowId":"8666","var1":"example","var2":"example"}]

Now I have setup datatables which just shows var1 in a column. I also have setup a custom field type plugin for the Datatables Editor.

On initializing of the field type I create 2 inputs, one input where the user can enter data and the other is a read only input.

So, here`s what I want:

  • in the first input of my custom field type I want to show the value of the editing cell > this works by using the val param in the set function inside my field type plugin.
  • in the second input of my custom field type I want to show another value of the data returned by the server belonging to this row. So to take the above data as an example, I want to show var2 in the second input field.

The question is, how to retrieve this value on either initializing the field or within the set function of the field type plugin? I have been trying to get values like this this.get('var1') within the set function but it does not seem to work. It does seem to work for a few of the fields, but not for the fields I must retrieve.

I hope you understand what I`m looking for. Basically, the question I'm asking is: how to access the row data of the cell i'm editing / other cells data within the same row from my custom field type plugin?

Thanks!

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Unfortunately I don't think this is possible at the moment in Editor. Each field has its own unique value and does have access to the rest of the row (intentionally as it could add a lot of complexity).

    I'll have a think about it, but I don't think that is directly possible via a plug-in.

    Allan

  • marcelvmarcelv Posts: 27Questions: 6Answers: 0

    Hi Allan,

    Thanks again for the quick help.

    Too bad this is not possible. I could think of a whole lot more cases where I`d want something like this. For example, if you have a todo list app and you want to give the 'description' column a color based on the 'status' value. I can never do this if I cannot access the values of other columns.

    Let me know if you think of anything that might help me on this :)

    Thanks!

  • marcelvmarcelv Posts: 27Questions: 6Answers: 0
    edited June 2015

    BTW, I'm trying to think of other solutions now and do you maybe have an idea about this:

    $jq(tableId).on( 'click', 'tbody td:not(.noedit)', function (e,ref) {
                editor.inline( this );
    });
    

    In the above code I call the editor. Is there any way to pass data to the plugin from this call? For example by appending the data to the node (this) or something like that?

    If that's possible then I could retrieve the data from a hidden div or something which I created in the column render function (because in the render function I am able to access the data like data.var1). The I could pass the value and I'm done, but I`m not sure if I can pass anything at all.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    if you have a todo list app and you want to give the 'description' column a color based on the 'status' value. I can never do this if I cannot access the values of other columns.

    Certainly you can do this in the DataTable - use columns.data, rowCallback and various other options. It is the Editor field that does not have access to the rest of the row's data - only its own data point.

    Having said that, there is no reason why you couldn't externally access the row's data and update the display in the form. Use a change event or similar and find the row that is being edited in the DataTable to get the data - there is just no way to do this in a field plug-in.

    Is there any way to pass data to the plugin from this call?

    Yes, you could create a field specific method - you do that simply by defining the function you want in your plug-in object. The first parameter for the function will always be the field's configuration object. The following parameters are whatever you want to pass in.

    So you might have:

    myFieldMethod: function ( conf, data ) {
       ...
    }
    

    and immediately after you call editor.inline( this ) do:

    editor.field( 'myField' ).myFieldMethod( table.row( this.parentNode ).data() );
    

    Allan

  • marcelvmarcelv Posts: 27Questions: 6Answers: 0

    Hi Allen,

    Thanks again for your quick & great help.

    You are right about the todo list thing, that is certainly possible because I can indeed access row data from the render function or callback.

    I think the custom function in the plugin is what I am looking for, I'll give that a try. Thanks again!

This discussion has been closed.