In Editable when a Cell is edited, edit automatically another Cell .

In Editable when a Cell is edited, edit automatically another Cell .

iamneoxiamneox Posts: 4Questions: 1Answers: 0
edited August 2015 in Free community support

Hi all!

Im new in datatables and I have a question, I hope you can show me the way .
((And sorry for my bad english , I'm going to try to explain me as good as I can )

The case is:

I have an Editable with some columns , where the user will insert different numbers.
When the number is inserted , I would like to calculate automatically the value of the other cell.

For example :
If we put "1000"" in the row[5] , i would like to set automatically the row[6] with 1000 * 10 = 10.000.

Note: I will inform by php the cost "10" in order to calculate the result.

So, informing the user a single field, two cells are updated updated in the table; one manually and another one automatically.

Best .

This question has an accepted answers - jump to answer

Answers

  • jtoler5jtoler5 Posts: 88Questions: 32Answers: 3

    In editor 1.4 this was easy with setFormatter on the PHP side as it submitted the whole row. Trying to figure this out w/ 1.5 myself.

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin
    Answer ✓

    Where do you want the calculation to occur?

    • Before the data is submitted to the server
    • Before it is written to the database
    • After it is written to the database and before returning to the client
    • On return to the client, before updating the DataTable
    • At the point of updating the DataTable

    My suggestion for generated values is normally to go with the last option and use the columns.render option.

    @jtoler5 - Yes, Editor's inline editing only submits the field in question now I'm afraid. I did look into submitting the whole form, but the internal mechanics mean that fields are now placed into an editing state individually, and it got really messy with inline editing. That isn't to say that I won't put it back if this turns out to be a mistake.

    However, you could use the preSubmit event to add the row's information to the submitted data. For example:

    $('#myTable').on( 'click', 'td', function () {
      var tr = this.parentNode;
    
      editor.inline( this );
      editor.one( 'preSubmit', function ( e, data ) {
        data.rowData = table.row( tr ).data();
      } );
    } );
    

    I know that's a bit messy and I will review this.

    Regards,
    Allan

  • jtoler5jtoler5 Posts: 88Questions: 32Answers: 3

    I don't mean to 'highjack' this thread but... Allan, I love the new Editor and really want to have it working the best way. So my last question would be, is that the best way to do what I am trying to achieve? For example: I used to have a setFormatter that would append a 2 digit number (year) onto certain columns. So I would get the FY variable from the $data row in the formatter. Is there a better way to do this with 1.5 now?

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin

    What is the FY variable? Could you show me the code you were using for 1.4?

    @iamneox - Reply back with any question you have in this thread as well - we'll just mix the two conversations for this one :-). Keep me on my toes!

    Allan

  • jtoler5jtoler5 Posts: 88Questions: 32Answers: 3

    FY = Fiscal Year

    What I had in 1.4:

    Field::inst('charges.FBFY')
                    ->setFormatter( function ( $val, $data, $opts ) {
                        if($val !== ' ' || $val != null) {
                            return $data['charges']['Fund'].$data['charges']['FY'];
                        }
                    }),
    

    I hate this database, but I did not design this database so I am limited to working with what is already in use. Fund number can change from year to year so each Fund number has the FY appended to it (FBFY).

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin

    I see yes. Unfortunately, in 1.5.0 that would require a database lookup, or using preSubmit to add the additional data as I described above.

    I will look at this again for 1.5.1!

    Thanks,
    Allan

  • iamneoxiamneox Posts: 4Questions: 1Answers: 0

    Hi again!

    Sorry for my very very late reply, I was on vacation.

    Sorry @allan but I'm too newbie and I don't understand exactly how the "render" works.

    I tried to do this very simple thing:
    When I just edit one cell, I would like edit automatically another cell with a static string.

    Like this :

        columns: [
            { data: "stats.id_stats" },
            { data: "bases.name", editField: "stats.id_base" },
            { data: "stats.tg_name" },
            { data: "stats.name_campaign" },
            { data: "stats.subject" },
            { data: "stats.date_send" },
            { data: "stats.status" },
            { data: "stats.amount" },
            { data: "stats.delivered" },
            { data: "stats.comment" , render : 'anystring'   }
                  ],
    

    But it doesn't work... Could you explain me like I'm 5 ?

    Thanks in advance.
    Best regards,

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin

    columns.render should be a Javascript function (normally) that will transform data from one from to another. You generally will not want to use a string as its value. The columns.render documentation has further information and examples.

    Allan

  • iamneoxiamneox Posts: 4Questions: 1Answers: 0
    edited September 2015

    @allan , thanks for your fast reply. I hope you are doing well.

    Following your suggestion I'm trying to concatenate using the function like this :
    It doesn't work, but there are no errors either.

                                {
                label: "comment",
                name: "stats.comment", 
                render:  function ( data, type, row ) {
                    return data +'thismystring';
                },
                                }
    

    Thanks!

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin

    Is that an Editor field definition? There is no fields.render method. As the documentation for columns.render shows, it is a DataTables column option.

    Allan

  • iamneoxiamneox Posts: 4Questions: 1Answers: 0
    edited September 2015

    Omg sorry it was a mistake, you are right x_X.
    Now I made the changes in the columns (not in the Editor field definition)

            { data: "stats.comment" ,
              render:  function ( data, type, row ) {
              //console.log(row);
              return data+'hola';
               },
            }
    

    And it works ^^.

    My problem now is to know how to make it work with other rows.
    I though I could do it using the Object row[x] like in this example : https://datatables.net/examples/advanced_init/column_render.html
    But I'm doing something wrong.

    Also another thing that concerns me is how to edit the field. I mean, for example If I have a field with the word "Good" and using the render I concatenate it with "Bye" I would like when I will push the cell to edit it shows "GoodBye" not only "Good".
    For sure, I will make a control in order to render runs only once.

    A lot of thanks @allan !!
    Best

This discussion has been closed.