Giter Site home page Giter Site logo

Comments (4)

vova07 avatar vova07 commented on May 27, 2024

Hi @pandeyamit,
Can you give some code example of your widget settings?
If your model attribute is a single value, or an array, the widget my set the values by default.

from yii2-select2-widget.

pandeyamit avatar pandeyamit commented on May 27, 2024

Hello,
thx for quick reply. Here is my code example:

Select Company field($model, 'company_id')->widget(Widget::className(), [ 'options' => [ 'multiple' => true, 'placeholder' => 'Choose Companies', ], 'settings' => [ 'width' => '50%', ], 'items' => $dataList, 'attribute' => 'name', ]); ?>

I want to show the selected companies of the user. I have a table user_companies having fields
id, user_id, company_id.
e.g one user has many companies association is used.

from yii2-select2-widget.

vova07 avatar vova07 commented on May 27, 2024

You need to set company_id attribute like an array with selected id of user's companies.
Short instruction:

  • If company_id is the real user's model attribute, or it's used in another places in your code, add a new attribute for this case, for example companyArray.
// User.php (user model)
protected $_companyArray;
  • Set the needed value for your companyArray attribute in the same model.
// User.php (user model)

// Setter for $_companyArray
public function setCompanyArray($value)
{
    $this->_companyArray = $value;
}

// Getter for $_companyArray
public function getCompanyArray()
{
    // Note $this->companies is the has many relation that must be specified in "User" model 
    if ($this->_companyArray === null && !empty($this->companies)) {
        $this->_companyArray = [];
        foreach ($this->companies as $company) {
            $this->_companyArray[] = $company->id;       
        }
    }
    return $this->_companyArray;
}
  • Use new attribute in your _form view. ... field($model, 'companyArray') ...
  • Profit!

from yii2-select2-widget.

pandeyamit avatar pandeyamit commented on May 27, 2024

Hello,

Thanks for your answer, I will try to do by this way.

On Thu, Jan 15, 2015 at 3:08 PM, Vasile C. [email protected] wrote:

You need to set company_id attribute like an array with selected id of
user companies.
Short instruction:

  • If company_id is the real user's model attribute, or it's used in
    another places in your code, add a new attribute for this case, for example
    companyArray.

// User.php (user model)protected $_companyArray;

  • Set the needed value for your companyArray attribute in the same
    model.

// User.php (user model)// Setter for $_companyArraypublic function setCompanyArray($value){ $this->_companyArray = $value;}// Getter for $_companyArraypublic function getCompanyArray(){ // Note $this->companies is the has many relation that must be specified in "User" model if ($this->_companyArray === null && !empty($this->companies)) { $this->_companyArray = []; foreach ($this->companies as $company) { $this->_companyArray[] = $company->id; } } return $this->_companyArray;}

  • Use new attribute in your _form view. ... field($model,
    'companyArray') ...
  • Profit!


Reply to this email directly or view it on GitHub
#2 (comment)
.

Thanks.

Best Regards !!

Amit Pandey
Sr. Software Engineer.
Soarlogic Information Technology Pvt. Ltd.

Mobile :- 9012474075
Mail to:- [email protected]

from yii2-select2-widget.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.