Count non empty cells in a row
Count non empty cells in a row
wijwij
Posts: 54Questions: 12Answers: 0
in DataTables
Hi,
I wonder if someone can help me:
When I select a row to edit it and hit "Update", I want to count **the **none empty cells in this row and update another cell value in the DT accordingly.
Also, I want this cell value to get updated automatically when the DT initialized
Any help is appreciated
This question has accepted answers - jump to:
This discussion has been closed.
Answers
One option might be to use the
postEdit
for this. You can iterate thedata
parameter to count the none empty cells. You can use theid
parameter as therow-selector
forrow().data()
to update the cell withrow().data()
.Use
rows().every()
to loop all the rows ininitComplete
. You can count the none empty values and update the proper cell.Kevin
Thanks Kevin.
Any available example?
You probably wouldn't want to use
postEdit
as the data would've been sent to the server by then.preSubmit
would be better - as you can count the empty cells (as Kevin described above) and put the value in the data being sent to the server. This example here shows how the case is changed on a field prior to submission - so you would do something like that.This sounds like it has the potential to get messy if you have many rows. It might be worth ensuring that's correct already in the DB. But if not, yep, do as Kevin described, calling
edit()
to update the rows where the count is incorrect.Colin
I agree with Colin. Storing that count in a field in the datatabse is a much better option.
Kevin
Thank you Colin & Kevin.
I will try your suggested ideas and I will update this page accordingly
Thanks
Hi,
Thanks to Kevin and Colin's answers, here is the script I used and worked for me, just in case someone may need it.
The problem I have now is how to communicate the "coun" variable value and send it back to the database? so the datatable got updated immediately.
I spent really long time trying to find an answer without success
You could add that variable to the data being sent - something like this where your
alert()
is :Then if you look at the message on the browser's network tab, you should see it being sent to the server,
Colin
I added this line right after the alert(coun), and got this error
Uncaught ReferenceError: data is not defined
Sorry, that would be
o.coun = coun
- I was taking the example frompreSubmit
,Colin
Excellent, I am getting the value of (o.coun) in the developer pane
Any idea how to replace the current value of "is_processed" with "o.coun"?
And if successful, I want the datatable to update its content automatically exactly how it did when we edit any of the fields inside the editor
Many many thanks Colin
I'm not clear but I think you would just do the same, i.e.
Colin
Thanks Colin.
What I meant to say is that I want the "is_processed" value in the database to get updated with the value from the "coun" variable as soon as I closed the editor.
Could it be done using ajax **and **POST?
or
just something like the following may suffice?
I'd suggest making your
coun
variable a regular Editor field - just of thehidden
type. Then instead of usingpreSubmit
move your code intoinitSubmit
which will let you set the value of the hidden field.So you can do:
That way
coun
is just a regular Editor parameter and can be treated as such on the server-side.Allan
YES!
Allan, that was it!
Oh my God, I spent really long time on this task.
Thanks a lot, Kevin, Colin, and Allan...you are wonderful guys