Is there any way to hide the delete button/div only for first row in knockout js. i have nested templates. and if use $index
it hides the remove button but also does the same for nested templates as they will have again the same index.
I had tried many thing but nothing works. I only want to hide the content of logical-div only if is the first row. and this template is repeating with same attributes/values. below is the HTML part.
Thanks in advance for any help.
<ul data-bind="template: { name: 'itemtemplate', foreach: $root.subitemsOf(null) }"></ul>
<script type="text/html" id="itemtemplate">
<li>
<div class="logical-div" style="margin-top: 5px; width:250px; border:1px solid gray;display:inline-block">
<select class='logical-inner-div' data-bind="options: $root.Condition, selectedOptions:logicalConditionSelected , optionsValue: 'logicalConditionVal', optionsText: 'logicalConditionName'" style="width:200px;">
</select>
</div>
<table class="block" style="margin-top: 5px;">
<tr>
<td>
<select data-bind="options: $root.tag, selectedOptions:tagSelected , optionsValue: 'tagVal', optionsText: 'tagName'" style="width:80px;">
</select>
</td>
<td>
<select data-bind="options: $root.isNot,selectedOptions:isNotSelected , optionsValue: 'isNotVal', optionsText: 'isNotName'" style="width:80px;">
</select>
</td>
<td>
<select data-bind="options: $root.alias,selectedOptions:aliasSelected, optionsValue: 'aliasVal', optionsText: 'aliasName'" style="width:300px;">
</select>
</td>
<td>
<a href="#" data-bind="click: $root.removeSegment"><img src="<?php echo Yii::app()->baseUrl . '/images/removeitem.gif';?>" alt="Delete"></a>
</td>
<td>
<button class="add-condition btn btn-primary btn-small" data-bind="click: $root.addNestedSegment" >Add Nested Block</button>
</td>
</tr>
<tr>
<td colspan="5">
<div style="margin-top: 5px;">
<ul data-bind="template: {name: 'itemtemplate', foreach: $root.subitemsOf($data)}"></ul>
</div>
</td>
</tr>
</table>
</li>
</script>
Some of JS Part.
function ItemModel(id, parent_id, initialLogicalVal,initialTagVal,initialIsNotVal,initialAliasVal) {
var self = this;
self.id = ko.observable(id);
self.parentId = ko.observable(parent_id);
self.logicalConditionSelected = ko.observable([initialLogicalVal]);
self.tagSelected = ko.observable([initialTagVal]);
self.isNotSelected = ko.observable([initialIsNotVal]);
self.aliasSelected = ko.observable([initialAliasVal]);
}
var viewModel = function RecursiveListViewModel(tasks)
{
var self = this;
// Knock out function for checking the parents
self.subitemsOf = function (item) {
var children = ko.utils.arrayFilter(self.items(), function (arrayItem) {
var parentItemId = (null === item) ? null : item.id();
return arrayItem.parentId() == parentItemId;
});
return children;
};
// Knock out function for checking if there are child for this record for populating nested records
self.hasSubitems = function (item) {
var firstMatch = ko.utils.arrayFirst(self.items(), function (arrayItem) {
return (arrayItem.parentId() == item.id());
});
return (null !== firstMatch); // At least one item found in array
};
// Knock out function for Adding the nested/child segment
self.addNestedSegment = function(item) {
self.items.push(new ItemModel(++ChildId, item.id(),'and','Tag','Is Not',firstAlias));
}
// Knock out function for Adding the Main/Parent segment
self.addMainSegment = function(data, event) {
self.items.push(new ItemModel(++ChildId, null,'and','Tag','Is Not',firstAlias));
}
// Knock out function for Deleting the segment with all of its children
self.removeSegment = function(data,item)
{
self.items.remove(data);
}
}
Instead of using the $index variable, you can check the object reference:
<!-- ko if: $data != $root.items()[0] -->
<div class="logical-div">
</div>
<!-- /ko -->
Or using some observable, like
<!-- ko if: $data != firstItem() -->
<div class="logical-div">
</div>
<!-- /ko -->
self.firstItem = ko.computed(function() { return self.items()[0]; });
The != operator in the sentence check if both objects are the same (if they point to the same object in memory). Because the first rendered element is the same as self.items()[0] then knockout will hide this div element (in this case, the ko if does not hide the element; it does not render it)
Greetings!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.