Manipulate DOM - Change values in database
Manipulate DOM - Change values in database
mgeyer
Posts: 20Questions: 0Answers: 0
Hello,
I want to display a value to a read only textfield, this value is combined through the values of different table values e.g.
Key = Customer + ID + Bezeichnung
my question is how can I read values from the table to concatenate them to a variable which is displayed in a read only field and how can I increase the value of a field (e.g. ID++ and save this to the table)?
please ask if something isn't clear to you.
thanks,
max
This discussion has been closed.
Replies
for example
if I click the button new, a modal appears.
inside this modal I want to create the key, concatened from the values loaded (this values are loaded inside the modal).
for example id is loaded in a read only textfield,
customer is loaded in a select drop down (so I have to selected the active one),
and bezeichnung/name can be changed by the user (so the key value has to update if the bezeichnung/name changes)
-> when I click save the ID should be increased, so when I click the next time new, there has to be a higher value.
Assuming you have the two pieces of data in the row's data object (how you build that will depend on how you are reading the database), then you could use a renderer to do the combining.
Allan
@allan thank you for your answer!
would it be visible for the user in real time?
is there an option to use normal javascript in fields:
for the moment I can't access by Id - but it would be clear to me.
please see the attached file (image)
the PMSchluessel (projectmanagementkey) is going to be generated dynamically on the user inputs - and the user sees the generated key in real time - it is combined with a onchange event for the text input - how to do this for the create button in datatable?
thank you!
https://editor.datatables.net/reference/api/dependent()
I would use "dependent" for this.
I use it all the time to show, hide and fill Editor fields depending on the respective situation. This can be very powerful - and complex too.
Here is a simple example from my own coding. As you can see you can embed "dependent" into "initEdit" and "initCreate" event handlers and hence create different dependencies if your are in edit or create mode. You can use "undependent" to delete the dependency on closing Editor for example.
@rf1234 hi roland,
can you give me some start help?
I'm not very familar with initCreate, etc.
where should I write which code to get things happen the right way?
how can I read the value of a field?
how can I change the value of a field when another field is changed (change event)?
=======
here is my php file (parent table)
//==============
//Parent Table
//==============
$(document).ready(function () {
var siteEditor = new $.fn.dataTable.Editor({
ajax: "php/projekte_kundenprojekte.php",
table: "#projekte_kundenprojekte",
fields: [
{
label: "Projektschlüssel:",
name: "Projekte_Kundenprojekte.PMSchluessel"
},
{
label: "Kunde:",
name: "Projekte_Kundenprojekte.Kunde_ID",
type: "select"
},
{
label: "ID:",
name: "Projekte_Kundenprojekte.Nummernkreis_ID",
type: "select"
},
{
label: "Beschreibung:",
name: "Projekte_Kundenprojekte.Beschreibung"
},
{
label: "Statusmeldung:",
name: "Projekte_Kundenprojekte.Statusmeldung"
},
{
label: "Zieldatum:",
name: "Projekte_Kundenprojekte.Zieldatum",
type: 'datetime',
def: function () { return new Date(); },
format: 'YYYY-MM-DD'
},
{
label: "Verantwortlich",
name: "Projekte_Kundenprojekte.Mitarbeiter_ID",
type: "select"
},
{
label: "Modus",
name: "Projekte_Kundenprojekte.Modus_ID",
type: "select"
}
]
});
});
```
thank you!
max
This is all java script. Put it all underneath the Editor definition before the data table definition.
editor.val('yourField') gives you the value of a field
The "dependent" event IS the actual change event.
editor.set sets the value of a field - just like in the examples above.
And yes: "Read the docs" ...
https://editor.datatables.net/reference/api/set()
https://editor.datatables.net/reference/api/val()
https://editor.datatables.net/manual/events
@rf1234 thank you very much!
for the moment I have a test license so I can't read all docs ^^
can you show me a demo code based on my code - for reading a field?
should look like this or?
yes, but why don't you just try it? I mean you have a debugger, I guess.
var key = siteEditor.val('PMSchluessel');
set a break point and take a look at the content of "key" for example or log it to the console.
And if you don't have access to the docs then I wouldn't use Editor. That doesn't really make very much sense. We can't replace the docs in the forum, sorry.
that is indeed how you could read a value inside
initCreate
. You aren't using the value for anything there, but you could then go on to do something with it.Allan
@rf1234 because I have tried and don't get the information why it isn't working.
I would really like to use the editor, but I will get the money for the editor if the system is nice to use - so it is a little bit tricky for me to get this challenge accomplished.
@allan thank you!
it would be great if somebody could give me a start up code - for the moment I'm trying this:
I just want to display in the PMSchluessel field a startup value - could also be test or something like this, but I don't get the information for the moment why it is not working.
It is for me like a needle in a haystack to get a clean solution, so any help would be awesome!
maximilian
this line assigns the value of the variable to itself. Maybe you want to think it over.
@rf1234
thank you
I see - how can I concatenate "_test"
edit:
solved - thank you!!!
For what it is worth, I would suggest against calling
dependent()
insideinitCreate
. You'd be adding a dependency every time you trigger a create event. That's going to leak memory and clock cycles.Allan
@allan
thank you for your hint.
I am going to get a solution!
@allan
you are ertainly right regarding max's code and the use of “dependent“! Thanks for clarifying!
I only had “dependent“ inside initCreate and initEdit in my example above because I have different dependencies for creating and editing on the same field and hence use “undependent“ on each close of the Editor form.
@allan @rf1234
so what would be a clean solution for my problem?
just callin 'initCreate' and a javascript function?
how to implement the onchange event without depend?
how could I get the value of a select input?
and how could I increase a value on the table if I click save?
I'm sorry I don't have enough knowledge for the moment.
thank you!
Just don't use
initCreate
. Usedependent()
.dependent()
is the way to listen for a change event (at least, one of them!).field().val()
Use
initSubmit
to get the value, increment it and then set the new value.Allan
@allan thank you - I'm trying my best.
I'm trying to convert this to "Create" - just for information.
You've got a shadow variable name in
projektnummer
btw (function is also called the same thing). It won't error, but it can be a bit confusing.From the code, you aren't updating based on user input, but rather when the page loads. So using
initEdit
would be the correct event to use here. Perhaps something like:Allan
@allan thanks.
===
just for information - without docs I can't solve this problem, so I have used bootstrap4 and the modal.
use this for calling the modal from a "custom" button (javascript-datatable)
build the modal
...
//put some where in $(document).ready(function () { and close
$('#modKundenprojekte').submit(function (e) {
$('#modKundenprojekte').modal('show');
e.preventDefault();
});
...
<?php
//or use configuration file
$con = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno())
{
echo "Ahoi Matrose - Datenbankleck entdeckt: " . mysqli_connect_error();
}
if($_POST["PMSchluessel"])
{
$pmschluessel = $_POST["PMSchluessel"];
$bezeichnung = $_POST["Bezeichnung"];
$kunde = $_POST["Kunde"];
$nummer = 1; #$_POST["Nummer"];
$beschreibung = $_POST["Beschreibung"];
$statusmeldung = $_POST["Statusmeldung"];
$zieldatum = date("Y-m-d"); #$_POST["Zieldatum"];
$verantwortlich = $_POST["Verantwortlich"];
$modus = $_POST["Modus"];
}
<?php > ``` ?>mysqli_close($con);
access a 1x1 javascript function to force your will
hope I could help