📜  Primefaces InputSwitch(1)

📅  最后修改于: 2023-12-03 14:45:39.101000             🧑  作者: Mango

Primefaces InputSwitch

Introduction

Primefaces InputSwitch is a user interface (UI) component that allows the user to toggle between two states, "on" and "off". It provides a smooth and attractive way of selecting boolean values. InputSwitch is part of the Primefaces library, a popular open-source UI component suite for JavaServer Faces (JSF) applications.

Features
  • Customizable label for "on" and "off" states
  • Ability to set initial state (on/off)
  • Disabled/enabled state
  • Support for Primefaces theme and skinning
  • Built-in validation
Usage

To use Primefaces InputSwitch in your JSF application, you need to first include the Primefaces library in your project. You can download the Primefaces library from the official website or include it as a Maven or Gradle dependency.

Once you have included the library, you can use the p:inputSwitch tag to create an InputSwitch component. Here is an example:

<p:inputSwitch value="#{bean.value}" onLabel="Yes" offLabel="No" />

In this example, value is a model property in your JSF backing bean that holds the boolean value of the InputSwitch. The onLabel and offLabel attributes are used to set the label text for the "on" and "off" states, respectively.

You can also set the initial state of the InputSwitch using the checked attribute:

<p:inputSwitch value="#{bean.value}" onLabel="Yes" offLabel="No" checked="true" />

In addition, you can disable the InputSwitch by setting the disabled attribute to true:

<p:inputSwitch value="#{bean.value}" onLabel="Yes" offLabel="No" disabled="true" />
Validation

InputSwitch has built-in validation that ensures the submitted value is a boolean. You can display validation messages using the p:message component:

<p:message for="inputSwitchId" />

<p:inputSwitch id="inputSwitchId" value="#{bean.value}" onLabel="Yes" offLabel="No" required="true" />

In this example, required="true" is used to make the InputSwitch a required field. The p:message component displays any validation error messages for the InputSwitch.

Conclusion

Primefaces InputSwitch is a powerful and easy-to-use UI component that allows the user to toggle between two states. It has many useful features, such as customizable labels, initial state, and validation, which make it a great choice for any JSF application.