Paragraph Text button

A couple of our apps have a paragraph text field we use for notes. Assuming the Knack’s inline edit is set to yes, as you know when a user clicks the notes column, a small “edit notes” window is opened where the user can enter notes. Upon submission, the notes are now seen in the table. Assuming the user was long winded, it can look overwhelming. What I want to accomplish is, to have a button labeled “notes” that when pressed, opens the same small “edit notes” window, user inputs notes, submits, and instead of showing notes in column, the “notes” button would change from original color (ie green) to yellow. The yellow button would indicate there are notes. Next user logs in, sees the “yellow” button, hits it and any and all notes are displayed and editable if needed using the Knack same “edit notes” window.

Basically when “notes” button is green = no notes and when the “notes” button is yellow = notes are present but hidden and viewable only by hitting the “yellow” button.

Don’t know if this is possible or if someone has solution.

Thank you… JON

Hi

Will the notes field be a field that keeps getting added to by different people?
Do you need to be able to tell if the notes have been added to by a different user?
Or
Will the notes always be empty until a user adds notes?

Craig

Hey Craig, thanks for the reply…

What I would like is to have the notes field work exactly as it does now with the exception I’d like to hide the notes field in the table (using KTL if I have to) and only be able to access it using a button (ie “notes”) that if pressed, will open up Knack’s edit note balloon. Once open user will have the option to add/edit current notes (if any) or add their own notes. Button will be placed to the right of table, green in color if no notes existing for that record and yellow if notes exist. Having the ability to tell whom added or edited notes is not important to me.

Thank again… JON

Hi Jon

Are the notes in the grid for each record?

Craig

Hey Craig, answer to question is “YES”… JON

Ok I’ll have a play tomorrow and see what I can come up with. I think this is possible though Jon

C

I appreciate it… JON

Hi Jon

If you require a very basic version you can use Knack grid display rules as follows:

You can also display an icon with the display rules if its empty like a plus:
Screen Shot 2023-10-08 at 19.56.54

You can also add some css to make the whole cell clickable which is something we add to all table cells excluding button links.:

td a {
  width: 100%;
}
td a::after {
   display: block;
   content: " "; 
   position: absolute;
   top: -4px;
   left: -9px;
   width: 500px;
   height: 300px; /* max possible */
   z-index: -1;
}
td{
   position: relative; 
   z-index: 0;
   overflow: hidden;
}/*END  Makes Table Cell clickable*/

If you want it to be a button that is going to take a bit more work. If you need it to be an actual button let me know and I’ll work out the css and the KTL keywords for it.

Craig

Hey Craig, thanks for taking the time on this project and for your reply.

Yeah, I’d like to hide the “notes” column and just have button on the right hand side of the grid/table that has a background color of green if no notes exist, and yellow background when notes are present for that record.

Your example for display rules #2 where it displays a green pencil is on the right track, other that I’d like to have a button in the column instead of an icon. I’ve tried several different rules including the ones you sent and I couldn’t get anything to work that I liked.

Thanks again Craig… JON

Hi Jon

Can you please provide me with the colours you would like the button to alternate between. Also a screenshot of another button you use that you would like this button to look like would be helpful.

I’m going to try and do this with just CSS & KTL if possible.

Craig

Hey Craig, I’d probably start with green (no notes), yellow (with notes) but if your using CSS I can change the look of the button as needed. Here is the CSS code for another button I’m using:

.redButton {
background: rgb(237,28,28);
border: 0px solid #e3545e;
border-radius: 3px;
padding: 3px 8px;
color: #FFFFFF;
display: inline-block;
text-align: center;
text-shadow: none;
cursor: pointer;
}

I really appreciate you trying to tackle this. I messed around with it today and was trying to figure out how to make a button open up Knack’s “edit note” ballon that currently opens when a user clicks the table column.

Can’t figure out what the class of this particular balloon/dialog box is. Hoping you have better luck and I wouldn’t be surprised if using KTL works. Awesome product Norm has put together.

