-1

I'm asking a question based on my previous question JQuery-csv not parsing all values ,this question will be a workaround on my problem which was solved but still needed a bit tweaking where the overflowed cell won't show up, but I can't figure out where or how to start with it.

My csv file looks like this: test.csv

header1, header2, header3, header4
value1, value2, value3, value4
value1, value2, value3, value4.1,value4.2,value4.3
value1, value2, value3, value4

Im parsing the csv using csv.parsers.splitLines , but this time I would like to check line by line that after the 3rd comma , I'll get all the remaining strings and replace the comma with some other separator like a colon :. ex:

value1, value2, value3, value4.1,value4.2,value4.3 --> value1, value2, value3, value4.1-value4.2-value4.3

at the moment I still don't know where to start in this issue a good nudge in this would be awesome. cheers!!

JSFiddle for my ideal output:

http://jsfiddle.net/xpvt214o/690880/

Dip Hasan
  • 225
  • 3
  • 9
rockStar
  • 1,296
  • 12
  • 22
  • 1
    split the string by comma (str.split(',')) and make sure you trim any whitespace of each of those elements after splitting. Then, iterate through the split array - if the element is index < 3 then output to your string a simple comma, if it is >= 3 then output to your string the value + ' --> '. – Chris Cousins Aug 27 '18 at 18:53
  • Precisely what output do you want to obtain from that CSV input? – David Thomas Aug 27 '18 at 20:44
  • @ChrisCousins that's precisely what i needed, i'll give a go what u have said but now im thinking how would i join the split string. – rockStar Aug 28 '18 at 06:14
  • @DavidThomas i have added a jsfiddle on my ideal output and stuff, hope it clarify what im missing – rockStar Aug 28 '18 at 06:15
  • @ChrisCousins im using the method csv.toArrays from `jquery-csv` this is convenient in making a 2 dimensional array from the data but i cant wrap my mind around how to iterate and at the same time add those overflowed data into one cell. http://jsfiddle.net/xpvt214o/691107/ – rockStar Aug 28 '18 at 06:52

1 Answers1

1

Fine, here we go:

            var separator = ",",
                agregator = "-";
            function generateTable(lines) {
                if (typeof(lines) === 'undefined' || lines.length == 0) {
                    return '';
                }
                var header = lines[0].split(separator);
                var html = '';
                var rows = [];
                // mapping
                for (var row in lines) {
                    if(row == 0) {
                        continue;
                    }
                    var cols = lines[row].split(separator),
                        values = {};
                    for (var col in cols) {
                        var item = header[col] ? header[col] : header[header.length-1];
                        if(values[item]) {
                            values[item].push(cols[col]);
                        } else {
                            values[item] = [cols[col]];
                        }
                    }
                    rows.push(values);
                }
                // printing
                for(var row in rows) {
                    html += '<tr>\r\n';
                    for(var item in rows[row]) {
                        html += '<td>' + item + ':' + rows[row][item].join(agregator) + '</td>\r\n';
                    }
                    html += '</tr>\r\n';
                }
                return html;
            }
            $.ajax({
                type: "GET",
                url: "test.csv",
                dataType: "text",
                success: function(response) {
                    $('#result').html(generateTable($.csv.parsers.splitLines(response)));
                }
            });

As I said on the previous question, to achieve what you want you must map those values first, then print those information. Please notice I changed a litle bit our code with new variables separator and agregator just to make it easy to change. Also that overflow solution just wraps the "lost cases" to the last header, if you know what I mean. As you got the last answer I will just talk about the changes.

Mapping

                for (var row in lines) {
                    if(row == 0) {
                        continue;
                    }
                    var cols = lines[row].split(separator),
                        values = {};
                    for (var col in cols) {
                        var item = header[col] ? header[col] : header[header.length-1];
                        if(values[item]) {
                            values[item].push(cols[col]);
                        } else {
                            values[item] = [cols[col]];
                        }
                    }
                    rows.push(values);
                }

Instead of populating the html variable, here I'll populate rows with simple objects. I decided to use a simple syntax: {headerName: [headerValues]}. So each header with the same name will be on the same array. Easy, right? One loop to rows, one loop to cols and we're done.

Printing

                for(var row in rows) {
                    html += '<tr>\r\n';
                    for(var item in rows[row]) {
                        html += '<td>' + item + ':' + rows[row][item].join(agregator) + '</td>\r\n';
                    }
                    html += '</tr>\r\n';
                }

Printing is more easy because the hardest part has gone. You just need to loop rows and cols again, just making a syntax conversion: {headerName:[headerValues]} to <td>headerName: [headerValues].join(agregator)</td>.

Kaique Garcia
  • 528
  • 3
  • 15
  • great as always! i have implemented it and im getting a single row for every row, but i would need to keep each cell in the row and not merge all cell, i would like to only merge the cell of those overflowed data. like this http://jsfiddle.net/xpvt214o/690880/ – rockStar Aug 28 '18 at 06:18
  • updated question, i have clarified it here https://stackoverflow.com/questions/52054827/add-overflow-table-cell-in-a-cell-with-a-header , or if possible from your answer bro to make there own table cell. – rockStar Aug 28 '18 at 09:46
  • solved: https://stackoverflow.com/a/52057909/4265572 hehe if I woudl change it here would be complicated to understand – Kaique Garcia Aug 28 '18 at 12:28