MJOIN with multiple select always shows multiple values

MJOIN with multiple select always shows multiple values

connectedsecurityconnectedsecurity Posts: 4Questions: 2Answers: 0

I am using an MJOIN as below.

$editor = Editor::inst( $db, 'product', 'product_id' )
                        ->fields(
                                Field::inst( DB_PREFIX . 'product.product_id' )
                             )
            ->join( 
                Mjoin::inst('category_description')
                    ->link('product.product_id', 'product_to_category.product_id')
                    ->link('category_description.category_id', 'product_to_category.category_id')
                    ->fields(
                        Field::inst('category_id')
                            ->options( Options::inst()
                                ->table('category_description')
                                ->value('category_id')
                                ->label('name')
                                ->render(function($row){
                                    return html_entity_decode($row['name']);
                                })
                            ),
                        Field::inst('name')
                    )
                )

with an editor instance set up like this

            {
                label: "Categories:",
                name:  "category_description[].category_id",
                type:  "select2",
                attr: {
                    multiple: true,
                    placeholder: 'Select categories'
                }               
            }

the select box always shows "multiple values" if more than 1 row is selected from the table despite the values being the same.

I have tried a normal select multiple box to rule out a plugin issue.

The correct values are show if only 1 row is selected.

The JSON data returned looks ok

Item 1

      "category_description": [
        {
          "category_id": "70",
          "name": "MyCategory"
        }
      ]

Item 2

      "category_description": [
        {
          "category_id": "70",
          "name": "MyCategory"
        }
      ]

Options

    "ct_category_description[].category_id": [
      {
        "label": "MyCategory",
        "value": "70"
      }
     ]

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    the select box always shows "multiple values" if more than 1 row is selected from the table despite the values being the same.

    This is because it is doing a shallow check - array1 !== array2 (or more accurately [1,2] !== [1,2]).

    What version of Editor is it you are using? There is actually a deep comparison function available in the latest versions and I though that this should work now. It certainly wouldn't prior to 1.6.

    Allan

  • connectedsecurityconnectedsecurity Posts: 4Questions: 2Answers: 0

    I am using the following versions

    JSZip 2.5.0, DataTables 1.10.13, AutoFill 2.1.3, Buttons 1.2.4, Column visibility 1.2.4, HTML5 export 1.2.4, Editor 1.6.1, Select 1.2.0

    does the deeper comparison require an additional parameter on the field?

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    No it shouldn't. I've just had a look through the source, and the deep compare is being used, but only to determine if values match when submitting them - not to determine if they are the same when displaying the form.

    That's a bug. I'm just about to look into what is required to fix that and will let you know what I find.

    Allan

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    If you search the Editor source code for:

    if ( i > 0 && val !== last ) {

    And replace that with:

    if ( i > 0 && ! _deepCompare( val, last ) ) {
    

    that will let this work as expected (worth noting that the order is important - the items in the array must be in the same order to be detected as the same - out of order means it is a different value).

    I'll have this change in 1.6.2.

    Regards,
    Allan

  • connectedsecurityconnectedsecurity Posts: 4Questions: 2Answers: 0

    That's fixed it,

    Many thanks

This discussion has been closed.