Concat two fields and enter results in database

Concat two fields and enter results in database

th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

I have a first_name field and a last_name field and I would like to concatenate them into a full_name field automatically and save the full_name into the database. I could also just display the full_name in the datatable. That would work also. I have been reading about preSubmit, render, setFormatter but I'm not having any luck getting those to work. What is the best way to do this and can someone show an example code snippet or two?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @th3t1ck ,

    When you say you have a first_name and last_name, where is that? Given you want them written to the database as full_name, and displayed as full_name, I'm not clear what the process is here. If it's currently first_name/last_name in the DB, it might be worth writing some SQL to join them now, and use that going forward, if I understand the problem.

    Cheers,

    Colin

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    Yes the database has a first_name field and a last_name field. I really would just like to display the full_name in the datatable that the user sees instead of having to create yet another field in the database table.

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    So I'm thinking I can just concatenate the first_name with the last_name and display it in the table with a render function. I just don't know how to get the values from the current row for first_name and last_name.

    My datatable code looks like this...

        $('#users').DataTable( {
            dom: '<\"top\"Bfl>iprt<\"bottom\">p<\"clear\">',
            ajax: \"users-con.php\",
            columns: [
                { data: \"oesa_users.first_name\" },
                { data: \"oesa_users.last_name\" },
                { data: \"oesa_users.full_name\",
                   render: function ( data, type, row ) {
                     var data = table.rows( 0 ).data();
                     var fullname = table.rows( 2 ).data();
                     var firstname = row[2]+' '+row[3];
                     return data;
                                     }
    

    first_name and last_name have values that when concatenated would populate the full_username.
    I know it's not right but I was hoping someone could help me figure this out.

  • kthorngrenkthorngren Posts: 21,324Questions: 26Answers: 4,949
    edited December 2018 Answer ✓

    Here is an example of combining data from multiple columns in one row.
    https://datatables.net/examples/advanced_init/column_render.html

    Also you will want to understand the data and row parameters of the columns.render function. data contains the data for the column and row contains the data for the full row. You don't want to use table.rows( 0 ).data(); within the render function.

    Since you are using data objects not arrays you will access the row elements using object notation. Also since you are not returning oesa_users.full_name from the server you will need to set that column data to null. You will want something like this:

            { data: null,
               render: function ( data, type, row ) {
                 return row.oesa_users.first_name + ' ' + row.oesa_users.last_name;
            }
    

    Kevin

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    Thank you for the clarification Kevin. I've been reading the docs and looking at examples but your explanation was simple and makes sense to me now. The worked exactly the way I wanted it to.

    Thank you.

This discussion has been closed.