Search code examples
javascriptphphtmlphalconvolt

Display more of a hidden text on button click, php, phalcon


I have a table that displays items from a database. One of the items is a description so it can be very long.The thing I'm having the most problem with is how can I use JS and HTML smoothly in my controller class.

I want to be able to display a little bit of it if its longer than 100 char, and a button that looks like '...' where if the user clicks on it, it displays the trimmed text. I want to do this using javascript and here is what I tried, this code is in my controller, so I'm just sending these to the view.

The problem is when I press the button it doesn't display anything so what is wrong here? Some suggested to use jquery but I don't want to write my js script elsewhere and call it again since I'm not sure how I will do that in Phalcon controller.

    $this->view->tblColumns = [
            'element one',
            'element two',
            function (tablename $instance) {
                if (strlen($desc = $instance->getDescription()) > 100) {
                    return $shortDesc = substr($instance->getDescription(), 0, 100) . '
<button style="background: none;border: none" onclick="(function(){
    var desc= <?php echo
    $desc; ?>; document.write(desc) ;
})()" >...</button>';

                } else {
                    return $instance->getDescription();
                }
            },

Solution

  • I find a way to do what I wanted, using the code for read more,read less from this link https://codepen.io/maxds/pen/jgeoA The thing I was having trouble with in phalcon MVC, was that I didn't know I could my java-script, and css in the view of the controller and that's what I did. I just used the js from that link, into my view file, the same for the css using tag and tag. And in the function on the controller I wrote the following `

    $this->view->tblColumns = [
                'one',
                'two',
                function(tablename $link){
                $desc=$link->getDescription();
                    $html=<<<HTML
                 <span span class="more"> $desc</span>
                      HTML;
                    return $html;
                }`