Issue with Sorting Dataable Date - datatables

I've been trying to sort a datatable date column, and I've not been able to successfully get it to work. I've researched and looked at several examples with no luck. The date always sort incorrectly.
The script:
table = $('#FindUserTable')
.on('search.dt', function () {
})
.on('page.dt', function () {
var info = table.page.info();
setTablePaging(info);
})
.on('length.dt', function (e, setting, len) {
var info = table.page.info();
setTablePaging(info);
})
.DataTable({
'paging': true,
responsive: true,
"autoWidth": false,
"language": {
"paginate": {
"next": "<i class='fa fa-chevron-right' style='color:#fff;'></i>",
"previous": "<i class='fa fa-chevron-left' style='color:#fff;'></i>"
}
},
"columnDefs": [
{ "orderable": false, "targets": 3 },
{
type : "date",
"targets": 2,
render: $.fn.dataTable.moment('MM/DD/YYYY')
}
],
"fnDrawCallback": function (evnt) {
$('.dataTables_info').addClass('Hidden');
var rows = $("#FindUserTable").dataTable().fnGetNodes();
var noRecordFound = $('.NoRecordFound ');
if (rows.length === 0 && noRecordFound.length === 1) {
$('.dataTables_empty').text('No record found.');
} else if (rows.length === 0 && noRecordFound.length === 0) {
$('.dataTables_empty').addClass('Hidden');
}
if ($('#FindUserTable_paginate ul.pagination li.paginate_button').size()> 2) {
$('#FindUserTable_paginate')[0].style.display = "block";
} else {
$('#FindUserTable_paginate')[0].style.display = "none";
}
}
});
<table class="table table-responsive table-condensed" id="FindUserTable>
<thead>
<tr role="row"><th style="width:30%" class="sorting_asc" tabindex="0" aria-controls="FindUserTable" rowspan="1" colspan="1" aria-sort="ascending" aria-label="
User Last Name: activate to sort column descending">
<label class="dataTableLabel"> User Last Name</label>
<br>
<div style="padding-bottom:.75em;">
<input class="form-control fullWidth" id="LastName" name="LastName" type="text" value="">
</div>
</th><th style="width:30%" class="sorting" tabindex="0" aria-controls="FindUserTable" rowspan="1" colspan="1" aria-label="
User First Name
: activate to sort column ascending">
<label class="dataTableLabel"> User First Name</label>
<br>
<div style="padding-bottom:.75em;">
<input class="form-control fullWidth" id="FirstName" name="FirstName" type="text" value="pat">
</div>
</th><th style="width:20%" class="sorting" tabindex="0" aria-controls="FindUserTable" rowspan="1" colspan="1" aria-label="
Date Of Birth:
activate to sort column ascending">
<label class="dataTableLabel">Date Of Birth</label>
<br>
<div style="padding-bottom:.75em;">
<input class="form-control fullWidth" data-val="true" data-val-date="The field DOB must be a date." id="DateOfBirth" name="DOB" type="text" value="">
</div>
</th><th style="width:20%; padding-bottom:.7em" rowspan="1" colspan="1">
<a id="searchUser" name="searchUser"><i class="glyphicon glyphicon-search fa-2x"></i></a>
</th></tr>
</thead>
<tbody class="findUserbody">
<tr role="row" class="odd">
<td style="text-align: center;" class="sorting_1"><div class="findUserData"> <input data-value="44" id="44" name="SelectedUser" type="radio" value="44"> Four </div></td>
<td style="text-align: center; "><div class="findUserData">User</div></td>
<td style="text-align: center; "><div class="findUserData"> 05/29/1996</div></td>
<td></td>
</tr><tr role="row" class="even">
<td style="text-align: center;" class="sorting_1"><div class="findUserData"> <input data-value="41" id="41" name="SelectedUser" type="radio" value="41"> One </div></td>
<td style="text-align: center; "><div class="findUserData">User</div></td>
<td style="text-align: center; "><div class="findUserData"> 07/31/2000</div></td>
<td></td>
</tr><tr role="row" class="odd">
<td style="text-align: center;" class="sorting_1"><div class="findUserData"> <input data-value="43" id="43" name="SelectedUser" type="radio" value="43"> Three </div></td>
<td style="text-align: center; "><div class="findUserData">User</div></td>
<td style="text-align: center; "><div class="findUserData"> 10/06/1996</div></td>
<td></td>
</tr><tr role="row" class="even">
<td style="text-align: center;" class="sorting_1"><div class="findUserData"> <input data-value="42" id="42" name="SelectedUser" type="radio" value="42"> Two </div></td>
<td style="text-align: center; "><div class="findUserData">User</div></td>
<td style="text-align: center; "><div class="findUserData"> 05/28/2000</div></td>
<td></td>
</tr></tbody>
</table>

