json-rules-filter 1.0.23 → 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.23",
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,13 +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
- "overflow-x": "hidden"
195
+ "overflow-x": "hidden",
196
+ "margin-right": "-10px",
197
+ "padding-right": "10px"
172
198
  })
173
199
  }
174
200
  },
@@ -206,8 +232,10 @@
206
232
  // Add new rule row
207
233
  this.$contenedor.find(".dropdown-rules-item").on("click", function (e) {
208
234
  e.preventDefault();
209
- self.addRuleRow($(this).data("column-type"), $(this).data("column-data"), $(this).data("column-name"));
210
- $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
+ }
211
239
  });
212
240
 
213
241
  // Clear all rules
@@ -223,14 +251,8 @@
223
251
  });
224
252
  },
225
253
 
226
- // Resets rule container and re-runs filtering (show all)
227
- reset: function () {
228
- $(".container-rules-filters").empty();
229
- this.applyRules();
230
- },
231
-
232
254
  // Adds a new rule row dynamically
233
- addRuleRow: function (type, dataField, name) {
255
+ addRuleRow: function (type, dataField, name, selectedValues, searchValue) {
234
256
  const self = this;
235
257
  const $container = this.$contenedor.find(".container-rules-filters");
236
258
 
@@ -247,7 +269,7 @@
247
269
  const searchInput = `
248
270
  <div class="col-7">
249
271
  <label class="${labelClass}"><span class="text-danger">*</span>${this.settings.language.searchLabel[type]}</label>
250
- <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}">
251
273
  </div>`;
252
274
 
253
275
  const row = `
@@ -269,13 +291,15 @@
269
291
  $container.append(row);
270
292
  // Fetch custom render function from settings
271
293
  const render = self.settings.filters.find((e) => e.data === dataField).render;
272
- this.initSelect2(id_select, type, dataField, render);
294
+ // Initialice select2
295
+ this.initSelect2(id_select, type, dataField, selectedValues, render);
273
296
  },
274
297
 
275
298
  // Initialize Select2 plugin on the newly created dropdown
276
- initSelect2: function (id, type, dataField, render) {
299
+ initSelect2: function (id, type, dataField, selectedValues, render) {
277
300
  const self = this;
278
301
  const $select = this.$contenedor.find(`#select-rule-option-${id}`);
302
+ let data = type == 'select' ? this.getSelectValues(dataField, selectedValues) : this.getRuleOptions(type);
279
303
 
280
304
  $select.select2({
281
305
  width: '100%',
@@ -284,7 +308,7 @@
284
308
  templateSelection: render,
285
309
  templateResult: render,
286
310
  // Load dataset based on type: unique values for 'select', operators for others
287
- data: type == 'select' ? this.getSelectValues(dataField) : this.getRuleOptions(type)
311
+ data: data
288
312
  });
289
313
 
290
314
  // Remove rule event
@@ -292,6 +316,9 @@
292
316
  e.preventDefault();
293
317
  $(`#select-rule-container-${id}`).remove();
294
318
  });
319
+
320
+ // Preselected values
321
+ $select.val(selectedValues).trigger('change');
295
322
  },
296
323
 
297
324
  // Helper to map operator objects from language settings
@@ -303,16 +330,35 @@
303
330
  },
304
331
 
305
332
  // Extract unique values from data to populate 'select' type filters
306
- getSelectValues: function (dataField) {
333
+ getSelectValues: function (dataField, selectedValues) {
307
334
  const getValueByPath = (obj, path) => {
308
335
  return path.split('.').reduce((acc, part) => acc && acc[part], obj);
309
336
  };
310
337
 
311
- // Using Set + JSON stringify/parse to ensure unique objects in the dropdown
312
- return [...new Set(this.data.map(function (item) {
313
- const val = getValueByPath(item, dataField);
314
- return JSON.stringify({ id: val, text: val });
315
- }))].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;
316
362
  },
317
363
 
318
364
  // Core filtering engine