Dynamic dropdown field from db

Moderator: crythias

Post Reply
katerina
Znuny newbie
Posts: 46
Joined: 19 Jul 2012, 13:01
Znuny Version: 3.1.7
Real Name: Katerina
Company: PNB

Dynamic dropdown field from db

Post by katerina »

I am trying to get working Dynamic dropdown field from db described in viewtopic.php?f=60&t=17033, but I stucked.
I commented out next code and filled the right info
# DatabaseDSN => 'DBI:odbc:database=123;host=localhost;',
# DatabaseUser => 'user',
# DatabasePw => 'somepass',
# Type => 'mysql',
but after executing the script extdata.pl I got error

Code: Select all

install_driver(odbc) failed: Can't locate DBD/odbc.pm 
So I installed perl-DBD-ODBC and perl-Class-DBI. The then I have to change in the code DBI:odbc into DBI:ODBC and I got another error:

Code: Select all

DBI connect('database=regkarpc;host=localhost;','regkar',...) failed: [unixODBC][Driver Manager]Data source name not found, and no default driver specified (SQL-IM002) at /opt/otrs/bin/cgi-bin/../../Kernel/System/DB.pm line 256
ERROR: ExtData-10 Perl: 5.10.1 OS: linux Time: Wed Mar  6 09:40:26 2013
So I read about odbc and configured odbc.ini for mysql database I wanted to use for populating dropdown field. I used the howto in this link:
http://wiki.sysconfig.org.uk/display/ho ... CentOS+5.2
Now the command
#isql mydsn
gives me some output from the database (mydns is the name in odbc,ini), but if I execute extdata.pl, I got the same error as above - DBI connect('database=regkarpc;host=localhost;','regkar',...) failed:

I don't know if the installation and configuration of odbc drivers were necessary or how to tell the script extdata.pl that it has to use the source mysdn or how to connect to my database some other way.

Or is it necessary for the table from which I want to extract my data to be in the otrs database?

To answer this question I copyed my table regkar_pc into the otrs database.
I commented the lines which originaly had been commented in your script
# DatabaseDSN => 'DBI:odbc:database=123;host=localhost;',
# DatabaseUser => 'user',
# DatabasePw => 'somepass',
# Type => 'mysql',
and run extdata.pl
I got an error

Code: Select all

DBD::mysql::st execute failed: called with 1 bind variables when 0 are needed at /opt/otrs/bin/cgi-bin/../../Kernel/System/DB.pm line 618.
ERROR: ExtData-10 Perl: 5.10.1 OS: linux Time: Wed Mar  6 10:38:37 2013

 Message: called with 1 bind variables when 0 are needed, SQL: 'SELECT ID,NAZEV FROM regkar_pc LIMIT 10'

 Traceback (6611): 
   Module: ./extdata.pl (unknown version) Line: 43

DBD::mysql::st fetchrow_array failed: fetch() without execute() at /opt/otrs/bin/cgi-bin/../../Kernel/System/DB.pm line 685.
So I modified the sql query into "SELECT NAZEV FROM regkar_pc" (I deleted the ID field from query), but the error is the same.

Can you help me, please?
otrs v.5 on linux CentOS 6.8 with MySQL Ver 14.14 Distrib 5.1.73, openLDAP
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Dynamic dropdown field from db

Post by reneeb »

You are passing 1 Bind parameter, but you do not have a placeholder in your SELECT statement. Please show the extdata.pl script.
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
katerina
Znuny newbie
Posts: 46
Joined: 19 Jul 2012, 13:01
Znuny Version: 3.1.7
Real Name: Katerina
Company: PNB

Re: Dynamic dropdown field from db

Post by katerina »

Code: Select all

#!/usr/bin/perl -w
    use strict;
    use warnings;

    use CGI qw(:standard);
    #use JSON; #install JSON via cpan/ppm if you want to use it instead

    print header(-type => 'application/json');

    #code to query data

    # use ../../ as lib location
    use FindBin qw($Bin);
    use lib "$Bin/../..";
    use lib "$Bin/../../Kernel/cpan-lib";
    use lib "$Bin/../../Custom";

    use Kernel::Config;
    use Kernel::System::Encode;
    use Kernel::System::Log;
    use Kernel::System::Main;
    use Kernel::System::DB;


       my %CommonObject = ();
       $CommonObject{ConfigObject} = Kernel::Config->new();
       $CommonObject{EncodeObject} = Kernel::System::Encode->new(%CommonObject);
       $CommonObject{LogObject}    = Kernel::System::Log->new(
        LogPrefix => 'ExtData',
        %CommonObject,
       );
       $CommonObject{MainObject}   = Kernel::System::Main->new(%CommonObject);
       $CommonObject{DBObject}     = Kernel::System::DB->new(
          %CommonObject,
    #     DatabaseDSN  => 'DBI:ODBC:database=regkarpc;host=localhost;',
    #     DatabaseUser => 'regkar',
    #     DatabasePw   => 'test',
    #     Type         => 'mysql',
       );
 my $query = param('q') || '';
       my $like = '%' . $query . '%';
       my $ResultAsArrayRef = $CommonObject{DBObject}->Prepare(
          SQL => "SELECT NAZEV FROM regkar_pc",

          Bind => [ \$like ],
          Order => 'asc',
          Limit => 10
       );
    #  my $json_text = to_json($ResultAsArrayRef);
    #  print $json_text;
    while ( my @Row = $CommonObject{DBObject}->FetchrowArray()) {
       print '<option value="' . $Row[0] . '">'. $Row[1] . "</option>\n";
    }
    1;
