Need to store both Null and 0 in database
Need to store both Null and 0 in database
danvanwijk
Posts: 17Questions: 4Answers: 0
in Editor
Link to test case: https://joomrp.urtestsite.com/portal/companies/sites.html
Debugger code (debug.datatables.net):
Error messages shown: There is no error message
Description of problem: When editing the Company column we are unable to select the value with 0 value. We want to store both NULL and 0 to database. Can you please suggest an example or datatype for this?
Replies
You can define the column as int and make it nullable. Something like this
It is already implemented like that but I need validation at the PHP end to make sure we can store both 0 and NULL in the database. Right now I am facing when the value is selected as 0 because I am using this validation ->validator( Validate::numeric() )
->setFormatter( Format::ifEmpty(null) )
You could try something like this:
I will explain the complete functionality. We need to store three values in database which is 0, 1 and NULL and these are the three values of a checkbox. I am using the code for php as follows:
Field::inst( 'assembled' )
->setFormatter( function ( $val, $data, $opts ) {
return ! $val ? 0 : 1;
} )->validator( Validate::numeric() )
->setFormatter( Format::ifEmpty(null) ),
By using this, I am able to store values null and 1 but value 0 is also getting replaced by null. I want to store the 0 as 0 in database. Let me know if you can help with this.
Thanks!
What is your client-side setup for this field? If you submit an empty string as the value for when you want
null
to be written to the database, that that should be fine.Your first formatting function will need to be a little more accurate in your formatting function though since
! $val
is loosely "falsy" - i.e. it will match for''
,null
and0
.However, I don't think you need that - try:
Allan
It didn't work after changing the given code. Following is the client site code and we are fixing it first for Assembled field:
jquery: function () {
Here is the link for the current functionality: https://joomrp.urtestsite.com/portal/inventory/parts.html
You need to look for Assembled field.
How is the use to indicate a
0
ornull
value?checkbox
does have aunselectedValue
option, but if you want the end user to select 0, 1 ornull
, you'd need at least one more option there. Probably a0
and set theunselectedValue
option to empty string (to represent a null) - although once a value has been selected, they'd never be able to unselect - so the solution really depends on what you actually want the end user to be able to select.Allan
Actually, we need to use a three state checkbox like this one https://vanderlee.github.io/tristate/
Default state will be indeterminate, then when we click once it will be checked. On second click it will be unchecked and on third click it will be indeterminate again and so on.
Do you think you can suggest something to help with this?
Yes, for a tristate checkbox, you would need to create a custom field type plug-in that will work with such a plug-in.
It might(?) sound a bit scary, but simple plug-ins for Editor are fairly straight forward when you break them down. See for example.
Allan
Ok. Can you provide some similar example on how to achieve tristate checkbox through it?
Custom development of a plug-in would be something that normally falls under the support packages, but I'll take a look and see what I can do next week. It might be a fun one to do.
Allan
Hi Allan,
Thanks for considering to help us with this! Do you have any update regarding this?
Thanks!
Yes, I've just knocked this example together.
You'll notice that the tri-state values can be set via the
options
parameter for the newtristate
field type. Those options just get passed through to the Tristate initialisation, so you can modify that object as needed. In your case, an empty string to representnull
might be a good idea and then use aifEmpty
formatter at the server side to make it properly null.Allan
Thanks, Allan! It seems to be working well for editing using select and edit. Do you think it will be possible to implement this for inline editing as well?
It should work out of the box for inline editing: https://live.datatables.net/lihadiga/2/edit .
Allan