Skip to main content

OptionsBy

Struct OptionsBy 

Source
pub struct OptionsBy {
    pub field: String,
    pub options: BTreeMap<String, Vec<SelectOption>>,
}
Expand description

A select whose options depend on another column: the browser looks the row’s value of field up in options. A value with no entry offers no choices.

Fields§

§field: String§options: BTreeMap<String, Vec<SelectOption>>

Implementations§

Source§

impl OptionsBy

Source

pub fn new(field: impl Into<String>) -> Self

Examples found in repository?
examples/library/main.rs (line 134)
126    fn schema(&self, ctx: &Context) -> Result<Schema, ApiError> {
127        let genres = Self::genres(ctx)?;
128        let branches = Self::branches(ctx)?;
129
130        let mut names: Vec<&str> = genres.iter().map(|g| g.genre.as_str()).collect();
131        names.sort_unstable();
132        names.dedup();
133
134        let mut by_genre = OptionsBy::new("genre");
135        for genre in &names {
136            let subgenres: Vec<&str> = genres
137                .iter()
138                .filter(|g| g.genre == *genre)
139                .map(|g| g.subgenre.as_str())
140                .collect();
141            by_genre.insert(*genre, subgenres);
142        }
143
144        // Wide enough for the longest subgenre there is, computed here so the
145        // browser does not have to measure anything. It is the count of
146        // characters, nothing more: what the control puts around them is the
147        // bundle's business.
148        let widest = genres.iter().map(|g| g.subgenre.len()).max().unwrap_or(0);
149        let width_ch = u16::try_from(widest.max(8)).unwrap_or(u16::MAX);
150
151        // A key that shows a branch's name and stores its code.
152        let branch_keys: Vec<SelectOption> = branches
153            .iter()
154            .map(|b| SelectOption::labelled(&b.code, &b.name))
155            .collect();
156
157        let mut publishers: Vec<String> = ctx
158            .optional_rows::<Book>(BOOKS_FILE)?
159            .iter()
160            .map(|b| b.publisher.trim().to_string())
161            .filter(|p| !p.is_empty())
162            .collect();
163        publishers.sort_unstable();
164        publishers.dedup();
165
166        Ok(Schema::new([
167            Column::string("title", "Title").width_ch(30),
168            Column::string("author_first", "First").width_ch(12),
169            Column::string("author_last", "Last").width_ch(14),
170            Column::select("genre", "Genre", names)
171                .allow_empty()
172                .cascades_to(["subgenre"]),
173            Column::select_by("subgenre", "Subgenre", by_genre)
174                .allow_empty()
175                .width_ch(width_ch),
176            Column::select(
177                "edition",
178                "Edition",
179                [
180                    SelectOption::labelled("1", "First (1)"),
181                    SelectOption::labelled("2", "Second (2)"),
182                    SelectOption::labelled("3", "Third (3)"),
183                ],
184            )
185            .allow_empty()
186            .numeric_value(),
187            // A year is a whole number; a rating is not, which is what the
188            // absence of int_only means.
189            Column::number("year", "Year").int_only().width_ch(4),
190            Column::number("copies", "Copies").int_only().width_ch(3),
191            Column::number("rating", "Rating").width_ch(3),
192            Column::boolean("lent", "Lent"),
193            Column::string("publisher", "Publisher")
194                .width_ch(20)
195                .datalist("publishers"),
196            Column::string("donor", "Donated by")
197                .width_ch(20)
198                .datalist("reader-names"),
199            Column::spaced_string("call_number", "Call no.").width_ch(14),
200            Column::string("pronunciation", "Say")
201                .width_ch(16)
202                .speak(Speak::new(
203                    "http://127.0.0.1:8765/say?text={value}",
204                    "table-editor-speech-url",
205                )),
206            Column::map(
207                "shelved",
208                "Shelved",
209                MapSpec::new("Branch", "Copies")
210                    .chips_show_key()
211                    .key_options(branch_keys)
212                    .value_options([
213                        SelectOption::labelled("one", "One"),
214                        SelectOption::labelled("several", "Several"),
215                        SelectOption::labelled("many", "Many"),
216                    ]),
217            ),
218            Column::string("due", "Due").width_ch(10),
219            Column::string("link", "Catalogue").width_ch(30),
220            Column::computed("shelf", "Shelf mark", "shelf").width_ch(18),
221            Column::text("notes", "Notes").wide(),
222        ])
223        .sortable()
224        .datalist("publishers", Datalist::fixed(publishers))
225        .datalist(
226            "reader-names",
227            Datalist::from_rows(["author_last", "author_first"], ", "),
228        )
229        .new_row(
230            NewRow::new()
231                .with("title", "")
232                .with("author_first", "")
233                .with("author_last", "")
234                .with("genre", "")
235                .with("subgenre", "")
236                .with("publisher", "")
237                .with("donor", "")
238                .with("call_number", "")
239                .with("pronunciation", "")
240                .with("due", "")
241                .with("link", "")
242                .with("notes", "")
243                .with("copies", 1)
244                .carry_forward(["genre", "subgenre", "publisher"]),
245        ))
246    }
Source