The issue was with the tds that have the dates. There was a div inside the TD which was causing the problem. During sorting, the div is also included as part of the date, and moment.js could not understand the date.

Related

[Vue warn]: Extraneous non-props attributes (id) were passed to component but could not be automatically inherited because component renders fragment

I learn VUE.JS and I got this two errors while I tried to get data values and make Edit/Update functionality.
I managed to get all data but I got warning:
[Vue warn]: Extraneous non-emits event listeners (editedStation) were passed to component but could not be automatically inherited because component renders fragment or text root nodes. If the listener is intended to be a component custom event listener only, declare it using the "emits" option.
at <StationItem key=121 id=121 line="test" ... >
at <StationList onVnodeUnmounted=fn<onVnodeUnmounted>
[Vue warn]: Extraneous non-props attributes (id) were passed to component but could not be automatically inherited because component renders fragment
in console after sending the data from the row into form.
Child component:
<template>
<tr>
<td style="padding: 10px">{{ line }}</td>
<td style="padding: 10px">{{ physLine }}</td>
<td style="padding: 10px">{{ wsType }}</td>
<td style="padding: 10px">{{ ws }}</td>
<td style="padding: 10px">{{ name }}</td>
<td style="padding: 10px">{{ isValid }}</td>
<td style="padding: 10px">{{ stationGroupId }}</td>
<td style="padding: 10px">{{ order }}</td>
<td style="padding: 10px" class="actions">
<button #click="editStation($event)" v-bind:id="stationId">
{{ editIsClicked ? "Done" : "Edit" }}
</button>
</td>
</tr>
<tr v-if="editIsClicked" style="background-color: #ffda99">
<td colspan="3" style="padding: 30px 0px 30px 30px">
<div class="form-control" style="margin-bottom: 10px; width: 80%">
<label for="line" style="display: block; text-align: left; font-size: 14px"
>Line Number</label
>
<input
type="text"
:value="station.lineNum"
style="width: 90%; display: block; text-align: left"
/>
</div>
<div class="form-control" style="margin-bottom: 10px; width: 80%">
<label
for="physLine"
style="width: 90%; display: block; text-align: left; font-size: 14px"
>
Phys line
</label>
<input
type="text"
:value="station.physLine"
style="width: 90%; display: block; text-align: left"
/>
</div>
<div class="form-control" style="margin-bottom: 10px; width: 80%">
<label
for="wsType"
style="width: 90%; display: block; text-align: left; font-size: 14px"
>WS Type</label
>
<input
type="text"
:value="station.wsType"
style="width: 90%; display: block; text-align: left"
/>
</div>
<div
class="form-control"
style="width: 50%; margin-bottom: 10px; border: 1px solid #b5b5b5"
>
<label
for="isValid"
style="width: 40%; display: block; text-align: left; font-size: 14px"
>is Valid</label
>
<input type="checkbox" :value="station.isValid" style="margin-left: -350px" />
</div>
<p v-if="!isValid.isValid">{{ errorMessage }}</p>
<tr v-for="(btn, index) in filteredButtonList" v-bind:key="btn.id">
<td>{{ btn.buttonListName }}</td>
<td>{{ btn.buttonListDesc }}</td>
<td>
<select
#change="onQuidoIdChange($event)"
class="form-select form-control required"
v-bind:id="index + 1"
>
<option
class="quido-id"
v-for="entry in selectedQuidoId"
:value="entry.quidoId"
selected
:key="entry.quidoId"
disabled
hidden
>
{{ entry.quidoName }}
</option>
<option
v-for="quido in filteredQuidosList"
:value="quido.quidoId"
v-bind:key="quido.quidoId"
v-bind:id="quido.quidoId"
>
{{ quido.quidoName }}
</option>
</select>
</td>
<td>
<tr>
<td>
<input
#change="onInputChange($event)"
type="number"
class="input-custom"
:placeholder="`Input ${index + 1}`"
v-bind:id="index + 1"
:value="station.stationButtons[index].input"
/>
</td>
</tr>
</td>
</tr>
</td>
<td colspan="6" style="padding-bottom: 250px:">
<div class="form-control" style="margin-bottom: 10px">
<label style="display: block; text-align: left; font-size: 14px" for="wsName"
>WS Name</label
>
<input
type="text"
:value="station.wsName"
style="width: 80%; display: block; text-align: left"
/>
</div>
<div class="form-control" style="margin-bottom: 10px">
<label style="display: block; text-align: left; font-size: 14px" for="wsName"
>Position Name</label
>
<input
type="text"
:value="station.posName"
style="width: 80%; display: block; text-align: left"
/>
</div>
<div class="form-control" style="margin-bottom: 10px">
<label
style="width: 80%; display: block; text-align: left; font-size: 14px"
for="wsName"
>Order</label
>
<input
type="text"
:value="station.order"
style="width: 80%; display: block; text-align: left"
/>
</div>
<div class="form-control" style="width: 82%">
<label for="stationGroupId">Choose Station Group</label>
<select
#change="changeStationGroupId($event)"
style="width: 100%"
v-bind:id="index + 1"
class="required"
>
<option
class="stgr-id"
v-for="entry in selectedStationGroupId"
:value="entry.stationGroupId"
selected
:key="entry.stationGroupId"
disabled
hidden
>
{{ entry.stationGroupsName }}
</option>
<option
v-for="stGrId in filteredStationGroups"
:value="stGrId.stationGroupsId"
v-bind:key="stGrId.stationGroupsId"
>
{{ stGrId.stationGroupsName }}
</option>
</select>
</div>
</td>
</tr>
</template>
Parent component:
<template>
<div class="section-global">
<station-form #save-data="saveData"> </station-form>
</div>
<div class="section-global">
<table v-if="hasStations" class="render-list-table">
<thead>
<!-- <th>Line ID</th> -->
<th>Line num</th>
<th>Phys Line</th>
<th>WS Type</th>
<th>WS Name</th>
<th>Pos. name</th>
<th>isValid</th>
<th>Station Group ID</th>
<th>Order</th>
<th>Actions</th>
</thead>
<tbody>
<station-item
v-for="station in filteredStations"
:key="station.stationId"
:id="station.stationId"
:station-id="station.stationId"
:line="station.lineNum"
:phys-line="station.physLine"
:ws-type="station.wsType"
:ws="station.wsName"
:name="station.posName"
:order="station.order"
:is-valid="station.isValid"
:stationGroupId="station.stationGroupId"
#editedStation="editFormData($event)"
>
</station-item>
</tbody>
</table>
<h1 v-else>NO STATIONS FOUND</h1>
</div>
</template>
<script>
import StationItem from "#/components/stations/StationItem.vue";
import StationForm from "#/components/stations/StationForm.vue";
export default {
data() {
return {
editedDataToBeSendToFormInputs: [],
};
},
components: {
StationItem,
StationForm,
},
computed: {
filteredStations() {
return this.$store.getters["stations/stations"];
},
hasStations() {
return this.$store.getters["stations/hasStations"];
},
probaFiltered() {
return this.$store.getters["stations/probaGetter"];
},
},
created() {
this.loadStations();
this.loadProba();
},
methods: {
editFormData(event) {
const dataToBeEdited = event;
// this.editedDataToBeSendToFormInputs.push(dataToBeEdited);
const test = this.probaFiltered;
test.push(dataToBeEdited);
this.loadProba();
},
saveData(data) {
this.$store.dispatch("stations/addStation", data);
},
loadStations() {
this.$store.dispatch("stations/loadStationList");
}
},
};
</script>
Problem visualisation.
Can someone help me get rid of this browser warnings?
I tried a couple of solutions from StackOverflow but without success. I wrapped the t-row in div tag and I got another issue with the render, I cannot render div as t-row in the next component where I need that data.
Thanks in advance.
After failing to send data via EventBus into Create section (A1) i decided to make form inside the component that I render (B2) and emit the data into B1 component.
I try to send the data from Child to Parent (from B2 to B1) but I get the error above. App works but I hate errors.
Check this the first warning.
And declare your component editedStation event with the emits option inside the component.
It seems for me, that you are using Vue 3. If so, then please remove the Vue 2 tag.

