Determine count of items in an editor 'select' item

Determine count of items in an editor 'select' item

icefieldicefield Posts: 45Questions: 19Answers: 1

I'm showing a select list of items (MapCategory) that a Map item can belong too. I want to disable the Create button in the Map editor UI until the user has created at least one MapCategory item. How can I determine the count of MapCategory items on the client side.

A snippet of code from the Map editor looks as follows:

    Editor::inst($db, 'Map', '_id')
        ->fields(
            <snip><snip><snip>
            Field::inst('Map._idCategory')
                ->options(Options::inst()
                    ->table('MapCategory')
                    ->value('_id')
                    ->label('name')
                )
                ->validator(Validate::dbValues()),
            Field::inst('MapCategory.name'),
            <snip><snip><snip>
        )
        ->leftJoin('MapCategory', 'MapCategory._id', '=', 'Map._idCategory')
        ->process($_POST)
        ->json();

The MapCategory is shown using a 'Select' control on the client (JavaScript) side. I'd like to disable 'Create' button on Map table until at least one MapCategory has been created. How can I get the count of MapCategory items in the editor above?

This question has accepted answers - jump to:

Answers

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

    On the client-side you could use field().val() to get the array and then get its length property - e.g.:

    editor.field('myFieldName').val().length
    

    Combine that with a little client-side validation and you can stop the user submitting until they've got at least one selected.

    Allan

  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    Answer ✓

    Hi @icefield ,

    If you want to disable the button, you can do something like this - this disables the New Entry form's Create button until there is something in the "Office" field. Hope that helps,

    Cheers,

    Colin

  • icefieldicefield Posts: 45Questions: 19Answers: 1

    Thank you both (Alan and Colin) very much. - Tom

This discussion has been closed.