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

Asked

Viewed 675 times

3

I have a table where there are filters for it: inserir a descrição da imagem aqui

As you can see in the image, for each column I have a filter where user can type whatever they want to do the search they understand. Now I am allowing the user to download the listing in PDF format with the filters applied. So I’m creating an array with filters where I then send the same array to a function in an application controller. So far so good!

Problem:

If the user in a filter writes for example Belém, Lda, and since there is a comma in this filter, I will create another position in the array and not create a complete string at an array position.

What I’m thinking of doing is: divide each position in the array with a character that the user does not use, such as a (Smile)

Doubt:

To fetch the filter data I am using (I use Devexpress where javascript functions are available to handle data, in this case the filterEditorState to retrieve filter data):

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 together in an array, always putting one at the beginning and end of each string.

It is possible to immediately construct an array and change the position divider (comma)? So I avoided making N lines and simplifying my code.

  • Regarding "Smile" I think it’s better to have a string type password. I still don’t understand the problem. You want to change the array you receive in var myArray = gvSortingListagemGARs.filterEditorState; or find an alternative to generate the array?

  • I wanted to find an alternative to generating the array. Where I could work around the comma question in a filter string. The solution I thought was to filter the respective string, but then I wondered if there is another solution to solve this

  • But even with sub-arrays and if you use split, a search string can be compromised (for example Belém, Lda)

1 answer

2


One possibility is to go to the DOM and search for 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 kept.

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

Example: http://jsfiddle.net/XT4N6/

Notes:

  • you might want to use return $(this).find('input').val(); if these fields are inputs
  • use the .get() to obtain a native array, not an "array-like" object from jQuery

Browser other questions tagged

You are not signed in. Login or sign up in order to post.