24sharon 24sharon - 1 month ago 5
AngularJS Question

Create Dynamic columns from big text data Javascript

I get the text dynamically, sometimes the text is short sometime the text long.

The Designer separate the text to tow columns.

enter image description here

How can I seperate the text to tow columns.

I use angular but simple javascript is ok.

Answer

Considering that you are using bootstrap column layout, you can divide the string content into two equal parts (space as a breaking point) and then you can construct a html string like this:

function splitInHalf(s){

    var middle = Math.floor(s.length / 2);
    var before = s.lastIndexOf(' ', middle);
    var after = s.indexOf(' ', middle + 1);

    if (middle - before < after - middle) {
        middle = before;
    } else {
        middle = after;
    }

    var s1 = s.substr(0, middle);
    var s2 = s.substr(middle + 1);
    return [s1, s2];

}

var x = <you big or small string of content>;
var splitX = splitInHalf[x];
var _html = '<div class="col-lg-6">'+splitX[0]+'</div><div class="col-lg-6">'+splitX[1]+'</div>'

// Now set this _html as your html
Comments