pub fn with( self, value: impl Into<String>, options: impl IntoIterator<Item = impl Into<SelectOption>>, ) -> Self

Source

pub fn insert( &mut self, value: impl Into<String>, options: impl IntoIterator<Item = impl Into<SelectOption>>, )

Examples found in repository?
examples/library/main.rs (line 141)
126    fn schema(&self, ctx: &Context) -> Result<Schema, ApiError> {
127        let genres = Self::genres(ctx)?;
128        let branches = Self::branches(ctx)?;
129
130        let mut names: Vec<&str> = genres.iter().map(|g| g.genre.as_str()).collect();
131        names.sort_unstable();
132        names.dedup();
133
134        let mut by_genre = OptionsBy::new("genre");
135        for genre in &names {
136            let subgenres: Vec<&str> = genres
137                .iter()
138                .filter(|g| g.genre == *genre)
139                .map(|g| g.subgenre.as_str())
140                .collect();
141            by_genre.insert(*genre, subgenres);
142        }
143
144        // Wide enough for the longest subgenre there is, computed here so the
145        // browser does not have to measure anything. It is the count of
146        // characters, nothing more: what the control puts around them is the
147        // bundle's business.
148        let widest = genres.iter().map(|g| g.subgenre.len()).max().unwrap_or(0);
149        let width_ch = u16::try_from(widest.max(8)).unwrap_or(u16::MAX);
150
151        // A key that shows a branch's name and stores its code.
152        let branch_keys: Vec<SelectOption> = branches
153            .iter()
154            .map(|b| SelectOption::labelled(&b.code, &b.name))
155            .collect();
156
157        let mut publishers: Vec<String> = ctx
158            .optional_rows::<Book>(BOOKS_FILE)?
159            .iter()
160            .map(|b| b.publisher.trim().to_string())
161            .filter(|p| !p.is_empty())
162            .collect();
163        publishers.sort_unstable();
164        publishers.dedup();
165
166        Ok(Schema::new([
167            Column::string("title", "Title").width_ch(30),
168            Column::string("author_first", "First").width_ch(12),
169            Column::string("author_last", "Last").width_ch(14),
170            Column::select("genre", "Genre", names)
171                .allow_empty()
172                .cascades_to(["subgenre"]),
173            Column::select_by("subgenre", "Subgenre", by_genre)
174                .allow_empty()
175                .width_ch(width_ch),
176            Column::select(
177                "edition",
178                "Edition",
179                [
180                    SelectOption::labelled("1", "First (1)"),
181                    SelectOption::labelled("2", "Second (2)"),
182                    SelectOption::labelled("3", "Third (3)"),
183                ],
184            )
185            .allow_empty()
186            .numeric_value(),
187            // A year is a whole number; a rating is not, which is what the
188            // absence of int_only means.
189            Column::number("year", "Year").int_only().width_ch(4),
190            Column::number("copies", "Copies").int_only().width_ch(3),
191            Column::number("rating", "Rating").width_ch(3),
192            Column::boolean("lent", "Lent"),
193            Column::string("publisher", "Publisher")
194                .width_ch(20)
195                .datalist("publishers"),
196            Column::string("donor", "Donated by")
197                .width_ch(20)
198                .datalist("reader-names"),
199            Column::spaced_string("call_number", "Call no.").width_ch(14),
200            Column::string("pronunciation", "Say")
201                .width_ch(16)
202                .speak(Speak::new(
203                    "http://127.0.0.1:8765/say?text={value}",
204                    "table-editor-speech-url",
205                )),
206            Column::map(
207                "shelved",
208                "Shelved",
209                MapSpec::new("Branch", "Copies")
210                    .chips_show_key()
211                    .key_options(branch_keys)
212                    .value_options([
213                        SelectOption::labelled("one", "One"),
214                        SelectOption::labelled("several", "Several"),
215                        SelectOption::labelled("many", "Many"),
216                    ]),
217            ),
218            Column::string("due", "Due").width_ch(10),
219            Column::string("link", "Catalogue").width_ch(30),
220            Column::computed("shelf", "Shelf mark", "shelf").width_ch(18),
221            Column::text("notes", "Notes").wide(),
222        ])
223        .sortable()
224        .datalist("publishers", Datalist::fixed(publishers))
225        .datalist(
226            "reader-names",
227            Datalist::from_rows(["author_last", "author_first"], ", "),
228        )
229        .new_row(
230            NewRow::new()
231                .with("title", "")
232                .with("author_first", "")
233                .with("author_last", "")
234                .with("genre", "")
235                .with("subgenre", "")
236                .with("publisher", "")
237                .with("donor", "")
238                .with("call_number", "")
239                .with("pronunciation", "")
240                .with("due", "")
241                .with("link", "")
242                .with("notes", "")
243                .with("copies", 1)
244                .carry_forward(["genre", "subgenre", "publisher"]),
245        ))
246    }

Trait Implementations§

Source§

impl Clone for OptionsBy

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OptionsBy

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for OptionsBy

Source§

impl PartialEq for OptionsBy

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for OptionsBy

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for OptionsBy

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.