Thanks again… JON

Hey Craig, here’s the code for adding a button to open the notes field. I hide the notes column using the KTL keyword _hc. The column is hidden and button will change to yellow if notes are present, green if none are present. The only thing I don’t like and I’m trying to find a solution for, is that when you hide the column using KTL, the edit note dialog appears in the upper right hand corner. If I don’t hide column and user presses notes button, the dialog appears in the column, but notes also are shown in column and that defeats the purpose of the button. If you find a solution with or without KTL, please let me know… JON

CODE TO ADD BUTTONS:

$(document).on(‘knack-view-render.view_157’, function (event, view) {
// Track the state of the inline editor
var isEditorOpen = false;

// Function to add a button to the table rows in view_157
function addButtonToView157() {
// Find the table element in view_157 by its ID
var tableView = document.getElementById(“view_157”);

// Check if the table exists
if (tableView) {
  // Get all the rows in the table body (excluding the header row)
  var rows = tableView.querySelectorAll("tbody tr");

  // Iterate through the rows
  for (var i = 0; i < rows.length; i++) {
    // Create a new cell for the button in each row
    var cell = document.createElement("td");

    // Create the button element
    var button = document.createElement("button");
    button.innerHTML = "Notes"; // Set the button label to "Notes"

    // Find the specific row
    var row = rows[i];

    // Find the cell containing field_141 within the row
    var cellWithField141 = row.querySelector("[data-field-key='field_141']");

    // Check if field_141 contains data
    if (cellWithField141.textContent.trim() !== "") {
      // Set a CSS class to change the button's color to yellow
      button.classList.add("notes-yellow-button");
    } else {
      // Set a CSS class to change the button's color to green
      button.classList.add("notes-green-button");
    }

    // Add a click event listener to the button
    button.addEventListener("click", function() {
      // Find the specific row where the button was clicked
      var row = this.closest("tr");

      // Find the cell containing field_141 within the row
      var cellWithField141 = row.querySelector("[data-field-key='field_141']");
      
      // Toggle the state of the inline editor
      if (isEditorOpen) {
        // If open, close it
        cellWithField141.querySelector(".kn-inplace-editor-close").click();
      } else {
        // If closed, open it
        cellWithField141.click();
      }

      // Update the state
      isEditorOpen = !isEditorOpen;
    });

    // Append the button to the cell
    cell.appendChild(button);

    // Append the cell to the current row
    rows[i].appendChild(cell);

    // Adjust the width of the first column (index 0) to 100 pixels
    if (i === 0) {
      cell.style.width = "25px";
    }
  }
}

}

// Call the function to add the button to view_157
addButtonToView157();
});

CODE FOR CSS BUTTONS:

.notes-yellow-button {
background-color: yellow;
border: 0px solid #298af2;
border-radius: 3px;
padding: 3px 6px;
color: #000000;
display: inline-block;
text-align: center;
text-shadow: none;
font-size: 12px;
cursor: pointer;
transition: 0.4s;
}

.notes-green-button {
background-color: green;
border: 0px solid #298af2;
border-radius: 3px;
padding: 3px 6px;
color: #ffffff;
display: inline-block;
text-align: center;
text-shadow: none;
font-size: 12px;
cursor: pointer;
transition: 0.4s;
}

OPTIONAL JS CODE THAT TOGGLES DIALOG BOX

$(document).on(‘knack-view-render.view_157’, function (event, view) {
setTimeout(function() {
// Attach a click event handler to the table
$(‘#view_157’).on(‘click’, ‘button’, function() {
// Find the specific row where the button was clicked
var row = $(this).closest(‘tr’);

  // Find the cell containing field_141 within the row
  var cellWithField141 = row.find("[data-field-key='field_141']");
  
  // Toggle the state of the inline editor
  if (isEditorOpen) {
    // If open, close it
    cellWithField141.find(".kn-inplace-editor-close").click();
  } else {
    // If closed, open it
    cellWithField141.click();
  }

  // Update the state
  isEditorOpen = !isEditorOpen;
});

}, 1500); // Adjust the delay as needed
});

