skip to Main Content

I have a db table with fields like this

posts table

'id'
'group_id'
'name'

I display this table as a list in kartik Select2 widget

$form->field($model, 'post')
    ->widget(
        kartikselect2Select2::className(),
        [
            'data' => ArrayHelper::map(Post::find()->all(), 'id', 'name'),
            'options' => [
                'placeholder' => Yii::t('modules', 'Choose post'),
                'multiple' => true
            ],
            'pluginOptions' => [
                'allowClear' => true,
            ],
        ]
    )->label('');

Now I have in the list all posts in a row from the table are displayed

But I have a group_id field in this table, and the corresponding groups table

'id'
'name'

How can I make it so that the name field from the groups table is displayed as a subtitle in the select and all records are respectively divided into groups depending on the group_id with the corresponding subtitle?

I try something like this but end up with the same group name and the same post name multiple times in the list

$result = [];

$posts = ArrayHelper::map(Post::find()->all(), 'id', 'name');
$groups = Group::find()->all();

foreach ($posts as $post) {
    $groups = $groups[$post->group_id] ?? null;
    if (is_null($groups)) {
        $result[$post->id] = $post->name;
    } else {
        $line = [];
        foreach ($groups as $group) {
            $line[$post->group_id . '-' . $group->id] = $group->name;
        }
        $result[$post->name] = $line;
    }
}

2

Answers


  1. Adding subtitles or tooltips to items in the Kartik Select2 widget in Yii2 can be quite useful for providing additional information to users when they hover over or select an option. Unfortunately, as of my last knowledge update in September 2021, the Kartik Select2 widget doesn’t directly support subtitles or tooltips out of the box. However, you can achieve this effect by customizing the widget and using some JavaScript/jQuery.

    Here’s a general outline of how you might approach this:

    Customize the data format: Modify the data format of the items you provide to the Select2 widget. Instead of just providing an array of items with text and values, you would need to include additional data for subtitles/tooltips.

    Modify the widget configuration: In your view file (typically a create or update view), set up the Kartik Select2 widget with the modified data format and necessary configuration options. You’ll need to include a template for rendering the subtitles/tooltips.

    Custom JavaScript/jQuery code: Add JavaScript/jQuery code to enhance the behavior of the Select2 widget. This code will handle rendering the subtitles/tooltips when the user interacts with the widget.

    Here’s a basic example to get you started:

    Assuming you have a model named Item with attributes id, name, and description, and you want to display the name as the main text and description as the subtitle/tooltip.

    1.In your controller action, fetch the data and pass it to the view:

    public function actionCreate()
    {
        $items = Item::find()->all();
    
        return $this->render('create', [
            'items' => $items,
        ]);
    }
    
    1. In your create view, set up the Kartik Select2 widget

      echo $form->field($model, 'item_ids')->widget(Select2::classname(), ['data' => yiihelpersArrayHelper::map($items, 'id', 'name'), // Main text
      'options' => ['multiple' => true, 'placeholder' => 'Select items...'],
      'pluginOptions' => [
          'templateResult' => new yiiwebJsExpression('function(item) {
              if (!item.id) { return item.text; }
              return $("<span>" + item.text + "</span><div>" + item.description + "</div>");
          }'),
          'escapeMarkup' => new yiiwebJsExpression('function(markup) {
              return markup;
          }'),
      ],]);
      
    Login or Signup to reply.
  2. According to documentation https://demos.krajee.com/widget-details/select2#settings solution that will suit your needs would be nested array where key would be group name and items would be posts.

    With that structure you will get same output result as in examples with countries and time zones.

    Considering your items have following values:

    id, name, group_id:
    item_id_1, item_name_1, group_id_1, 
    item_id_2, item_name_2, group_id_1,
    item_id_xy, item_name_xy, group_id_2
    

    you can map your items with ArrayHelper::map() function:

    ArrayHelper::map(Post::find()->all(), 'id', 'name', 'group_id')
    

    resulting array would have following structure

    [
       'group_id_1' => [
          'item_id_1' => 'item_name_1,
          'item_id_2' => 'item_name_2,
       ],
       'group_id_2' => [
          'item_id_xy' => 'item_name_xy'
       ]
    ]
    

    then you can iterate that array and fetch group names from database and put as key instead of groupId to display group name, and append to item names if you need more detail group names

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search