added new options in select

This commit is contained in:
MaxOvs19 2023-10-04 18:26:51 +03:00
parent 49478b316c
commit d5bbec2b7f
8 changed files with 76 additions and 10 deletions

View File

@ -85,3 +85,9 @@ Tested in JS and React. Errors in work in React applications are revealed.
- Optimized code. - Optimized code.
- Added error messages when using public methods. - Added error messages when using public methods.
### 04.10.2023 - update 0.3.2
- Added the ability to set the name attribute for further sending data to the server. (\*nameSelect: string)
- Added a function to POST a request to the server when selecting an item. (\*getRequestUrl: string)
- Optimized code.

View File

@ -1,6 +1,6 @@
# CG-SELECT # CG-SELECT
## version ~ 0.3.12 ## version ~ 0.3.2
<a href="https://github.com/apuc/cg-select/blob/main/READMERU.md">ЧИТАТЬ НА РУССКОМ</a> <a href="https://github.com/apuc/cg-select/blob/main/READMERU.md">ЧИТАТЬ НА РУССКОМ</a>
@ -30,6 +30,7 @@ npm i cg-select
``` ```
<script src="https://cdn.itguild.info/items/cg-select/latest/main.js"></script> <script src="https://cdn.itguild.info/items/cg-select/latest/main.js"></script>
``` ```
#### PHP #### PHP
Repository: <a href="https://git.itguild.info/apuc/php-cg-select-v2">PHP cg-select package integration</a> Repository: <a href="https://git.itguild.info/apuc/php-cg-select-v2">PHP cg-select package integration</a>
@ -156,8 +157,6 @@ All documentation on CG-SELECT is located in the folder of the same name. The do
<a href="https://cg-select.itguild.info/up_/documentation/index.html">Documentation</a> <a href="https://cg-select.itguild.info/up_/documentation/index.html">Documentation</a>
## Contributing ## Contributing
1. Fork it! 1. Fork it!

View File