Hi Jon

I’m heading down the route of making the column invisible and the span within the column visible. I’ve got the button to work I’m now working on hiding the text(Nearly There).
image

Doing it this way means that you can continue to use Knacks cell-edit without using javascript.

As soon as I’ve got it working I’ll post the css here.

Craig

Sounds good…

My solution / above script works as expected with the exception of the “edit notes” dialog is opening in the upper left hand corner instead of desired position of the middle of the screen when I hide the column using KTL. Not a KTL issue, just when hiding column the dialog doesn’t know where to be placed. Not a big deal, but trying to find a solution.

Button is green when NO notes are present then changes to yellow when notes are present. Notes can only be viewed or edited when user presses the green and/or yellow button. As you know, Knack’s default is to click the row/column of the field you want to edit, in this case, “edit notes” dialog is shown. The problem is, long winded users make the table look horrible with Knack’s default. With this solution, notes are hidden and user is made aware there is notes present by button being yellow.

You should try my solution and I’m looking forward to seeing your solution.

Thanks again… JON

Hi Jon

Code looks good I’ll give it a go on my test area.

Thank you

Craig

Sounds good…

If you have a solution using my above script to make the “edit notes” dialog be placed in the middle of screen, please let me know. Again, it only is displayed if you hide the column. If you show the column, and user press notes button, dialog is displayed in the row/column of button, but this defeats the purpose of the notes button. Hiding the column and adding a button to add, view or edit notes is what I wanted and script above does this.

Hi Jon

I’ve tried your code and I couldn’t get it to work not sure why. I have come up with a simpler solution (I think). It only uses 2 lines of JavaScript, 5 KTL keywords and a few lines of css. You will need to change the view id and the field ids. You will also need a yes/no field (Has Notes) in the object with the Notes with 2 conditional rules like this:

The JavaScript

$(document).on("knack-view-render.view_142", function (event, view) {
    $('td.field_409 span').hide(); // Hide the notes text 
    $('td.field_409').append('<button class="notes-button">Notes</button>'); // Add a button to the cell
});

The CSS - please edit to your liking.

.notes-button {
  visibility: visible;
  background: inherit;
  border-radius: 3px;
  padding: 4px 8px;
  color: #000;
  display: inline-block;
  text-align: center;
  text-shadow: none;
  cursor: pointer;
  box-shadow: 7px 7px 5px -7px #caf4bc7a;;
}

The Keywords

_hc=Has Notes
_cls=[ktlDisplayNone], [ktlTarget, $('#view_142 th.field_409')]
_style=[visibility: visible], [ktlTarget, $('#view_142 td.field_409 span')]
_style=[visibility: hidden], [ktlTarget, $('#view_142 td.field_409')]
_cls=[notes-button], [ktlTarget, $('#view_142 td.field_409 span')]
_cfv=[field_408, is, True, , yellow, ,], [ktlTarget, field_409]
_cfv=[field_408, is, False, , green, ,], [ktlTarget, field_409]

With all this in place it will look like this:
image

Any problems let me know.

Craig

Hi Jon

I will look at moving the cell-editor to the middle of the screen. This could be done with just CSS.

Craig

Hey Craig, not sure why my code is not working for you. Works perfect for me!!!

I even shared it with a friend of mine and he say’s it works great.

It appears some of the lines of code I posted are above and below where the bulk of the code is.

If there’s a better way of posting my script, please let me know.

Just a fyi, I just replaced my script by copying and pasting the “entire” code and it works fine…

I 'll give your code a try and encourage you to try copying and pasting my entire code and try mine.

I do like how short and sweet your code is compared to mine!!!

Thank you sir… JON

Hi Jon

If you are happy with your solution, then by all means use it. I think moving the cell-editor is not as easy as I thought as every time you open the cell-editor Knack must use JavaScript to rewrite the CSS inline styles. Not saying it’s impossible but it may involve more work than is worth doing.

Craig