Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script type="text/javascript">
- let selectedId = $('#state_id').data('selected-id');
- if (selectedId > 0) {
- $('#state_id').val(selectedId).trigger('change');
- }
- window.initAllSelects = function() {
- $('select[data-selected]').each(function() {
- const $el = $(this);
- let val = $el.data('selected');
- if ($el.prop("multiple") && !Array.isArray(val)) {
- val = [val];
- }
- $el.val(val).trigger("change");
- });
- };
- initAllSelects();
- window.setSelectedValues = function(selectors)
- {
- // Convert single string to array
- if (typeof selectors === "string") {
- selectors = [selectors];
- }
- selectors.forEach(selector => {
- const $element = $(selector);
- if ($element.length) {
- let selectedValue = $element.data("selected");
- // Skip if selectedValue is empty string or 0
- if (selectedValue === "" || selectedValue === 0 || selectedValue === undefined) {
- return; // skip this element
- }
- // Handle multi-select
- if ($element.prop("multiple") && !Array.isArray(selectedValue)) {
- selectedValue = [selectedValue]; // convert single value to array
- }
- $element.val(selectedValue).trigger("change");
- }
- });
- };
- // Single select
- setSelectedValues("#gender_id");
- // Multiple select elements
- setSelectedValues([
- "#gender_id",
- "#blood_group_id",
- "#marital_status_id",
- "#nationality_id",
- "#present_state_id",
- "#permanent_state_id",
- "#department_id",
- "#designation_id",
- "#job_type_id",
- "#reporting_to",
- "#shift_id",
- "#status",
- ]);
- // Container Inside Value Selected
- window.applySelectedValues = function(container)
- {
- // Convert single string to array
- if (typeof container === 'string') {
- container = [container];
- }
- // If not array, make it an array
- if (!Array.isArray(container)) return;
- container.forEach(c => {
- const $container = c ? $(c) : $(document);
- $container.find('select[data-selected]').each(function ()
- {
- let selected = $(this).data('selected');
- if (selected === undefined || selected === null) return;
- // If select is multiple and value is string → parse JSON
- if (this.multiple && typeof selected === 'string') {
- try {
- selected = JSON.parse(selected);
- } catch (e) {
- selected = [];
- }
- }
- // Convert everything to string (important)
- if (Array.isArray(selected)) {
- selected = selected.map(String);
- } else {
- selected = String(selected);
- }
- $(this).val(selected).trigger('change');
- });
- });
- };
- applySelectedValues('#earning_table'); // Single selector
- applySelectedValues(['#earning_table', '#deduction_table', '#contribution_table']); // Multiple selectors
- <script>
Advertisement
Add Comment
Please, Sign In to add comment