@ -1,6 +1,6 @@
# CG-SELECT # CG-SELECT
## Версия ~ 0.3.12 ## Версия ~ 0.3.2
<a href="https://github.com/apuc/cg-select/blob/main/README.md">English README</a> <a href="https://github.com/apuc/cg-select/blob/main/README.md">English README</a>

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "cg-select", "name": "cg-select",
"version": "0.3.12", "version": "0.3.2",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {

View File

@ -1,6 +1,6 @@
{ {
"name": "cg-select", "name": "cg-select",
"version": "0.3.12", "version": "0.3.2",
"source": "index.js", "source": "index.js",
"main": "dist/main.js", "main": "dist/main.js",
"description": "Feature rich Select control for React/JS with multiselect, autocomplete and styling", "description": "Feature rich Select control for React/JS with multiselect, autocomplete and styling",

View File

@ -46,6 +46,8 @@ export default class CGSelect implements ICgSelect {
label?: string; label?: string;
styles?: IStyle; styles?: IStyle;
event?: string; event?: string;
nameSelect?: string;
getRequestUrl?: string;
url?: string; url?: string;
multiselect?: boolean; multiselect?: boolean;
multiselectTag?: boolean; multiselectTag?: boolean;
@ -95,6 +97,11 @@ export default class CGSelect implements ICgSelect {
* @type {Element | null} * @type {Element | null}
*/ */
private buttonAction?: Element | null; private buttonAction?: Element | null;
/**
* Array for request
* @type {IItems}
*/
private forUrlSelectedItems: IItems[] = [];
/** /**
* @param {ICgSelect} setting Object accepting select settings. * @param {ICgSelect} setting Object accepting select settings.
@ -113,6 +120,8 @@ export default class CGSelect implements ICgSelect {
nativeSelectMode: true/false, nativeSelectMode: true/false,
listDisplayMode: true/false, listDisplayMode: true/false,
language: 'ru/en', language: 'ru/en',
nameSelect: "titleYouSelect";
getRequestUrl: "/endpoint/123";
styles: { styles: {
head: { head: {
background: '...', background: '...',
@ -197,6 +206,8 @@ export default class CGSelect implements ICgSelect {
styles, styles,
label, label,
event, event,
nameSelect,
getRequestUrl,
selected, selected,
placeholder, placeholder,
theme, theme,
@ -216,6 +227,8 @@ export default class CGSelect implements ICgSelect {
this.styles = styles; this.styles = styles;
this.label = label; this.label = label;
this.event = event; this.event = event;
this.nameSelect = nameSelect;
this.getRequestUrl = getRequestUrl;
this.selected = selected; this.selected = selected;
this.placeholder = placeholder; this.placeholder = placeholder;
this.theme = theme; this.theme = theme;
@ -283,7 +296,7 @@ export default class CGSelect implements ICgSelect {
} }
const ulList = document.createElement('ul'); const ulList = document.createElement('ul');
const nativeSelect = createNativeSelect(); const nativeSelect = createNativeSelect(this.nameSelect);
let inputSearch: HTMLInputElement; let inputSearch: HTMLInputElement;
let textNode: Text; let textNode: Text;
@ -384,7 +397,7 @@ export default class CGSelect implements ICgSelect {
const response = await fetch(this.url!); const response = await fetch(this.url!);
const dataUrl = await response.json(); const dataUrl = await response.json();
const nativeSelect = createNativeSelect(); const nativeSelect = createNativeSelect(this.nameSelect!);
dataUrl.forEach((dataItem: IItems, index: number) => { dataUrl.forEach((dataItem: IItems, index: number) => {
const item = { const item = {
@ -613,6 +626,17 @@ export default class CGSelect implements ICgSelect {
select!.innerText = this.selectedItems.join(','); select!.innerText = this.selectedItems.join(',');
} }
} }
if (this.getRequestUrl!) {
this.forUrlSelectedItems.push(item);
fetch(this.getRequestUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8',
},
body: JSON.stringify(this.forUrlSelectedItems),
});
}
} else { } else {
if (this.multiselectTag) { if (this.multiselectTag) {
const tagItem = document.getElementById(`tag-${index}-${item.id}`); const tagItem = document.getElementById(`tag-${index}-${item.id}`);
@ -622,6 +646,17 @@ export default class CGSelect implements ICgSelect {
if (Array.isArray(this.selectedItems)) { if (Array.isArray(this.selectedItems)) {
this.selectedItems.splice(checkIndex, 1); this.selectedItems.splice(checkIndex, 1);
this.indexes.splice(checkIndex, 1); this.indexes.splice(checkIndex, 1);
if (this.getRequestUrl! && this.forUrlSelectedItems.length > 0) {
this.forUrlSelectedItems.splice(checkIndex, 1);
fetch(this.getRequestUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8',
},
body: JSON.stringify(this.forUrlSelectedItems),
});
}
nativeOptionMultiple(nativeOption, item.title, false); nativeOptionMultiple(nativeOption, item.title, false);
} }
} }
@ -648,6 +683,16 @@ export default class CGSelect implements ICgSelect {
option.classList.remove('active'); option.classList.remove('active');
}); });
option.classList.add('active'); option.classList.add('active');
if (this.getRequestUrl!) {
fetch(this.getRequestUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8',
},
body: JSON.stringify(item),
});
}
} }
clearSelect(select!, this.element!, selectedItemsClear); clearSelect(select!, this.element!, selectedItemsClear);

View File

@ -5,10 +5,14 @@ import { ICreateBreadCrumb } from './create-element.interface';
* The method that creates the native select. * The method that creates the native select.
* @returns {HTMLSelectElement} Returns the created native select. * @returns {HTMLSelectElement} Returns the created native select.
*/ */
export function createNativeSelect(): HTMLSelectElement { export function createNativeSelect(nameSelect: string | undefined): HTMLSelectElement {
const nativeSelect = document.createElement('select'); const nativeSelect = document.createElement('select');
nativeSelect.setAttribute('name', 'dataSelect'); if (nameSelect == undefined) {
nativeSelect.setAttribute('name', 'CgSelect');
} else {
nativeSelect.setAttribute('name', nameSelect!);
}
nativeSelect.classList.add('nativeSelect'); nativeSelect.classList.add('nativeSelect');
return nativeSelect; return nativeSelect;
} }

View File

@ -75,6 +75,18 @@ export interface ICgSelect {
* @type {string} * @type {string}
*/ */
event?: string; event?: string;
/**
* Optional parameter responsible for the native select attribute.
* @type {string}
*/
nameSelect?: string;
/**
* Optional parameter responsible for the sending address when selected in the select.
* @type {string}
*/
getRequestUrl?: string;
/** /**
* Required parameter (if no other way to get data (items) is specified), * Required parameter (if no other way to get data (items) is specified),
* data that comes from the backend in the format { id: "", title: "", value: ""}. * data that comes from the backend in the format { id: "", title: "", value: ""}.