json-rules-filter 1.0.24 → 1.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-rules-filter",
3
- "version": "1.0.24",
3
+ "version": "1.0.25",
4
4
  "description": "para crear reglas avanzadas de filtrado sobre datasets JSON de forma dinámica y visual.",
5
5
  "main": "src/jquery.jsonRulesFilter.js",
6
6
  "scripts": {
@@ -82,12 +82,36 @@
82
82
  return result;
83
83
  },
84
84
 
85
+ // -------------------- PUBLIC FUNCTIONS ----------//
86
+
85
87
  // Updates dataset from external source and refreshes the UI
86
- updateData: function (newData) {
88
+ updateData: function (newData, callback) {
87
89
  this.data = newData;
88
- this.reset();
90
+ //Run callback del usuario
91
+ callback?.();
92
+ },
93
+
94
+ //Set pre-aplicated rules
95
+ preloadRules(rules) {
96
+ const self = this;
97
+ $.each(rules, function (clave, valor) {
98
+ console.log(valor);
99
+ self.addRuleRow(valor.typeFilter, valor.dataField, valor.dataName, valor.optionFilter, valor.searchValue);
100
+ });
101
+ this.applyRules();
102
+ },
103
+
104
+ // Resets rule container and re-runs filtering (show all)
105
+ reset: function (callback) {
106
+ const rules = this.getAplicatedRules();
107
+ $(".container-rules-filters").empty();
108
+ this.applyRules();
109
+ //Run callback del usuario
110
+ callback?.(rules);
89
111
  },
90
112
 
113
+ //--------------------------------------------------//
114
+
91
115
  // Collects all rules currently defined in the DOM
92
116
  getAplicatedRules: function () {
93
117
  const rules_items = $contenedor.find("select[name=select-rule-option]");
@@ -162,15 +186,15 @@
162
186
  this.$contenedor.append(template);
163
187
  if (this.settings.scrollY !== false) {
164
188
  const maxHeight = typeof this.settings.scrollY === 'number' ?
165
- this.settings.scrollY + 'px' :
166
- this.settings.scrollY;
189
+ this.settings.scrollY + 'px' :
190
+ this.settings.scrollY;
167
191
 
168
192
  this.$contenedor.find(".container-rules-filters").css({
169
193
  "max-height": maxHeight,
170
194
  "overflow-y": "auto",
171
195
  "overflow-x": "hidden",
172
196
  "margin-right": "-10px",
173
- "padding-right":"10px"
197
+ "padding-right": "10px"
174
198
  })
175
199
  }
176
200
  },
@@ -208,8 +232,10 @@
208
232
  // Add new rule row
209
233
  this.$contenedor.find(".dropdown-rules-item").on("click", function (e) {
210
234
  e.preventDefault();
211
- self.addRuleRow($(this).data("column-type"), $(this).data("column-data"), $(this).data("column-name"));
212
- $dropdown.dropdown("hide");
235
+ self.addRuleRow($(this).data("column-type"), $(this).data("column-data"), $(this).data("column-name"), "", "");
236
+ if (self.settings.bsVersion === 4) {
237
+ $dropdown.dropdown("hide");
238
+ }
213
239
  });
214
240
 
215
241
  // Clear all rules
@@ -225,14 +251,8 @@
225
251
  });
226
252
  },
227
253
 
228
- // Resets rule container and re-runs filtering (show all)
229
- reset: function () {
230
- $(".container-rules-filters").empty();
231
- this.applyRules();
232
- },
233
-
234
254
  // Adds a new rule row dynamically
235
- addRuleRow: function (type, dataField, name) {
255
+ addRuleRow: function (type, dataField, name, selectedValues, searchValue) {
236
256
  const self = this;
237
257
  const $container = this.$contenedor.find(".container-rules-filters");
238
258
 
@@ -249,7 +269,7 @@
249
269
  const searchInput = `
250
270
  <div class="col-7">
251
271
  <label class="${labelClass}"><span class="text-danger">*</span>${this.settings.language.searchLabel[type]}</label>
252
- <input type="text" class="form-control" name="input-rule-search" id="input-rule-search-${id_select}">
272
+ <input type="text" class="form-control" value="${searchValue}" name="input-rule-search" id="input-rule-search-${id_select}">
253
273
  </div>`;
254
274
 
255
275
  const row = `
@@ -271,13 +291,15 @@
271
291
  $container.append(row);
272
292
  // Fetch custom render function from settings
273
293
  const render = self.settings.filters.find((e) => e.data === dataField).render;
274
- this.initSelect2(id_select, type, dataField, render);
294
+ // Initialice select2
295
+ this.initSelect2(id_select, type, dataField, selectedValues, render);
275
296
  },
276
297
 
277
298
  // Initialize Select2 plugin on the newly created dropdown
278
- initSelect2: function (id, type, dataField, render) {
299
+ initSelect2: function (id, type, dataField, selectedValues, render) {
279
300
  const self = this;
280
301
  const $select = this.$contenedor.find(`#select-rule-option-${id}`);
302
+ let data = type == 'select' ? this.getSelectValues(dataField, selectedValues) : this.getRuleOptions(type);
281
303
 
282
304
  $select.select2({
283
305
  width: '100%',
@@ -286,7 +308,7 @@
286
308
  templateSelection: render,
287
309
  templateResult: render,
288
310
  // Load dataset based on type: unique values for 'select', operators for others
289
- data: type == 'select' ? this.getSelectValues(dataField) : this.getRuleOptions(type)
311
+ data: data
290
312
  });
291
313
 
292
314
  // Remove rule event
@@ -294,6 +316,9 @@
294
316
  e.preventDefault();
295
317
  $(`#select-rule-container-${id}`).remove();
296
318
  });
319
+
320
+ // Preselected values
321
+ $select.val(selectedValues).trigger('change');
297
322
  },
298
323
 
299
324
  // Helper to map operator objects from language settings
@@ -305,16 +330,35 @@
305
330
  },
306
331
 
307
332
  // Extract unique values from data to populate 'select' type filters
308
- getSelectValues: function (dataField) {
333
+ getSelectValues: function (dataField, selectedValues) {
309
334
  const getValueByPath = (obj, path) => {
310
335
  return path.split('.').reduce((acc, part) => acc && acc[part], obj);
311
336
  };
312
337
 
313
- // Using Set + JSON stringify/parse to ensure unique objects in the dropdown
314
- return [...new Set(this.data.map(function (item) {
315
- const val = getValueByPath(item, dataField);
316
- return JSON.stringify({ id: val, text: val });
317
- }))].map((e) => JSON.parse(e));
338
+ // 1. Extraemos los valores únicos de la base de datos actual
339
+ let uniqueOptions = [...new Set(this.data.map(function (item) {
340
+ const val = getValueByPath(item, dataField).toString();
341
+ if (val !== undefined && val !== null) {
342
+ return JSON.stringify({ id: val, text: val });
343
+ }
344
+ return null;
345
+ }).filter(item => item !== null))].map((e) => JSON.parse(e));
346
+
347
+ // 2. Al ser siempre un array, iteramos directamente si tiene contenido
348
+ if (selectedValues && selectedValues.length > 0) {
349
+ selectedValues.forEach(val => {
350
+ // Si el valor no existe ya en nuestras opciones únicas, lo empujamos
351
+
352
+ if (val && !uniqueOptions.some(opt => opt.id == val)) {
353
+ uniqueOptions.push({
354
+ id: val,
355
+ text: val
356
+ });
357
+ }
358
+ });
359
+ }
360
+
361
+ return uniqueOptions;
318
362
  },
319
363
 
320
364
  // Core filtering engine