Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8352861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:05:38+00:00 2026-06-09T09:05:38+00:00

Q1 : How to call a function of component from view? one of my

  • 0

Q1 : How to call a function of component from view?

one of my function is using most of the controllers.

public function actionDynamicdepartment()
    {

            //Department
            $data = Department::model()->findAll('p_id=0 AND company_id=:company_id', array(':company_id'=>(int) $_POST['company_id']));
            $data = CHtml::listData($data,'id','name');
            $dddepatment = "<option value=''>Please select a department</option>"; 
            foreach($data as $value=>$name)
                $dddepatment .= CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);

            //Section and Team
            $ddsection = "<option value=''>Please select a section</option>";
            $ddteam = "<option value=''>Please select a team</option>";

            // return data (JSON formatted)
            echo CJSON::encode(array(
              'dddepartment'=>$dddepatment,
              'ddsection'=>$ddsection,
              'ddteam'=>$ddteam
            ));
    }

I want to put it into component or some place.

And I want to call those function from my views. e.g

<div class="row">
    <?php echo $form->labelEx($model,'company_id'); ?>
    <?php
        $records = Company::model()->findAll();
        $company = CHtml::listData($records, 'id', 'name');

        echo $form->dropDownList($model,'company_id', $company, array('prompt'=>'Please select a company',
                'ajax' => array(
                    'type'=>'POST', //request type
                    'url'=>CController::createUrl('department/dynamicdepartment'), //url to call.
                    'dataType'=>'json',
                    'data'=>array('company_id'=>'js:this.value'),  
                    'success'=>'function(data) {
                        $("#FaMovehistory_department_id").html(data.dddepartment);
                        $("#FaMovehistory_section_id").html(data.ddsection);
                        $("#FaMovehistory_team_id").html(data.ddteam);
                    }',
                )
            )
        ); 
    ?>
    </div>

Or
Q2 : put those function at one of the controller (department.php). And can I call those function from different view?

Q3 : if do as Q2, is there any traffic?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:05:40+00:00Added an answer on June 9, 2026 at 9:05 am

    What I use to do is to define a CWidget (like Dmitry said) and then I create some functions (I tend to make them static, as if it was a library), so if, for instance, your Widget is called “Departments”, you could do something like this:

    Yii::import("application.components.Departments");
    Departments::actionDynamicdepartment();
    

    Pretty straightforward. You could, for this situation, return that CJson instead of echoing. However, you may not be interested in having a static response from this method.

    For your last questions, I tend to approach the population of dropdowns in a more classic manner, having an ajax call (I use jquery) requesting a central controller and passing some variables to it. That, of course, generates traffic.

    So, to sum up, if you want to recieve a list of departments and avoid changing it during the current page, you could go for a widget/component. If, on the other side, your dropdown needs to be responsive along with the rest of the items in a form, a controller’s action is your best (and probably unique) option.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to call a JavaScript function from icefaces check box component like
I'm trying to call a .NET component using interop from a classic ASP page
I am using JSON data from an AJAX call to build a url and
I have this function in the model of my component: public function getTotProperty($user){ $database
How to call function using parameters in powershell with parenthesis. I have as example
how do I call function from another class inside a different class? Here's my
How do you call function lol() from outside the $(document).ready() for example: $(document).ready(function(){ function
I'm trying to call a function in a Silverlight 4 component to open a
I read from the jQuery website, that get() loads data from the server using
I'm trying to get the success data from a jquery Ajax call so I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.