Thanks and sorry that I don't know what placeholder is
otrs v.5 on linux CentOS 6.8 with MySQL Ver 14.14 Distrib 5.1.73, openLDAP
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Dynamic dropdown field from db

Post by reneeb »

Your "prepare" looks like

Code: Select all

       my $ResultAsArrayRef = $CommonObject{DBObject}->Prepare(
          SQL => "SELECT NAZEV FROM regkar_pc",

          Bind => [ \$like ],
          Order => 'asc',
          Limit => 10
       );
You pass one element in the Bind arrayreference. So the database driver expects a placeholder in you statement. So it should look like

Code: Select all

       my $ResultAsArrayRef = $CommonObject{DBObject}->Prepare(
          SQL => "SELECT NAZEV FROM regkar_pc WHERE columne LIKE '?'",

          Bind => [ \$like ],
          Order => 'asc',
          Limit => 10
       );
the "?" is the placeholder.

The second possible solution would be

Code: Select all

       my $ResultAsArrayRef = $CommonObject{DBObject}->Prepare(
          SQL => "SELECT NAZEV FROM regkar_pc",
          Order => 'asc',
          Limit => 10
       );
No placeholder, no bind variable => the database driver is happy, too.

What the right solution is, depends on you needs...
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
katerina
Znuny newbie
Posts: 46
Joined: 19 Jul 2012, 13:01
Znuny Version: 3.1.7
Real Name: Katerina
Company: PNB

Re: Dynamic dropdown field from db

Post by katerina »

Thank you a lot,
now extdata.pl gives me output that seems OK.

But I want dropdown field to be seen in New Telephone Ticket
When I create new phone ticket I can see my dropdown field, but it is empty. I have created dynamic field named jmenoPC as dropdown list, but this field I didn't set as dynamic field in sysconfig - ticket -Frontend::Agent::Ticket::ViewPhoneNew
I added this code into AgentTicketPhone.dtl but id doesn't work - the dynamic field isn't filled with the output of extdata.pl

Code: Select all

<label for="myselect">Pocitac:</label>
<div class="Field">
<select id="myselect" name="DynamicField_jmenoPC">
</select>
</div>
There you can see where is the code placed in AgentTicketPhone.dtl

Code: Select all

