Concat two fields and enter results in database
Concat two fields and enter results in database
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
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
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.
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...
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.
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
androw
parameters of thecolumns.render
function.data
contains the data for the column androw
contains the data for the full row. You don't want to usetable.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:Kevin
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.