简体   繁体   中英

auto resize div after ajax content call completes

I have an api call that grabs data just fine. Its setup to use a button to open up a collapsible div. If you have it open already, you'll see the first line or 2 come up once the data is fetched, and you have to close/reopen the div to see all the data. been stuck on this for a bit, tried a bunch of stuff already. heres the JS

$(document).ready(function() {

function myFunction(val) {}
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
    coll[i].addEventListener("click", function() {
        this.classList.toggle("active");
        var hidden_content = this.nextElementSibling;
        if (hidden_content.style.maxHeight) {
            hidden_content.style.maxHeight = null;
        } else {
            hidden_content.style.maxHeight = hidden_content.scrollHeight + "px";
        }
    });
}
document.getElementById('collapsible').click();
$.ajax({
    type:"GET",
    url:"pages/splunk_bf_data",
    dataType:"json",
    success:function(data){
        var html = '<p>' + data + '</p>'
        $('.data-container').html(html);
    }
})})

cheezed it with clicking it twice through an after completion function. if there is a better/more legit way of doing this, please post it..I prefer best practices.

but heres what i did

$(document).ready(function() {

function myFunction(val) {}
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
    coll[i].addEventListener("click", function() {
        this.classList.toggle("active");
        var hidden_content = this.nextElementSibling;
        if (hidden_content.style.maxHeight) {
            hidden_content.style.maxHeight = null;
        } else {
            hidden_content.style.maxHeight = hidden_content.scrollHeight + "px";
        }
    });
}

function changeSizeByClicking() {
    document.getElementById('collapsible2').click();
    document.getElementById('collapsible2').click();
}

document.getElementById('collapsible').click();
$.ajax({
    type:"GET",
    url:"pages/splunk_bf_data",
    dataType:"json",
    beforeSend:function(){
        var html = '<p>' + 'Fetching data from Splunk..' + '<br>' + 'please wait...'+ '</p>'
        $('.data-container').html(html);
       },
    success:function(data){
        var html = '<p>' + data + '</p>'
        $('.data-container').html(html);
    },
    complete:function(data){
        changeSizeByClicking();
    }
})})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM