0

I'm trying to dynamically render values I am pulling from SQL that looks like this:

enter image description here

into something that looks like this:

enter image description here

I already have HTML and CSS placed and I am approaching this using $.each but I cant seem to populate the inside of the view tree:

enter image description here

EDIT:

here is my script:

AjaxGet('GetUserAppFeatures', { "uid": userid }, false, true)
        .success(function (result) {

            $.each(result.model, function (val) {

                var li = $('<li></li>');

                li.append('<span class="caret">' + result.model[val].AppName + '</span> <input type="checkbox" id="chkApp' + result.model[val].AppId + '">');

                var ul = $('<ul class="nested"></ul>');
                $.each(result.model[val], function (index) {

                    ul.append('<li>' + result.model[val].FeatureName[index] + '</li> <input type="checkbox" id="chkApp' + result.model[val].FeatureId[index] + '">');

                    li.append(ul);
                });


                treeview.append(li);

            });

        });
2
  • Please post some of your code. Commented May 27, 2020 at 4:44
  • This is easy. Please create a small demo for this using jsfiddle or snippet here so that we can update it and provide a better solution. You can hard code the array data to be same as data you are getting from database for the demo purpose. Commented May 27, 2020 at 4:46

2 Answers 2

1

For this type of data you need to group by.

var yourData = [
{Name : "Forms", value : "Request"},
{Name : "Forms", value : "Report"},
{Name : "Forms", value : "Password"},
{Name : "Energy", value : "Report"},
{Name : "Energy", value : "CUstomer Multiplier"},
{Name : "Energy", value : "Product Feedback"},
];
Renderdata();
function Renderdata(){
  var data = groupBy(yourData,"Name");
  var html = '';
  for(var i= 0 ; i < data.length; i++){
    html += "<div>"+data[i][0].Name+" </div>"+"<ul>";
   for(var j= 0 ; j < data[i].length; j++){
    html += "<li>"+data[i][j].value+"</li>";
  } 
  html += "</ul>";
  }
  $('#div').html(html);
}

function groupBy(collection, property) {
    var i = 0, val, index,
        values = [], result = [];
    for (; i < collection.length; i++) {
        val = collection[i][property];
        index = values.indexOf(val);
        if (index > -1)
            result[index].push(collection[i]);
        else {
            values.push(val);
            result.push([collection[i]]);
        }
    }
    return result;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="div"></div>

Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps you need an intermediate step where you construct an array modelling the second image you have there. Something like:

// Do this before $.each()
var sql_data = {
    Forms: [],
    Energy: []
};


// Inside of $.each() do something like this:
sql_data[col_1].push(col_2);

Where "col_1" and "col_2" are respectively the first and second columns in your data. Then you can use that "sql_data" object to create the HTML that you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.