How to load an array and divide its positions by special character

3

I have a table where there are filters for it:

Asyoucanseeintheimage,foreachcolumnIhaveafilterwhereuserscantypewhattheyunderstandtodotheresearchtheyunderstand.IamnowallowingtheusertodownloadthePDFlistingwiththefiltersapplied.SoI'mcreatinganarraywiththefilterswhereIthensendthesamearraytoafunctioninanapplicationcontroller.Sogood!

Problem:

IftheuserinafilterwritesforexampleBelém,Lda,andsincethereisacommainthisfilter,Iwillcreateanotherpositioninthearrayandnotcreateacompletestringatanarrayposition.

WhatI'mthinkingofdoingis:divideeachpositioninthearraywithacharacterthattheuserdoesnotuse,suchas(smile)

Doubt:

TogetthefilterdataI'musing(usecomponent DevExpress where they provide javascript functions to be able manipulate data, in this case filterEditorState to fetch the data from the filters):

var myArray = gvSortingListagemGARs.filterEditorState; //Devolve um array com todos os filtros

I know I can build the array as follows:

var pos1 = gvSortingListagemGARs.filterEditorState[1];
var pos2 = gvSortingListagemGARs.filterEditorState[2];
var pos3 = gvSortingListagemGARs.filterEditorState[3];
...

And then put all the data in an array, always putting at the beginning and end of each string.

Is it possible to build an array soon and change the position divider? So I avoided making N lines and simplifying my code.

    
asked by anonymous 04.08.2014 / 11:42

1 answer

2

One possibility is to go to the DOM to fetch these values and create an array with them. Using JSON.stringify, you can pass to the server side in JSON format and then the commas within each string are retained.

var array = $('table tr:first td').map(function () {
    return $(this).text(); 
}).get(); 
var JSONarray = JSON.stringify(array); 

Example: link

Notes:

  • You may want to use return $(this).find('input').val(); if these fields are inputs
  • I use .get() to get a native array, not a jQuery array-like object
04.08.2014 / 12:58