How to pass value to model (Vuejs 3)

in my Vuejs3 project, there is a form in which the new row will be generated by pressing a button, so I put an array to handle new rows then I need to validate inputs, but after validation of the input of the array, the value didn't pass to model, but it works without the validation.. please help me to understand the mistake that I did.
The Form:
<table class="table table-stripped table-border">
<thead>
<tr>
<th>#</th>
<th style="width: 18%">
Medikament <span class="text-danger">*</span>
</th>
<th style="width: 9%">Milligram</th>
<th style="width: 9%">
Packung <span class="text-danger">*</span>
</th>
<th style="width: 7%">
Stück <span class="text-danger">*</span>
</th>
<th style="width: 19%">Apothekenname</th>
<th style="width: 24%">Adresse der Apotheke</th>
<th style="width: 14%">Postleitzahl</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in patientRequestForCreationDtos" :key="index">
<td>{{ index + 1 }}</td>
<td>
<input type="text" v-model="item.drugName" class="form-control" />
</td>
<td>
<input type="number" v-model="item.milligram" class="form-control" />
</td>
<td>
<input type="number" v-model="item.box" class="form-control" />
</td>
<td>
<input type="number" v-model="item.total" class="form-control" />
</td>
<td>
<input type="text" class="form-control" v-model="item.drugStoreName" :readonly="patientDetails.isElga == false" />
</td>
<td>
<input type="text" class="form-control" v-model="item.drugStoreAddress" :readonly="patientDetails.isElga == false" />
</td>
<td>
<input type="text" class="form-control" v-model="item.drugStorePostalCode" :readonly="patientDetails.isElga == false" />
</td>
<td>
<button type="button" #click="deleteRequestItemRow(index)" class="btn btn-sm btn-danger">
-
</button>
</td>
</tr>
</tbody>
</table>
The Array:
patientRequestForCreationDtos: [{
milligram: null,
box: null,
total: null,
drugStoreName: "",
drugStoreAddress: "",
drugStorePostalCode: "",
drugName: "",
}, ],
The validation function:
checkValidation() {
if (!this.patientRequestForCreationDtos.drugName) {
Swal.fire("drug name is required...");
return;
}
return true;
},
```js
---
it always says => drug name is required..
this.patientRequestForCreationDtos is array. maybe you can do this.
checkValidation(patientRequestForCreationDto) {
if (!patientRequestForCreationDto.drugName) {
Swal.fire("drug name is required...");
return;
}
return true;
},
if you'll have only one element in patientRequestForCreationDtos then you gotta choose first element in the array and then check its property
checkValidation() {
if (!this.patientRequestForCreationDtos[0].drugName) {
Swal.fire("drug name is required...");
return;
}
return true
},
also if your patientRequestForCreationDtos is always gonna be an array then you might find this helpful

Delete row from Vue Js dynamic array

I am trying to create a dynamic table in Vue. I am able to add row properly, but when I try to remove the row, every time it removes the last row by default.
Even if I hard coded the index number, still same result.
Its been 2 days and I am stuck in this problem. Appreciate any help.
My Template:
<div class="box-body">
<table id="myTable" class="table">
<thead>
<tr class="timesheetTableHeader">
<th style="width: 50rem">Project</th>
<th style="width: 40rem">Activity</th>
<th style="width: 20rem">Charge Back</th>
<th style="width: 10rem">Hours</th>
</tr>
</thead>
<div class="mt-2"></div>
<tbody>
<tr v-for="(row, index) in rows">
<td>
<mwselect :options="Project"
v-model="row.projectName"
></mwselect>
</td>
<td>
<mwselect :options="Activity"
v-model="row.activity"
></mwselect>
</td>
<td>
<mwselect :options="CostCenter"
v-model="row.chargeBackCC"
></mwselect>
</td>
<td>
<b-form-input type="number"
class="pb-1 pt-1"
required
v-model="row.hours"
name="Hours">
</b-form-input>
</td>
<td>
<a #click="removeElement(index)" style="cursor: pointer">
<i class="fa fa-trash-o pt-1" style="color:red; text-shadow: 1px 1px 1px #ccc;
font-size: 1.5em;" title="Delete Line"
></i></a>
</td>
</tr>
</tbody>
</table>
</div>
My Script:
<script>
var period_name = ''
var start_date = ''
var end_date = ''
var counter = 0
export default {
components: {
mwselect
},
data() {
return {
Project: projects,
CostCenter: costcenter,
Activity: activities,
rows: []
} // return end
},
methods: {
createLine(index) {
var elem = document.createElement('tr');
this.rows.push({
projectName: "",
activity: "",
chargeBackCC: "",
hours: ""
})
},
removeElement: function (index) {
alert(index)
this.rows.splice(index, 1);
}
}
}
</script>
Ordering is not guaranteed unless you put key attribute to the for loop. So this change to your tag must solve the problem:
<tr v-for="(row, index) in rows" :key="index">
Here is more information about the attribute key: https://v2.vuejs.org/v2/guide/list.html#key
Thanks Cmertayak,
Adding :key="index" worked and it solved my problem.

Is it possible to order jquery datatable by grouped column headers. If so, where to begin?

class='sorting'I have a table that I added an additional row within the tag to which I am using to indicate grouping of the columns below it.
I would like to add sorting by the grouping row (top row) so for example when "Group 1" is clicked it sorts the table by the columns under that group (e.g. col1, col2 then by col3).
I am not even sure if this is possible naively in datatables. I would prefer to avoid a jquery hack. My searches have only produced results on row grouping.
datatables version: 1.10.0-beta.1
jsfiddle: https://jsfiddle.net/mrgulick/usfo2gys/4/
<div id="demo">
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="display" id="example">
<thead>
<tr>
<th></th>
<th colspan="3" style="background-color:green;border:solid thin;" class='sorting'>Group 1</th>
<th colspan="3" style="background-color:blue;border:solid thin;" class='sorting'>Group 2</th>
<th colspan="2" style="background-color:red;border:solid thin;" class='sorting'>Group 3</th>
</tr>
<tr>
<th style="background-color:gray;border:solid thin;">ungrouped</th>
<th style="background-color:green;border:solid thin;">col 1</th>
<th style="background-color:green;border:solid thin;">col 2</th>
<th style="background-color:green;border:solid thin;">col 3</th>
<th style="background-color:blue;border:solid thin;">col 4 </th>
<th style="background-color:blue;border:solid thin;">col 5</th>
<th style="background-color:blue;border:solid thin;">col 6</th>
<th style="background-color:red;border:solid thin;">col 7</th>
<th style="background-color:red;border:solid thin;">col 8</th>
</tr>
</thead>
<tbody>
<tr>
<td>string value 1</td>
<td style="text-align: center;">
<input type="checkbox" checked />
</td>
<td style="text-align: center;">
<input type="checkbox" />
</td>
<td style="text-align: center;">
<input type="checkbox" checked />
</td>
<td style="text-align: center;">
<input type="checkbox" />
</td>
<td style="text-align: center;">
<input type="checkbox" />
</td>
<td style="text-align: center;">
<input type="checkbox" checked />
</td>
<td style="text-align: center;">
<input type="checkbox" />
</td>
<td style="text-align: center;">
<input type="checkbox" checked />
</td>
</tr>
<tr>
<td>string value 2</td>
<td style="text-align: center;">
<input type="checkbox" />
</td>
<td style="text-align: center;">
<input type="checkbox" checked />
</td>
<td style="text-align: center;">
<input type="checkbox" />
</td>
<td style="text-align: center;">
<input type="checkbox" checked />
</td>
<td style="text-align: center;">
<input type="checkbox" checked />
</td>
<td style="text-align: center;">
<input type="checkbox" />
</td>
<td style="text-align: center;">
<input type="checkbox" />
</td>
<td style="text-align: center;">
<input type="checkbox" checked />
</td>
</tr>
<tr>
<td>string value 2</td>
<td style="text-align: center;">
<input type="checkbox" checked/>
</td>
<td style="text-align: center;">
<input type="checkbox" />
</td>
<td style="text-align: center;">
<input type="checkbox" checked/>
</td>
<td style="text-align: center;">
<input type="checkbox" />
</td>
<td style="text-align: center;">
<input type="checkbox" checked/>
</td>
<td style="text-align: center;">
<input type="checkbox" checked/>
</td>
<td style="text-align: center;">
<input type="checkbox" checked/>
</td>
<td style="text-align: center;">
<input type="checkbox" />
</td>
</tr>
</tbody>
</table>
</div>
js below
var dt = $('#example').DataTable(dtOptions);
var dtOptions = {
"dom": '<"clearfix"><"pull-left"l><"pull-right"Bf>r<"horizontal-scroll"t><"pull-left"i><"pull-right"p><"clearfix">',
"order": [],
"columnDefs": [{
"targets": [0],
"visible": true,
"searchable": true
}],
"aoColumns": [
null, {
"orderSequence": ["desc", "asc"]
}, {
"orderSequence": ["desc", "asc"]
}, {
"orderSequence": ["desc", "asc"]
}, {
"orderSequence": ["desc", "asc"]
}, {
"orderSequence": ["desc", "asc"]
}, {
"orderSequence": ["desc", "asc"]
}, {
"orderSequence": ["desc", "asc"]
}, {
"orderSequence": ["desc", "asc"]
}
]
}

Clear input fields onfocus

I'm having problems creating a couple functions to clear an input field onfocus and reset it back to the default text onblur if it's empty. It works fine until you change some of the text and then backspace it out.
My functions are:
function enterField (id, defaultText) {
if (document.getElementById(id).value == defaultText) {
document.getElementById(id).setAttribute("value", "");
document.getElementById(id).style.color = "#000000";
}
}
function leaveField (id, defaultText) {
if (document.getElementById(id).value == "" || document.getElementById(id).value == defaultText) {
document.getElementById(id).setAttribute("value", defaultText);
document.getElementById(id).style.color = "#dddddd";
}
}
And my html is:
<table style="width: 464px; margin-right: -7px; margin-left: -7px; table-layout: fixed;" cellpadding="0" cellspacing="8">
<tr>
<td class="input"><input id="name" type="text" class="name" size="32" value="Name" onfocus="enterField('name', 'Name');" onblur="leaveField('name', 'Name');"></td>
<td class="input"><input id="email" type="text" class="name" size="32" value="Email" onfocus="enterField('email', 'Email');" onblur="leaveField('email', 'Email');"></td>
</tr>
<tr>
<td class="input" colspan="2"><textarea id="message" class="message" rows="8" onfocus="enterField('message', 'Message');" onblur="leaveField('message', 'Message');">Message</textarea></td>
</tr>
<tr>
<td style="text-align: right;" colspan="2"><input id="submit" type="submit" class="button" value="Send"></td>
</tr>
</table>
You can see how it operates at www.ag-designs.ca/index_new.php. Click "Let's Talk" at the bottom.
change
document.getElementById(id).setAttribute("value", defaultText);
to
document.getElementById(id).value = defaultText;
at both occurrences in your functions.