How to highlight the selected row in v-data-table? i tried to modified the data by adding flag variable selected in the Example
In the above example click on a row will be highlighted by adding class called primary. If it is a static data it is working fine, but if it is dynamic data like getting data from API, the data in the data table will always be refreshed, if i change the pagination and sort and all.
For example, in my case, if i sort the column the data will come from the API and data in the v-data-table will be refreshed with sorted data, in this case it is very hard to maintain that selected flag each and every time when ever the data changes. Is there any other way to highlight a selected row?
<v-data-table #click:row="rowClick" item-key="name" single-select ...
methods: {
rowClick: function (item, row) {
row.select(true);
//item.name - selected id
}
}
<style>
tr.v-data-table__selected {
background: #7d92f5 !important;
}
</style>
or
<style scoped>
/deep/ tr.v-data-table__selected {
background: #7d92f5 !important;
}
</style>
Example
https://codepen.io/nicolai-nikolai/pen/GRgLpNY
Your solution does not work because the selected property is added to the data when you click on a row, but when data is reloaded then the data without a selected property replaces the old data.
So to make it work:
- add an id field to each item in the list of desserts
- add an selectedId with default value -1 to your data
- change the line of code in method activerow to this.selectedId = item.id;
- change the class attribute in the <tr> to :class="{'primary': props.item.id===selectedId}"
If on reload only your list of desserts changes, and the new list contains an item with the same id as selected before, then the same row should become selected.
I forked the codepen example to show you how this works:
https://codepen.io/anon/pen/PrWjxQ?editors=1010
How to make the row clickable is allready explained by others but not how to toggle the row selection. The selection state can be checked through isSelected and then set accordingly.
To style the selected row properly you need something more to take into account: the dark and light theme and the hover css pseudo class - which is overridden through the important tag so we need to restyle it.
Check out the Codepen Example
methods: {
rowClick: function (item, row) {
let selectState = (row.isSelected) ? false : true;
row.select(selectState);
}
}
<style>
.theme--light.v-data-table tbody tr.v-data-table__selected {
background: #f5c17d70 !important;
}
.theme--dark.v-data-table tbody tr.v-data-table__selected {
background: #a17b4970 !important;
}
.theme--dark.v-data-table tbody tr.v-data-table__selected:hover {
background: #a17b49c2 !important;
}
.theme--light.v-data-table tbody tr.v-data-table__selected:hover {
background: #ffd296d2 !important;
}
</style>
Related
This is before selecting an absent button
This is after selecting an absent button
This is my code in vuejs:
<b-button pill variant="outline-secondary" :class="{ absent: showAbsent }" #click="markAttendance" >A</b-button>
methods: {
markAttendance() {
this.showAbsent = true;
}
}
.absent { background-color: red; }
It seems like you're just duplicating the b-button instead of creating unique approach for multiple buttons.
You may need to enhance your data to create an array of attendees like attendees: [{absence: false, name: "John Doe"}...], and then loop/iterate the attendees like <b-button v-for="(atende, a) in atendees" :key="a" :class="{absent: atendee.absence}" #click="atendees[a].absence=!atendees[a].absence">.
Vue has a concise documentation, you may read more about loops here.
Working with Vue.js, I do use a simple way to set dynamically the height of a text area that resizes when typing. But I am not able to do it when the component mounts or the value updates.
I have already try http://www.jacklmoore.com/autosize/, but it has the same problem.
I have created a sandbox that shows the problem, when typing the box it updates, but not when the value changes dynamically
Live example: https://codesandbox.io/s/53nmll917l
You need a triggerInput() method:
triggerInput() {
this.$nextTick(() => {
this.$refs.resize.$el.dispatchEvent(new Event("input"));
});
}
to use whenever you're changing the value programatically, triggering the resize logic used on <textarea> on "real" input events.
Updated codesandbox.
Note: Without the $nextTick() wrapper, the recently changed value will not have been applied yet and, even though the input is triggered, the element has not yet been updated and the resize happens before value has changed, resulting in the old height and looking like it didn't happen.
Not really feeling the answers posted here. Here is my simple solution:
<textarea
rows="1"
ref="messageInput"
v-model="message"
/>
watch: {
message: function(newItem, oldItem) {
let { messageInput } = this.$refs;
const lineHeightInPixels = 16;
// Reset messageInput Height
messageInput.setAttribute(
`style`,
`height:${lineHeightInPixels}px;overflow-y:hidden;`
);
// Calculate number of lines (soft and hard)
const height = messageInput.style.height;
const scrollHeight = messageInput.scrollHeight;
messageInput.style.height = height;
const count = Math.floor(scrollHeight / lineHeightInPixels);
this.$nextTick(() => {
messageInput.setAttribute(
`style`,
`height:${count*lineHeightInPixels}px;overflow-y:hidden;`
);
});
},
}
<style scoped>
textarea {
height: auto;
line-height: 16px;
}
</style>
I am creating a simple inventory system that will have various categories for the items, as well as the option to display all items.
Going from the 'all' category to the 'general' category will remove the unnecessary item, but leaves a gap for a significant period of time and there is not animation of the item after the gap sliding into place.
I am doing this using Vuejs and vue2-animate.
computed:
{
active_items: function()
{
var _self = this;
if(_self.active_category === 'all')
{
return _self.items;
} else
{
return _self.items.filter(function(i)
{
return i.category === _self.active_category;
});
}
}
},
https://jsfiddle.net/Crotanite/cn07tmho/8/
The gap that is left in place of disappearing list items is because an element that transition is being applied to, stays in place until leave-active animation phase is finished.
Simple fix, is to add position: absolute; to a leaving element. This will allow sibling list items to take it's position.
Below is updated version of your example with additional class zoomOut__absolute added to leave-active-class attribute. Additional class is added to avoid overwriting styles of animate.css:
JSFiddle
Is it possible to "lock" the top row of the results from dataTables? so that when i scroll through 100s of row results, the top heading doesnt move. I have tried setting a size to tbody and making it overflow in css but that obviously doesn't work.
tbody{
overflow-y:scroll;
height:600px;
}
Yes, it is possible. Sounds like you want to use the DataTables "FixedHeader" extension:
$(document).ready( function () {
var table = $('#example').DataTable();
new $.fn.dataTable.FixedHeader( table );
} );
Source: https://www.datatables.net/extensions/fixedheader/
i have the view as the image attached. I want to add date picker for the column expiry date in all the rows.
here the first row contains the id for the column expiry date is 'expiry_1_date'
2nd row -> 'expiry_2_date'
3nd row -> 'expiry_3_date'
4nd row -> 'expiry_4_date'
5nd row -> 'expiry_5_date'
Please help me to acheive the desired result in Jquery. thanks in advance!!!
Use starts with selector. and call the datepicker method.
try like this
$('input:text [id^="expiry_"]').datepicker();
or alternate way add a class to each text box.
then use like this
$('.SomeClass').datepicker();
Demo
use attribute selector [] with ^
Selects elements that have the specified attribute with a value beginning exactly with a given string.
try this
$('input[id^="expiry_"]').datepicker();
Set a input with a class datepicker
<input type="text" class="datepicker" />
and then load it like :
$(function() {
$( ".datepicker" ).datepicker();
});
You can get the date from it like:
$(".datepicker").datepicker(
{
onSelect: function()
{
var dateObject = $(this).datepicker('getDate');
alert(dateObject);
}
});
You can select all element with id that start with string expiry and assign datepicker to it:
$('input[id^="expiry"]').each(function() {
$(this).datepicker();
})
Demo: http://jsfiddle.net/pyMzT/
Try like
$("input[id^='expiry'][id$='date']").datepicker();
See DEMO HERE
and to add css to it try with this
<style type="text/css">
.ui-datepicker {
background: #333;
border: 1px solid #555;
color: #EEE;
}
</style>