Changing returned ajax data structure

Changing returned ajax data structure

classic12classic12 Posts: 228Questions: 60Answers: 4
edited November 2017 in Free community support

I have produced some data from the user_ files, files and my own quote table using this MYSQL statement.

"SELECT 
    a.quoteID, c.filename
    FROM quotes a 
    INNER JOIN users_files b on b.user_id = a.quoteID 
    INNER JOIN files c on c.id = b.file_id"

I get the data back as:

[{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/23.pdf"},{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/22.jpeg"},{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/21.jpeg"},{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/20.jpeg"},{"quoteID":"2","quoteTitle":"Kaitlin","notes":"Smith","web_path":"\/surplusAdmin3\/upload\/24.jpg"},{"quoteID":"8","quoteTitle":"Bryar2","notes":"Long","web_path":"\/surplusAdmin3\/upload\/17.png"},{"quoteID":"11","quoteTitle":"Avram","notes":"Allison","web_path":"\/surplusAdmin3\/upload\/4.jpg"}]

I need the data as follows

[{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/23.pdf","web_path1":"\/surplusAdmin3\/upload\/22.jpeg,"web_path2":"\/surplusAdmin3\/upload\/21.jpeg","web_path3":"\/surplusAdmin3\/upload\/20.jpeg"},{"quoteID":"2","quoteTitle":"Kaitlin","notes":"Smith","web_path":"\/surplusAdmin3\/upload\/24.jpg"}]

So I have used the following to try this

// loop through and add field 'Type' can all be done in one loop.
        dataReturned.forEach(function(e){
        if (typeof e === "object" ){
        e["Type"] = "other";
        
       // console.log('stuff '+e['Type'][i] )
  }
  
});

//loop through and workout the file type
        dataReturned.forEach(function(e){
        if (typeof e === "object" ){
        var ext = e['web_path'].substr(e['web_path'].lastIndexOf('.') + 1);
        if ( (ext === 'jpeg' ) || ( ext === 'jpg' )|| ( ext === 'png' )|| ( ext === 'gif' ) || ( ext === 'pdf' ))
        
        { e["Type"] = "image"; }
        //console.log(e['web_path']);
       // console.log('stuff '+ JSON.stringify(e))
  }
  
});
// create a new array formated as needed
var currentQuoteID =0;
var dataChanged = [];
var arrayRow = -1 ;
var fileCount = 0;
dataReturned.forEach(function(e){
// now loop through and get the  quoteID and add a new row to a new array ( copying the row data)
// if the next row = same quoteID push the new web_path to the existing & repeat.
// if not the same quoteID add a new row. We will end up with one row for each quoteID and x umber of files in each row.
        console.log(' current quote '+ currentQuoteID + ' = '+ e["quoteID"] + 'file count = '+  fileCount);
        if ( currentQuoteID !== e["quoteID"]) {dataChanged.push(e); arrayRow = arrayRow + 1; fileCount = 1}
      else
        {
            console.log('just add the filename array Row = ' +arrayRow );
            // need to insert to the array arrayRow
            // dataChanged.push('web_path'+fileCount,e["web_path"]);
            toPush = 'web_path'+fileCount+'"'+':'+'"'+ e["web_path"];
            dataChanged.push(toPush);
            fileCount = fileCount +1;
        }
        currentQuoteID = e["quoteID"];
       console.log('stuff '+ JSON.stringify(dataChanged));
});

I am close as my result is

 [{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"/surplusAdmin3/upload/23.pdf","Type":"image"},"web_path1\":\"/surplusAdmin3/upload/22.jpeg","web_path2\":\"/surplusAdmin3/upload/21.jpeg"]

So I need to insert at the end of the current array instead of the push.

What is the correct syntax please

dataChanged.push(toPush);

to

dataChanged[arrayRow].toPush
var currentQuoteID =0;
var dataChanged = [];
var arrayRow = -1 ;
var fileCount = 0;
dataReturned.forEach(function(e){
// now loop through and get the  quoteID and add a new row to a new array ( copying the row data)
// if the next row = same quoteID push the new web_path to the existing & repeat.
// if not the same quoteID add a new row. We will end up with one row for each quoteID and x umber of files in each row.
        console.log(' current quote '+ currentQuoteID + ' = '+ e["quoteID"] + 'file count = '+  fileCount);
        if ( currentQuoteID !== e["quoteID"]) {dataChanged.push(e); arrayRow = arrayRow + 1; fileCount = 1}
      else
        {
            console.log('just add the filename array Row = ' +arrayRow );
            // need to insert to the array arrayRow
            // dataChanged.push('web_path'+fileCount,e["web_path"]);
            toPush = 'web_path'+fileCount+'"'+':'+'"'+ e["web_path"];
            dataChanged.push(toPush);
            fileCount = fileCount +1;
        }
        currentQuoteID = e["quoteID"];
       console.log('stuff '+ JSON.stringify(dataChanged));
});

Cheers

Steve Warby

Answers

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin

    You want to add extra data to each object in the array? The MDN documentation for forEach says that the first parameter passed in to the callback is the item in the array - so you could use:

    e.web_path2 = ...
    e.web_path3 = ...
    

    to modify those objects.

    Allan

  • classic12classic12 Posts: 228Questions: 60Answers: 4

    How do I edit the post. It was a late night last night and its not formatted correctly.

  • classic12classic12 Posts: 228Questions: 60Answers: 4

    e is the dataReturned array I need to update the dataChanged array.

    So I now have

    dataChanged = [{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"/surplusAdmin3/upload/23.pdf","Type":"image"},"web_path1","/surplusAdmin3/upload/22.jpeg"]
    
    

    I now need to get my result as:

    dataChanged = [{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"/surplusAdmin3/upload/23.pdf","Type":"image","web_path1","/surplusAdmin3/upload/22.jpeg"}]
    

    I need to insert at the end not add.

    I've tried various approaches.

    I thought I could do

    ```
    dataChanged[arrayRow].'web_path'+fileCount = 'web_path'+fileCount

    Heres hoping....

    Cheers

    Steve Warby

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin

    I've edited it now. You only get 5 minutes to edit a post I'm afraid.

    dataChanged[arrayRow]['web_path'+fileCount] = 'web_path'+fileCount
    

    would be the Javascript syntax to update a variable web_path{i} inside the arrayRow item.

    Allan

  • classic12classic12 Posts: 228Questions: 60Answers: 4

    Great guys,

    thanks for all the help.

    I actually use "

                var field = 'web_path'+fileCount;
                var data = webPath+e["web_path"];
                dataChanged[arrayRow]['web_path'+fileCount] = data
    

    webPath allows me to change the server.

    I now populate the rows with web_path, web_path1 etc but some have no value. Is there a option to hide if undefined ?

  • classic12classic12 Posts: 228Questions: 60Answers: 4

    Also on iphone if I rotate the device there is a large wasted area on both sides.

    I have tried

    window.addEventListener("orientationchange", function() {
        //$.jGrowl("the orientation of the device is now " + screen.orientation.angle);
        //$.jGrowl("the orientation of the device is now ");
        tableDataChanged.draw();
        tableDataChanged.columns.adjust();
       
    });
    

    http://toolfolks.com/surplusAnywhere4/

    I am using responsive plugin.

    Cheers

    Steve Warby

  • kthorngrenkthorngren Posts: 20,145Questions: 26Answers: 4,736
    edited November 2017

    there is a large wasted area on both sides

    Try adding width="100%" to your table in HTML. For example:
    <table id="example" class="display" width="100%"></table>

    Kevin

This discussion has been closed.