[Not exactly the same as the question "how to disable knockout click...". My question involves specific usage of an HTML table and contains valuable approaches on solving such case.]
I have the following table and button below it:
<table>
<tbody data-bind="foreach: my-array">
<tr data-bind="click: $ShowDetails()">
...
<button>Add New Record</button>
The table rows are clickable (and would load some details data in another table).
On click of the button I need to disable all table rows and add one new <tr>
on top.
I know how to add the new record on top:
$('<tr><td contenteditable="true">New Record Here</td></tr>').prependTo('table > tbody');
But how to disable all rows of the table so they won't be clickable and look disabled (grayed out)?
Just add disabled
class to your <tr>
's using $("tr").addClass("disabled")
.
The grayed out backgroung can be added by using $('tr').css('background-color','grey')
or by describing .disabled
class in your css-file:
tr.disabled {
background-color: grey;
}
Then in your ShowDetails()
method just check if calling element has the .disabled
class by using $(this).hasClass("disabled")
method. Show details if it doesn't and do nothing if it does.
Instead of checking the disabled class you can also add a new bool observable named AddMode()
and set it to true on Add New button click, and on ShowDetails()
put a first line if(AddMode() === true) return;
(by @st_stefanov)
Something like this, yes. I will give it a try. Thanks.
instead of checking the disabled class I can also add a new bool observable named AddMode() and set it to true on Add New button click, and on ShowDetails() I will put a first line if(AddMode() === true) return; What do you think?
Awesome. Thank you for your help FleGMan. I have chosen this as an answer, because it gave a full solution on all points I had in my question details. Welcome to the forum man and this is a good start for you here! Keep going this way! (Stan)