<!--dtl:js_on_document_complete-->
<script type="text/javascript">//<![CDATA[
    $('#PriorityID').bind('change', function (Event) {
        Core.AJAX.FormUpdate($('#NewPhoneTicket'), 'AJAXUpdate', 'PriorityID', [ 'TypeID', 'Dest', 'NewUserID','NewResponsibleID', 'NextStateID', 'ServiceID', 'SLAID', '
    });
//]]></script>
<!--dtl:js_on_document_complete-->
                    </div>
<label for="myselect">Pocitac:</label>
<div class="Field">
<select id="myselect" name="DynamicField_jmenoPC">
</select>
</div>
<div class="Clear"></div>
                        
<!-- dtl:block:DynamicField -->
                    <div class="Row Row_DynamicField_$QData{"Name"}">
                        $Data{"Label"}
                        <div class="Field">
                            $Data{"Field"}

At the end of AgentTicketPhone.dtl I have

Code: Select all

<script type="text/javascript">
        $.get('extdata.pl', function(data) {
           $('#myselect').append(data);
    });
</script>
Thanks for your help very much
otrs v.5 on linux CentOS 6.8 with MySQL Ver 14.14 Distrib 5.1.73, openLDAP
crythias
Moderator
Posts: 10169
Joined: 04 May 2010, 18:38
Znuny Version: 5.0.x
Location: SouthWest Florida, USA
Contact:

Re: Dynamic dropdown field from db

Post by crythias »

katerina wrote:the dynamic field isn't filled with the output of extdata.pl
The dynamic field isn't called "myselect" in View Source.
It's likely something like DynamicField_telephonenumber (here, my field is ddl)

Code: Select all

<div class="Row Row_DynamicField_ddl">
                        <label id="LabelDynamicField_ddl" for="DynamicField_ddl">
        Dummy Drop Down List:
</label>

                        <div class="Field">
                            <select class="DynamicFieldText" id="DynamicField_ddl" name="DynamicField_ddl" size="1">
</select>

                        </div>
                        <div class="Clear"></div>
                    </div>
and all I did was append the following to AgentTicketPhone.dtl and it just worked:

Code: Select all

<script type="text/javascript">
        $.get('extdata.pl', function(data) {
           $('#DynamicField_ddl').append(data);
    });
</script>
If it doesn't, it's because your output isn't properly formatted.

Note that your SELECT only retrieves one field/column:

Code: Select all

"SELECT NAZEV FROM regkar_pc",
So if you are using it as an option list, you'll want it both as key and value. Otherwise, you will want to SELECT a key as well, because your output is:

Code: Select all

       print '<option value="' . $Row[0] . '">'. $Row[1] . "</option>\n";
This wants two fields per row. It will never show you a list because there is no value for the second result. You can change 1 to 0 and use the same value or you can SELECT a key before the display.
OTRS 6.0.x (private/testing/public) on Linux with MySQL database.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask
katerina
Znuny newbie
Posts: 46
Joined: 19 Jul 2012, 13:01
Znuny Version: 3.1.7
Real Name: Katerina
Company: PNB

Re: Dynamic dropdown field from db

Post by katerina »

Thank you very much for your help and patience.
It still doesn't work for me.
My code now looks like that:

Code: Select all

<div class="Row Row_DynamicField_jmenoPC">
        <label id="LabelDynamicField_jmenoPC" for="DynamicField_jmenoPC">
        Jmeno PC:
</label>
        <div class="Field">
        <select class="DynamicFieldText" id="DynamicField_jmenoPC" name="DynamicField_jmenoPC" size="1">
</select>
        </div>
        <div class="Clear"></div>
        </div>
If I set in sysconfig - ticket -Frontend::Agent::Ticket::ViewPhoneNew dynamic field jmenoPC then I can see in View source the same code generated by the system - I want to say that DynamicField_jmenoPC is really correct


The output of http://otrsserver/otrs/extdata.pl?q=ro is
<option value="1">1-dokument</option>
<option value="2">1-sesterna-1p</option>
<option value="3">1-lekar-pr</option>
<option value="4">1-lekar2-pr</option>
<option value="5">1-lekar2-1p</option>
and at the end of AgentTicketPhone.dtl I have

Code: Select all

<script type="text/javascript">
        $.get('extdata.pl', function(data) {
           $('#DynamicField_jmenoPC').append(data);
    });
    </script>
It looks like the script above doesn't run, but I don't know how to find out what's going on.
I can't see any misspeling and I tried some experimetns like placing the script on another place or wrap it in some commands, but in fact I am trying without understanding.

In View Source I see

Code: Select all

   </div>
    <div id="CustomerTickets"></div>
</div>

<script type="text/javascript">
        $.get('extdata.pl', function(data) {
           $('#DynamicField_jmenoPC').append(data);
    });
    </script>

<div id="Footer" class="ARIARoleContentinfo">

    <a href="http://otrs.org/" target="_blank">Powered by OTRS 3.2.2</a>
    <div class="TopOfPage"><a href="#Top">Top of page</a></div>
Thank you again.
otrs v.5 on linux CentOS 6.8 with MySQL Ver 14.14 Distrib 5.1.73, openLDAP
crythias
Moderator
Posts: 10169
Joined: 04 May 2010, 18:38
Znuny Version: 5.0.x
Location: SouthWest Florida, USA
Contact:

Re: Dynamic dropdown field from db

Post by crythias »

katerina wrote:It looks like the script above doesn't run, but I don't know how to find out what's going on.
You may need to load jquery on the page. I'm already loading it because of my lightbox plugin. jquery isn't loaded by default (?) so that could very well be the problem.
OTRS 6.0.x (private/testing/public) on Linux with MySQL database.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask
katerina
Znuny newbie
Posts: 46
Joined: 19 Jul 2012, 13:01
Znuny Version: 3.1.7
Real Name: Katerina
Company: PNB

Re: Dynamic dropdown field from db

Post by katerina »

so I downloaded http://lokeshdhakar.com/projects/lightbox2/, I added into HTMLHead.dtl
lines

Code: Select all

<script src ="$Config{"Frontend::WebPath"}thirdparty/lightbox/js/jquery-1.7.2.min.js"></script>
<script src ="$Config{"Frontend::WebPath"}js/thirdparty/lightbox/js/lightbox.js"></script>
<link href="$Config{"Frontend::WebPath"}js/thirdparty/lightbox/css/lightbox.css" rel="stylesheet" />
but the new telephone ticket still doesn't show the dropdown list with my db and I found in my firefox error console:
jQuery is not defined
http ://172.19.11.95/otrs-web/js/thirdparty/lightbox/js/lightbox.js line 46
$ is not defined
http ://172.19.11.95/otrs/index.pl?Action=AgentTicketPhone&OTRSAgentinterface.... line 674
in lightbox.js near line 46 there is

Code: Select all

(function() {
  var $, Lightbox, LightboxOptions;

  $ = jQuery;

  LightboxOptions = (function() {

    function LightboxOptions() {
      this.fileLoadingImage = 'images/loading.gif';
      this.fileCloseImage = 'images/close.png';
      this.resizeDuration = 700;
      this.fadeDuration = 500;
      this.labelImage = "Image";
      this.labelOf = "of";
    }

    return LightboxOptions;

  })();
I didn't do the other code modifications for lightbox, because I don't want the lightbox functionality, I only want to load jquery.

I tried also to go another way: Instead of the code above I added to HTMLHead.dtl

Code: Select all

<script src ="$Config{"Frontend::WebPath"}thirdparty/js/jquery-1.6.4/jquery.js"></script>
</head>
but I got the error
$ is not defined
http ://172.19.11.95/otrs/index.pl?Action=AgentTicketPhone&OTRSAgentinterface.... line 674
so I don't know what to do again.
Thank for any help
otrs v.5 on linux CentOS 6.8 with MySQL Ver 14.14 Distrib 5.1.73, openLDAP
katerina
Znuny newbie
Posts: 46
Joined: 19 Jul 2012, 13:01
Znuny Version: 3.1.7
Real Name: Katerina
Company: PNB

Re: Dynamic dropdown field from db

Post by katerina »

Now I found my mistake, I wrote wrong path to the jquery.
Now I corrected the path, I have no error in th error console, but the script doesn't run either.
otrs v.5 on linux CentOS 6.8 with MySQL Ver 14.14 Distrib 5.1.73, openLDAP
crythias
Moderator
Posts: 10169
Joined: 04 May 2010, 18:38
Znuny Version: 5.0.x
Location: SouthWest Florida, USA
Contact:

Re: Dynamic dropdown field from db

Post by crythias »

I can't really help with "script doesn't run."

Likely it *does* run, but you need to pass some variables. (For instance, this is calling extdata.pl only. So you'd get a return result of the same thing that extdata.pl would return if you go there.
try console.log(data) within the jquery function to see if it is called.
OTRS 6.0.x (private/testing/public) on Linux with MySQL database.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask
katerina
Znuny newbie
Posts: 46
Joined: 19 Jul 2012, 13:01
Znuny Version: 3.1.7
Real Name: Katerina
Company: PNB

Re: Dynamic dropdown field from db

Post by katerina »

Thanks,
every time I learn something new..

Code: Select all

<script type="text/javascript">
        $.get('extdata.pl', function(data) {
           $('#DynamicField_jmenoPC').append(data);
           console.log(data);
    });
</script>
The code above gives me in the console the right output, I see
<option value="1">1-dokument</option>
<option value="2">1-sesterna-1p</option>
<option value="3">1-lekar-pr</option>
but I see nothing in dropdown box.

In my code I have this (the other part of the code is normal text dynamic field)

Code: Select all

<div class="Row Row_DynamicField_jmenoPC">
        <label id="LabelDynamicField_jmenoPC" for="DynamicField_jmenoPC">
        Jmeno PC:
</label>
        <div class="Field">
        <select class="DynamicFieldText" id="DynamicField_jmenoPC" name="DynamicField_jmenoPC" size="1">
        
</select>
        </div>
        <div class="Clear"></div>
        </div>

<!-- dtl:block:DynamicField -->
                    <div class="Row Row_DynamicField_$QData{"Name"}">
                        $Data{"Label"}
                        <div class="Field">
                            $Data{"Field"}
                        
                        </div>
                        <div class="Clear"></div>
                    </div>
<!-- dtl:block:DynamicField -->
Thank you again for your patience and help
otrs v.5 on linux CentOS 6.8 with MySQL Ver 14.14 Distrib 5.1.73, openLDAP
katerina
Znuny newbie
Posts: 46
Joined: 19 Jul 2012, 13:01
Znuny Version: 3.1.7
Real Name: Katerina
Company: PNB

Re: Dynamic dropdown field from db

Post by katerina »

My problem is, that the line
$('#DynamicField_jmenoPC').append(data);
has no efect.

I tried to use other fields instead of #DynamicField_jmenoPC, but append never worked and never appended any text. I tried appendTo, but it didn't work either.
otrs v.5 on linux CentOS 6.8 with MySQL Ver 14.14 Distrib 5.1.73, openLDAP
crythias
Moderator
Posts: 10169
Joined: 04 May 2010, 18:38
Znuny Version: 5.0.x
Location: SouthWest Florida, USA
Contact:

Re: Dynamic dropdown field from db

Post by crythias »

just for fun, can you drop this in your dtl?

Code: Select all

<select id="mine" name="mine"></select>
<script type="text/javascript">
        $.get('extdata.pl', function(data) {
           $('#mine').append(data);
           console.log(data);
        });
</script>
katerina
Znuny newbie
Posts: 46
Joined: 19 Jul 2012, 13:01
Znuny Version: 3.1.7
Real Name: Katerina
Company: PNB

Re: Dynamic dropdown field from db

Post by katerina »

I tried the code on my two testing machines, one was freshly installed otrs 3.2.2, the other was clon of upgraded otrs from 3.1.12 to 3.2.2.
The results are the same:
I can see the dropdown box, but it is empty, in firebug console I can see the output that should populate the dropdown.

And yesterday at home I tried to write simple php with this jquery function and it worked - I saw the dropdown box filled with the string I have as a parameter for append.
otrs v.5 on linux CentOS 6.8 with MySQL Ver 14.14 Distrib 5.1.73, openLDAP
crythias
Moderator
Posts: 10169
Joined: 04 May 2010, 18:38
Znuny Version: 5.0.x
Location: SouthWest Florida, USA
Contact:

Re: Dynamic dropdown field from db

Post by crythias »

Try a more recent version of jquery.
OTRS 6.0.x (private/testing/public) on Linux with MySQL database.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask
katerina
Znuny newbie
Posts: 46
Joined: 19 Jul 2012, 13:01
Znuny Version: 3.1.7
Real Name: Katerina
Company: PNB

Re: Dynamic dropdown field from db

Post by katerina »

Thank you for your help,
I tried 3 versions of jquery, none worked.
At the end I gave up.
We have no dropdown field with PC names, our technician isn't happy but he accepted it.
Thank you again
otrs v.5 on linux CentOS 6.8 with MySQL Ver 14.14 Distrib 5.1.73, openLDAP
pushreply
Znuny newbie
Posts: 14
Joined: 17 Apr 2013, 16:32
Znuny Version: 3.2.4
Real Name: Pu Shrep

Re: Dynamic dropdown field from db

Post by pushreply »

crythias wrote:just for fun, can you drop this in your dtl?

Code: Select all

<select id="mine" name="mine"></select>
<script type="text/javascript">
        $.get('extdata.pl', function(data) {
           $('#mine').append(data);
           console.log(data);
        });
</script>
Hello Crythias,

I tried that, I also use dynamic_field in "new process ticket".
I put the script at the end of the "AgentTicketProcess.dtl"

The result of extdata.pl is something like this

Code: Select all

<option value="Application for leave - 2013-05-23 09:23:34">Application for leave - 2013-05-23 09:23:34</option>
<option value="Test - 2013-05-23 11:43:12">Test - 2013-05-23 11:43:12</option>
<option value="Test - 2013-05-23 11:45:52">Test - 2013-05-23 11:45:52</option>
<option value="Test - 2013-05-23 09:23:34">Test - 2013-05-23 09:23:34</option>
<option value="Test - 2013-05-23 11:43:12">Test - 2013-05-23 11:43:12</option>
<option value="Test - 2013-05-23 11:45:52">Test - 2013-05-23 11:45:52</option>
The same result also appears in the console (firebug),
but the data are not displayed in the dropdownlist.

Jquery version: actual and CDN.


Any help would be appreciated.
crythias
Moderator
Posts: 10169
Joined: 04 May 2010, 18:38
Znuny Version: 5.0.x
Location: SouthWest Florida, USA
Contact:

Re: Dynamic dropdown field from db

Post by crythias »

Just make sure that jquery is actually loaded. You may need to include the reference to it in HTMLHead.dtl
OTRS 6.0.x (private/testing/public) on Linux with MySQL database.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask
pushreply
Znuny newbie
Posts: 14
Joined: 17 Apr 2013, 16:32
Znuny Version: 3.2.4
Real Name: Pu Shrep

Re: Dynamic dropdown field from db

Post by pushreply »

crythias wrote:Just make sure that jquery is actually loaded. You may need to include the reference to it in HTMLHead.dtl
yes, I did put the jquery include in HTMLHead.dtl.

just for fun:
if I change ".get" to ".each", it loads every char from "extdata.pl" to the dropdownlist :) (e, x, t, d....)
so, I think jquery does load.
crythias
Moderator
Posts: 10169
Joined: 04 May 2010, 18:38
Znuny Version: 5.0.x
Location: SouthWest Florida, USA
Contact:

Re: Dynamic dropdown field from db

Post by crythias »

does your script have
$('#DynamicField_fieldname').append(data);

?
OTRS 6.0.x (private/testing/public) on Linux with MySQL database.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask
pushreply
Znuny newbie
Posts: 14
Joined: 17 Apr 2013, 16:32
Znuny Version: 3.2.4
Real Name: Pu Shrep

Re: Dynamic dropdown field from db

Post by pushreply »

crythias wrote:does your script have
$('#DynamicField_fieldname').append(data);

?
yes,

inside "AgentTicketProcess.dtl", on the last line, before "<!-- dtl:js_on_document_complete -->"

Code: Select all

<script type="text/javascript">
	$.get('externalData.pl', function(data) {
		$('#DynamicField_Ticket1WeekOverdue').append(data);
		console.log(data);
	});
</script>
after rendered in browser, I trace the html using firebug:

Code: Select all

<div class="Field">
<select id="DynamicField_Ticket1WeekOverdue" class="DynamicFieldText" size="1" name="DynamicField_Ticket1WeekOverdue"> </select>
</div>
as you can see, between the select tags is empty.

jquery goes inside "HTMLHead.dtl":

Code: Select all

    <link rel="stylesheet" type="text/css" href="$Config{"Frontend::WebPath"}skins/Agent/default/css/thirdparty/ui-theme/jquery-ui.css" />
	<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.0.min.js"></script>
formatted result in "externalData.pl", just the same as your example

Code: Select all

print '<option value="' . $Row[0] . '">'. $Row[0] . "</option>\n";
result from "http://localhost:81/otrs/externaldata.pl?q="

Code: Select all

<option value="Test - 2013-05-23 09:23:34">Test - 2013-05-23 09:23:34</option>
<option value="Test - 2013-05-23 11:43:12">Test - 2013-05-23 11:43:12</option>
<option value="Test - 2013-05-23 11:45:52">Test - 2013-05-23 11:45:52</option>
<option value="Test - 2013-05-23 09:23:34">Test - 2013-05-23 09:23:34</option>
crythias
Moderator
Posts: 10169
Joined: 04 May 2010, 18:38
Znuny Version: 5.0.x
Location: SouthWest Florida, USA
Contact:

Re: Dynamic dropdown field from db

Post by crythias »

ah. maybe you'll need to get the full path because you're not using normal port?
OTRS 6.0.x (private/testing/public) on Linux with MySQL database.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask
pushreply
Znuny newbie
Posts: 14
Joined: 17 Apr 2013, 16:32
Znuny Version: 3.2.4
Real Name: Pu Shrep

Re: Dynamic dropdown field from db

Post by pushreply »

crythias wrote:ah. maybe you'll need to get the full path because you're not using normal port?

let me put the full path:

Code: Select all

<script type="text/javascript">
	$.get('http://localhost:81/otrs/externaldata.pl', function(data) {
		$('#DynamicField_Ticket1WeekOverdue').append(data);
		console.log(data);
	});
</script>
still does not work..
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Dynamic dropdown field from db

Post by reneeb »

Any messages on the JS console? Can you add some log messages in the externaldata.pl? Do those message appear when you reload the page (-> does the Perl script run)? Is the JS triggered (add an "alert('test')" before the "$.get()" line? Any messages in the Apache log?
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
pushreply
Znuny newbie
Posts: 14
Joined: 17 Apr 2013, 16:32
Znuny Version: 3.2.4
Real Name: Pu Shrep

Re: Dynamic dropdown field from db

Post by pushreply »

reneeb wrote:Any messages on the JS console? Can you add some log messages in the externaldata.pl? Do those message appear when you reload the page (-> does the Perl script run)? Is the JS triggered (add an "alert('test')" before the "$.get()" line? Any messages in the Apache log?
it seems the JS is not triggered after "$.get.."
only the "alert('test1') is loaded, test2 is not loaded...(also 3 and 4)

Code: Select all

<script type="text/javascript">
	alert('test1'); 
	$.get('http://localhost:81/otrs/externaldata.pl', function(data) {
	alert('test2'); 
		$('#DynamicField_Ticket1WeekOverdue').append(data);
		alert('test3'); 
		console.log(data);
		alert('test4'); 
	});
</script>
BUT, if I change "$.get()" to "$.each()", all alerts come up.


using firebug, the only log message I 've seen:

Code: Select all

GET http://localhost:81/otrs/externaldata.pl 200 OK	185ms Common...7a2f.js (line 35)
HeadersResponseCookies

<option value="Test - 2013-05-23 09:23:34">Test - 2013-05-23 09:23:34</option>
<option value="Test - 2013-05-23 11:43:12">Test - 2013-05-23 11:43:12</option>
<option value="Test - 2013-05-23 11:45:52">Test - 2013-05-23 11:45:52</option>
<option value="Test - 2013-05-23 09:23:34">Test - 2013-05-23 09:23:34</option>
<option value="Test - 2013-05-23 11:43:12">Test - 2013-05-23 11:43:12</option>
<option value="Test - 2013-05-23 11:45:52">Test - 2013-05-23 11:45:52</option>
<option value="Test - 2013-05-23 09:23:34">Test - 2013-05-23 09:23:34</option>
<option value="Test - 2013-05-23 11:43:12">Test - 2013-05-23 11:43:12</option>
<option value="Test - 2013-05-23 11:45:52">Test - 2013-05-23 11:45:52</option>
<option value="Test - 2013-05-23 09:23:34">Test - 2013-05-23 09:23:34</option>
and of course, perl script is running :)

I don't see any significant message in the apache access.log...

Code: Select all

127.0.0.1 - - [12/Jun/2013:14:56:07 +0200] "GET /otrs/index.pl?Action=AgentTicketProcess HTTP/1.1" 200 5920
127.0.0.1 - - [12/Jun/2013:14:56:07 +0200] "GET /otrs-web/skins/Agent/default/css-cache/CommonCSS_b4ff5c25f04bb86aab577c62b0f4b1bc.css HTTP/1.1" 304 -
127.0.0.1 - - [12/Jun/2013:14:56:07 +0200] "GET /otrs-web/skins/Agent/ivory/css-cache/CommonCSS_03c8ceba6a0c7aac900e606116c22ba0.css HTTP/1.1" 304 -
127.0.0.1 - - [12/Jun/2013:14:56:07 +0200] "GET /otrs-web/skins/Agent/default/css-cache/ModuleCSS_c85cf21cf6be84cfe722ae0601e7d228.css HTTP/1.1" 304 -
127.0.0.1 - - [12/Jun/2013:14:56:07 +0200] "GET /otrs-web/skins/Agent/default/css/thirdparty/ui-theme/jquery-ui.css HTTP/1.1" 304 -
127.0.0.1 - - [12/Jun/2013:14:56:07 +0200] "GET /otrs-web/js/js-cache/CommonJS_40b014e6951f0a153445a962fbe07a2f.js HTTP/1.1" 304 -
127.0.0.1 - - [12/Jun/2013:14:56:07 +0200] "GET /otrs-web/js/thirdparty/ckeditor-4.0/ckeditor.js HTTP/1.1" 304 -
127.0.0.1 - - [12/Jun/2013:14:56:07 +0200] "GET /otrs-web/js/js-cache/ModuleJS_67fa546b1763785d854527fa79921f15.js HTTP/1.1" 304 -
127.0.0.1 - - [12/Jun/2013:14:56:08 +0200] "GET /otrs-web/skins/Agent/ivory/img/toolbar_sprite.png HTTP/1.1" 304 -
127.0.0.1 - - [12/Jun/2013:14:56:08 +0200] "GET /otrs-web/skins/Agent/default/img/header_bg.png HTTP/1.1" 304 -
127.0.0.1 - - [12/Jun/2013:14:56:08 +0200] "GET /otrs-web/skins/Agent/ivory/img/logout_sprite.png HTTP/1.1" 304 -
127.0.0.1 - - [12/Jun/2013:14:56:08 +0200] "GET /otrs-web/skins/Agent/default/img/ITSMChangeManagement_toolbar_sprite.png HTTP/1.1" 304 -
127.0.0.1 - - [12/Jun/2013:14:56:09 +0200] "GET /otrs-web/skins/Agent/ivory/img/navigation_sprite.png HTTP/1.1" 304 -
127.0.0.1 - - [12/Jun/2013:14:56:08 +0200] "GET /otrs-web/skins/Agent/default/img/search_bg.png HTTP/1.1" 304 -
127.0.0.1 - - [12/Jun/2013:14:56:09 +0200] "GET /otrs-web/skins/Agent/default/img/arrow_top.png HTTP/1.1" 304 -
127.0.0.1 - - [12/Jun/2013:14:56:10 +0200] "GET /otrs/externaldata.pl HTTP/1.1" 200 122
or error.log

Code: Select all

 RemoteAddress: 127.0.0.1
 RequestURI: /otrs/index.pl?

 Traceback (6764): 
   Module: Kernel::System::ProcessManagement::Process::ProcessStartpointGet (OTRS 3.2.4) Line: 358
   Module: Kernel::System::ProcessManagement::Process::ProcessList (OTRS 3.2.4) Line: 295
   Module: Kernel::Modules::AgentTicketProcess::Run (OTRS 3.2.4) Line: 263
   Module: Kernel::System::Web::InterfaceAgent::Run (OTRS 3.2.4) Line: 863
   Module: ModPerl::ROOT::ModPerl::Registry::C_3a_OTRS_OTRS_bin_cgi_2dbin_index_2epl::handler (unknown version) Line: 41
   Module: (eval) (v1.99) Line: 204
   Module: ModPerl::RegistryCooker::run (v1.99) Line: 204
   Module: ModPerl::RegistryCooker::default_handler (v1.99) Line: 170
   Module: ModPerl::Registry::handler (v1.99) Line: 31
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Dynamic dropdown field from db

Post by reneeb »

Try to replace

Code: Select all

<script type="text/javascript">
   alert('test1');
   $.get('http://localhost:81/otrs/externaldata.pl', function(data) {
   alert('test2');
      $('#DynamicField_Ticket1WeekOverdue').append(data);
      alert('test3');
      console.log(data);
      alert('test4');
   });
</script>
with

Code: Select all

<script type="text/javascript">
   alert('test1');
   var request = $.ajax({
       type: 'GET',
       url: 'http://localhost:81/otrs/externaldata.pl', 
       dataType: 'html'
    });

    request.done( function(data) {
   alert('test2');
      $('#DynamicField_Ticket1WeekOverdue').append(data);
      alert('test3');
      console.log(data);
      alert('test4');
   });

   alert( 'test5' );
</script>
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
pushreply
Znuny newbie
Posts: 14
Joined: 17 Apr 2013, 16:32
Znuny Version: 3.2.4
Real Name: Pu Shrep

Re: Dynamic dropdown field from db

Post by pushreply »

reneeb wrote: with

Code: Select all

<script type="text/javascript">
   alert('test1');
   var request = $.ajax({
       type: 'GET',
       url: 'http://localhost:81/otrs/externaldata.pl', 
       dataType: 'html'
    });

    request.done( function(data) {
   alert('test2');
      $('#DynamicField_Ticket1WeekOverdue').append(data);
      alert('test3');
      console.log(data);
      alert('test4');
   });


   alert( 'test5' );
</script>

WOW, what did I do wrong ? :lol:
It works!!! :)
thanks reneeb
--------------------------------------------------------
UPDATE:
this works, only if this chunk:

Code: Select all

<select id="DynamicField_Ticket1WeekOverdue" class="DynamicFieldText" size="1" name="DynamicField_Ticket1WeekOverdue"></select>
hardcoded into the dtl:

Code: Select all

			<select id="DynamicField_Ticket1WeekOverdue" class="DynamicFieldText" size="1" name="DynamicField_Ticket1WeekOverdue"></select>
<!-- dtl:block:ProcessList -->
                <label class="Mandatory" for="ProcessEntityID"><span class="Marker">*</span>$Text{"Process"}:</label>
                <div class="Field">
                    $Data{"ProcessList"}
                    <div id="ProcessEntityIDError" class="TooltipErrorMessage"><p>$Text{"This field is required."}</p></div>
                    <div id="ProcessEntityIDServerError" class="TooltipErrorMessage"><p>$Text{"This field is required."}</p></div>
                <div class="Spacing Clear"></div>
<!-- dtl:block:ProcessList -->

what I am trying to do:
load the result into a dynamic field of a custom process ticket in the

Code: Select all

http://localhost:81/otrs/index.pl?Action=AgentTicketProcess
and yeah, still does not work for that :(
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Dynamic dropdown field from db

Post by reneeb »

So the select doesn't exist in the HTML form? Then you have to check in the *done* method if that dropdown exists. If it exists, just append the data, otherwise you have to create the dropdown first...
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
pushreply
Znuny newbie
Posts: 14
Joined: 17 Apr 2013, 16:32
Znuny Version: 3.2.4
Real Name: Pu Shrep

Re: Dynamic dropdown field from db

Post by pushreply »

reneeb wrote:So the select doesn't exist in the HTML form? Then you have to check in the *done* method if that dropdown exists. If it exists, just append the data, otherwise you have to create the dropdown first...
yes, it does exist, but if I create it explicitly in the dtl.
If it's created using dynamic_field (type: Dropdown), it appears (rendered by the browser), but does not show up in the HTML source...

in the firebug console log, there is GET --> from "externalData.pl"
and
POST http://localhost:81/otrs/index.pl?

I think, the HTML form (consists of dynamic fields) generated by "AgentTicketProcess.dtl" is sent to index.pl.

Code: Select all

<div class="WidgetSimple">
            <div class="Header">
                <h2>Process Information</h2>
            </div>
....
Another thing:
The data is loaded AFTER the "AgentTicketProcess.dtl" file is loaded, BEFORE a process (-ticket) has not been selected.
Suppose I have more than 1 process, the data from "externalData.pl" will always be loaded up first, regardless which process I've selected.
My objective is to load the data from "externalData.pl" to the "dynamic_fields based form" (which is hidden until a process is selected).

I know how to create (hardcoded) html form, but not in this (ajax) case..
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Dynamic dropdown field from db

Post by reneeb »

So you have to do the AJAX call within "Core.AJAX.FunctionCall" (in AgentTicketProcess.dtl) in the else-if-branch...
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
pushreply
Znuny newbie
Posts: 14
Joined: 17 Apr 2013, 16:32
Znuny Version: 3.2.4
Real Name: Pu Shrep

Re: Dynamic dropdown field from db

Post by pushreply »

reneeb wrote:So you have to do the AJAX call within "Core.AJAX.FunctionCall" (in AgentTicketProcess.dtl) in the else-if-branch...
so, here is what I did:
inside the javascript "Core.AJAX.FunctionCall(.."

Code: Select all

var $ElementToUpdate = $('#ActivityDialogContent'),
                    JavaScriptString = '';
				var request = $.ajax({
					type:'GET',
					url: 'externaldata.pl',
					dataType: 'html'
					});
                if (!Response) {...
inside the "Core.AJAX.FunctionCall(..." ELSE-IF:

Code: Select all

.....
                    $ElementToUpdate.fadeIn();
					alert('start -request.done-');
					request.done(function(data){
						$('#DynamicField_Ticket1WeekOverdue').append(data);
						alert('start console');
						console.log(data);
					});
                    try {
                        /*jslint evil: true */....
Result is loaded into the dropdownlist :)

But:
if I change the selection in the dropdown list, the result is disappeared...:(

--------------------
UPDATE
to make it clearer:

Code: Select all

 Core.AJAX.FunctionCall('$Env{"Baselink"}', 'Action=$Env{"Action"};Subaction=DisplayActivityDialogAJAX;AJAXDialog=1;ProcessEntityID=' + $('#ProcessEntityID').val(), function (Response) {
                var $ElementToUpdate = $('#ActivityDialogContent'),
                    JavaScriptString = '';
				
                if (!Response) {

                    // We are out of the OTRS App scope, that's why an exception would not be caught. Therefor we handle the error manually.
                    Core.Exception.HandleFinalError(new Core.Exception.ApplicationError("No content received.", 'CommunicationError'));
                    $('#AJAXLoader').addClass('Hidden');
                }
                else if ($ElementToUpdate && isJQueryObject($ElementToUpdate) && $ElementToUpdate.length) {
                    $ElementToUpdate.get(0).innerHTML = Response;
                    $ElementToUpdate.find('script').each(function() {
                        JavaScriptString += $(this).text();
                        $(this).remove();
                    });
                    $ElementToUpdate.fadeIn();
					
                    try {
                        /*jslint evil: true */
                        eval(JavaScriptString);
                    }
                    catch (Exception) {}
                    Core.Form.Validate.Init();
                    $('#AJAXLoader').addClass('Hidden');
                    $('#AJAXDialog').val('1');
the process ticket form is displayed.

then I inserted this code right after "$('#AJAXDialog').val('1');" :

Code: Select all

var $request = $.ajax({
						type:'GET',
						url: 'externaldata.pl',
						dataType: 'html'
					});
					$request.done(function(data){
						alert('append values');
						$('#DynamicField_Ticket1WeekOverdue').append(data);
						alert('start console');
					console.log(data);
					});
the values are loaded to the dropdownlist ("DynamicField_Ticket1WeekOverdue")

After selecting another value from the dropdownlist, all values are disappeared from the dropdownlist.
ps: I put $ sign in front of the var decl. only to keep the consistency with other vars decl.
Amukinado
Znuny newbie
Posts: 76
Joined: 02 Jun 2010, 15:52
Znuny Version: 3.2.8
Location: Portugal

Re: Dynamic dropdown field from db

Post by Amukinado »

Hello. Not trying to double ask on different threads, but can i do this for this purpose?

viewtopic.php?f=61&t=21320

I'm not shure if I can put code or what kind of code can I input on CI definitions, so any help will be good.

Thanks
OTRS V3.2.8 - ITSM 3.2.6 - Cent OS 5.9x64
danny2001k
Znuny newbie
Posts: 6
Joined: 07 Nov 2013, 14:36
Znuny Version: 3.2.12

Re: Dynamic dropdown field from db

Post by danny2001k »

Hello,

A little help pls:
I created the extdata.pl and it works

Added a Dinamic filed in dtl file and added the javascript code:

Code: Select all

<script type="text/javascript">
var request = $.ajax({
       type: 'GET',
       url: 'extdata.pl',
       dataType: 'html'
    });
request.done( function(data) {
$('#DynamicField_contclust').append(data);
alert(data);
});
</script>
In the alert i get the correct output i want in my file:
<option="4">Others</option>
<option="5">Press</option>
<option="6">Feedback</option>
<option="7">Product & services</option>

but when I look in the loaded file source I get:
<select id="DynamicField_contclust" aria-invalid="false" name="DynamicField_contclust">Others Press Feedback Product & services </select>
so missing all the html tags... any ideea why?
ty danny
crythias
Moderator
Posts: 10169
Joined: 04 May 2010, 18:38
Znuny Version: 5.0.x
Location: SouthWest Florida, USA
Contact:

Re: Dynamic dropdown field from db

Post by crythias »

danny2001k wrote:<option="4">Others</option>
<option="5">Press</option>
<option="6">Feedback</option>
<option="7">Product & services</option>
<option value="x"> is correct. <option="x"> is incorrect.
OTRS 6.0.x (private/testing/public) on Linux with MySQL database.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask
danny2001k
Znuny newbie
Posts: 6
Joined: 07 Nov 2013, 14:36
Znuny Version: 3.2.12

Re: Dynamic dropdown field from db

Post by danny2001k »

crythias wrote: <option value="x"> is correct. <option="x"> is incorrect.
Ty, that solved it... simple mistake, and I looked at the JS for 3 hours :)
crythias
Moderator
Posts: 10169
Joined: 04 May 2010, 18:38
Znuny Version: 5.0.x
Location: SouthWest Florida, USA
Contact:

Re: Dynamic dropdown field from db

Post by crythias »

Took me a bit to look at it, too. I tried jsfiddle to address. Same deal. Changed ajax to a var data="<option...>".and still same result. then copied from a "working" option list and ... oh. yeah. :)
OTRS 6.0.x (private/testing/public) on Linux with MySQL database.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask
Post Reply