Download the PHP package flsouto/htchoice without Composer
On this page you can find all versions of the php package flsouto/htchoice. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package htchoice
HtChoice
This library can be extended to easily implement widgets that are based on choice. It is not tied to any specific html element and does not define if the user is supposed to choose only one option or multiple options. This is up to your implementation.
Notice: a lot of functionality is already inherited from base abstract classes. So I highly recommend reading these other materials before you start developing with this library:
- [HtField]()
- [HtWidget]()
Installation
Use composer:
Usage
For demonstrating how one would extend this abstract class to implement his own choice type of widget, I
have prepared a SimpleChoice
class which provides a text-based list of options and an input field where
the user is supposed to type in the value of the desired option. Of course in the real world this would be
useless since better control elements such as select, radio buttons and checkboxes already exist for that.
However this simplified version will be good for showing how custom choice elements can
be implemented if you don't like sticking to the standard ones. So here is the implementation of our SimpleChoice
class:
Setting the array of options
In order to feed the choice widget with options for the user to choose we must use the options
method which accepts an associative array:
Outputs:
Selecting an option
In the next example we want the third option (i.e. the "Fall" season) to be marked as selected:
Outputs:
Readonly mode
We can use the inherited readonly
setter to tell the widget we don't want users interacting with it:
Outputs:
Options as Numeric Arrays
Besides associative arrays, the options
method accepts numeric arrays. In this case
each option's value will be the same as its own label:
Outputs:
Options as Datasets
Usually you will be fetching options from the database and these will come in the form of rows, also known as a
dataset structure. The options
setter understands that as well:
Outputs:
Notice: the column names don't need to be id
and name
. They can be anything as long as they occur in the first
and second positions of each row. In other words, the first column will always be the value, and the second column will always be the label.
Options as Array of Objects
This is the same as passing a dataset, only that each row is an object instead of an array:
Outputs:
Options as Tuples
The options
also accepts an array of tuples in the form: [value1, label1], [value2,label2], and so on...
Outputs:
Lazy Loaded Options
Last but not least, you can pass a function to the options
method for returning the options only just
before the widget is rendered. This is ideal, for instance, if you are fetching the options from the database:
Outputs:
Notice: the value returned by the lazy loader can be anything supported by the options
method:
associative arrays, numeric arrays, array of tuples, and even another function, as you can see in the example below:
Outputs: