0

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();
                }
            },
0

2 Answers 2

1
  1. do NOT use document.write after load of the page. It will wipe the page
  2. your desc needs to be in single quotes and have no carriage returns.
  3. you cannot use an IIFE in an onclick unless it returns a function
  4. if your button is in a form, you will submit the form - it should be type=button

You MAY mean

<button type="button" onclick="var desc='<?php echo $desc; ?>'; 
document.querySelector('#someContainer').innerHTML=desc;"...>

but a better way is to toggle the existing text inside tags (span for example)

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

4 Comments

I can put the $desc in an html container and reference it in the querySelector, is that what you mean? I am not exactly sure what the container should be.
It could be the parentcontainer of the button - you can replace the button with the text. It was just an example
thank you using span tags seems like a better option
-1

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;
            }`

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.