JQuery cannot find element with the same class on the same page - javascript

I'm trying to set the text of an HTML element but I'm unable to do so through JQuery
class selector .text() function. It is somehow unable to find the element to set. The function was able to find the element when it was called in the document.ready() function but due to functional requirements, I moved it into an API call instead. The API call is unable to find and set a particular set of elements. There are other elements on the page utilizing the same class which was set successfully.
Documented below are the two boot grid tables in which column fields I'm trying to set the values. Note that they exist on the same HTML page.
Currently only the columns in ItemTbl can be set and reflected by the $(".accrMonth").text(values) function.
I've attempted to switch their position on the webpage but still only ItemTbl is being set
Attached a screenshot Output of searching for the class
function setDatesSO(dateString) {
var dateParts = dateString.split("/");
var reviewPeriod = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
console.log("table 1: " + $("#test1").hasClass("accrMonth2"));
console.log("table 1: " + $("#test2").hasClass("accrMonth1"));
console.log("table 1: " + $("#test3").hasClass("accrMonth"));
console.log("table 2: " + $("#test4").hasClass("accrMonth2"));
console.log("table 2: " + $("#test5").hasClass("accrMonth1"));
console.log("table 2: " + $("#test6").hasClass("accrMonth"));
reviewPeriod.setMonth(reviewPeriod.getMonth() - 1);
$(".accrMonth").text(reviewPeriod.toLocaleString('en-GB', {
month: 'long'
}));
reviewPeriod.setMonth(reviewPeriod.getMonth() - 1);
$(".accrMonth1").text(reviewPeriod.toLocaleString('en-GB', {
month: 'long'
}));
reviewPeriod.setMonth(reviewPeriod.getMonth() - 1);
$(".accrMonth2").text(reviewPeriod.toLocaleString('en-GB', {
month: 'long'
}));
}
<table id="OverviewTbl" class="table table-striped table-hover" style="word-wrap: break-word;">
<thead>
<tr>
<th data-column-id="accrMonth2" id="test1" class="accrMonth2">MONTH-3</th>
<th data-column-id="accrMonth1" id="test2" class="accrMonth1">MONTH-2</th>
<th data-column-id="accrMonth" id="test3" class="accrMonth">MONTH-1</th>
</tr>
</thead>
</table>
<table id="ItemTbl" class="table table-striped table-hover" style="word-wrap: break-word;">
<thead>
<tr>
<th data-column-id="accrMonth2" id="test4" class="accrMonth2">MONTH-3</th>
<th data-column-id="accrMonth1" id="test5" class="accrMonth1">MONTH-2</th>
<th data-column-id="accrMonth" id="test6" class="accrMonth">MONTH-1</th>
</tr>
</thead>
</table>
Function API call,note that startDate value is valid as results are updated for ItemTbl.It is called when user clicks a button.
$.ajax({
type: "POST",
url: getReviewById,
data: JSON.stringify(form),
contentType: "application/json; charset=utf-8",
dataType: "json",
beforeSend: function (xhr) {
xhr.setRequestHeader(csrfHeader, csrfToken);
},
success: function (jd) {
setDatesSO(jd.startDate);
},
});

I dont see error as you could see in my sample
console.log($('#test1').hasClass("accrMonth2"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="OverviewTbl" class="table table-striped table-hover" style="word-wrap: break-word;">
<thead>
<tr>
<th data-column-id="accrMonth2" id="test1" class="accrMonth2">MONTH-3</th>
<th data-column-id="accrMonth1" id="test2" class="accrMonth1">MONTH-2</th>
<th data-column-id="accrMonth" id="test3" class="accrMonth">MONTH-1</th>
</tr>
</thead>
</table>
<table id="ItemTbl" class="table table-striped table-hover" style="word-wrap: break-word;">
<thead>
<tr>
<th data-column-id="accrMonth2" id="test4" class="accrMonth2">MONTH-3</th>
<th data-column-id="accrMonth1" id="test5" class="accrMonth1">MONTH-2</th>
<th data-column-id="accrMonth" id="test6" class="accrMonth">MONTH-1</th>
</tr>
</thead>
</table>
are you sure your table is loaded?

Related

How can i auto filter on javascript laravel?

I don't want to redirect page when I want to filter the table so how can I make it in javascript?
this is my javascript
javascript
document.getElementById('inputdismissal').value = programId
$('#modalAddDismissal').modal("show",function(){
$('#inputdismissal').val(programId).trigger('change');
$('#inputdismissal').change(function(){
//fire your ajax call
var value = $(this).val().toLowerCase();
$("#myTable tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
})
})
and i have this modal,
modal.php
<div class="modal-header">
<input class="form-control" id="inputdismissal" type="text" readonly/>
</div>
<div class="modal-body">
<table id="tblDisNikName" class="table table-sm table-responsive table-hover table-bordered">
<thead class="thead-dark">
<tr>
<th scope="col">Select</th>
<th scope="col">NIK</th>
<th scope="col">Nama</th>
<th scope="col">Position</th>
<th scope="col">Program</th>
</tr>
</thead>
<tbody id="myTable" class="cursor-pointer">
#foreach($dismissalcrew as $datadis)
<tr>
<td><input class="dismissalTest" type="checkbox"></td>
<td>{{$datadis->employee_nik}}</td>
<td>{{$datadis->employee_nama}}</td>
<td>{{$datadis->crewprogramposition_name}}</td>
<td>{{$datadis->show_name}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
I already get my value into inputDismissal and show up on my modal. but the table didn't get filtered at all, how can I just show filtered table when I call the modal using inputDismisal, I mean getting filtered automatically as I call the modal
SO its basically very simple, i just use jquery and put the value
function filterInput(programId){
$(document).ready(function(){
var value = programId.toLowerCase();
$("#myTable tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
$('#modalAddDismissal').modal("show")
}
just put the modal after the filter and it's work

how to send object from html ng-repeat to js function

I am getting data from db and put it in a scope, then iterate on these data by ng-repeat in html.
what i am trying to do is to send the selected object when the user check on a checkbox to get another data based on an id for example . i tried to use ng-model and ng-change on the checkbox but it sends only true or false .
HTML
<div id="frame" class="widget-content" style="height:50vh; overflow:auto;">
<table style="font-size: 12px;display: inline-block;">
<thead>
<tr>
<th>Check</th>
<th>Group Code</th>
<th>Group Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="g in Groups | unique : 'Group_code'"
ng-click="showInEdit(g)"
ng-class="{selected:g === selectedRow}">
<td><input type="checkbox" ng-model="g"
ng-true-value="g" ng-change="Getrequest2(g)"/>
<td>{{g.Group_code}}</td>
<td>{{g.group_name_latin}}</td>**
</tr>
</tbody>
</table>
<table style="font-size: 12px; float: left;">
<thead>
<tr>
<th>Check</th>
<th>Item Code</th>
<th>Item Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="i in items | unique : 'Item_code'"
ng-click="showInEdit(i)"
ng-class="{selected:i === selectedRow}">
<td><input type="checkbox" />
<td>{{i.Item_code}}</td>
<td>{{i.ITEM_NAME_LATIN}}</td>
</tr>
</tbody>
</table>
</div>
angularjs
$scope.Getrequest1 = function (g) {
//console.log(g);
$scope.typecd = g.TypeCode;
$scope.ShowLoading('slow');
LOASrv.GetGroup("LOAApprovalSetup/GetGroup" +
"?typ=" + g.TypeCode +
'&hospid=' + $scope.hospitalid +
'&userky=' + $scope.userkey
).then(function (response) {
$scope.Groups = (response.data);
$scope.HideLoading('slow');
})
}
$scope.Getrequest2 = function (i) {
console.log(i);
$scope.ShowLoading('slow');
LOASrv.GetItem("LOAApprovalSetup/GetItem" +
"?typ=" + $scope.typecd +
'&hospid=' + $scope.hospitalid +
'&userky=' + $scope.userkey +
'&groupcd=' + i
).then(function (response) {
$scope.items = (response.data);
$scope.HideLoading('slow');
})
}
You’re assigning ‘g’ object to checkbox ng-model.
This changes the value of ‘g’ from object to boolean on checkbox select.
That’s why you’re getting true/false
Change ng-model=“g.someBooleanName” and try

Laravel error Unauthenticated when passing Datatable from view to controller

Hello i'm trying to pass Datatable from view to my controller. Here is my Datatable code in view :
<div class="table-responsive">
<table id="example1" class="table table-bordered table-hover display">
<thead>
<tr>
<th><input type="checkbox" name="select_all" value="1" id="example-select-all" /></th>
<th>No</th>
<th>Id</th>
<th>Exemplar</th>
<th width="40%">Title</th>
</tr>
</thead>
<tbody id="bookTable"></tbody>
</table>
</div>
And here is my javascript code :
$('#printLabel').click(function(e) {
e.preventDefault();
$.post("{{ url('admin/printselectedlabel') }}",
//$('#searchForm').serialize())
table1.$('input,select,textarea').serialize()).fail(function(data) {
alert(data.responseJSON.error);
});
});
The codes above gave me error : {error: "Unauthenticated."}. Also, i'm not sure if "table1.$('input,select,textarea').serialize())" is the right why to pass datatable to controller.
Here is my route :
Route::group(['prefix' => 'admin'], function () {
Route::post('/printselectedlabel', 'Admin\PrintLabelController#printSelectedLabel');});
I havent add any code yet inside the printSelectedLabel() function inside the PrintLabelController
public function __construct()
{
$this->middleware('admin');
}public function printSelectedLabel(Request $request){
}

How to make href work in jquery-bootgrid basic plugin

Am using bootgrid-basic to show my data,
<table id="grid-basic"
class="table table-bordered table-striped table-condensed mb-none">
<thead>
<th data-column-id="aa">aa</th>
<th data-column-id="ss" data-order="desc">ss</th>
<th data-column-id="dd">dd</th>
<th data-column-id="ff">ff</th>
<th data-column-id="aaa">aaa</th>
<th data-column-id="aaaaa" >aaaAa</th>
</tr>
</thead>
<tbody>
#foreach($alldata as $data)
<tr>
<td>{{$data->aa}}</td>
<td>{{$data->ss}}</td>
<td>0</td>
<td>{{$data->dd}}</td>
<td>{{$data->ff}}</td>
<td>ASSSsdf</td>
</tr>
#endforeach
</tbody>
</table>
and initialized $("#grid-basic").bootgrid(); in script.
Everything is working fine like search,data ordering ,pagination but those links doesnt see to work.
If i use formatter links work and remaining doesnt work.
$("#grid-basic").bootgrid(
formatters: {
"action": function (column, row)
{
return '<a href=\"/model/' + row.actions + '"\>' +row.actions+ '</a>' ;
}});
A jsfiddle link here : http://jsfiddle.net/6xpyxbcg/
There is a parentheses missing in your JS, it should be bootgrid({ and you need to add data-formatter="link" to the th tag of the column you wish to use the formatter on (i.e. the link column).
HTML
<th data-column-id="link" data-formatter="link" >Received</th>
JQuery
$(function()
{
$("#grid-basic").bootgrid({
formatters: {
"link": function(column, row)
{
return "" + row.link + "";
}
}
}
)
});
Demo in jsFiddle
P.S. trying using the built in snippet next time, as there is a one click button that allows you to copy the code to the answer section and amend it accordingly.

Data Table hangs sporadically (No data available in table) when data sorted or loaded with Knockout binding

I am binding data to a table using Knockout JS and the JQuery/Bootstrap based; Data Table API. The table becomes unresponsive sporadically when sorted or loaded. There are no errors in the log.
It also shows 0 of 0 data as illustrated in the screenshot below:
I have seen similar errors/issues but could not get a solutions for them, E.g. This post:
Code:
function viewModel(){
var self = this;
self.Data = ko.observableArray([]);
$.getJSON('https://restcountries.eu/rest/v1/all', function(data){
self.Data(data);
});
}
ko.applyBindings(viewModel());
$(document).ready(function() {
$('#example').dataTable();
});
HTML:
<div class="table-responsive">
<table id="example" cellspacing="0"
class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th scope="col">Country</th>
<th scope="col">Capital</th>
<th scope="col">Population</th>
<th scope="col">Region</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="col">Country</th>
<th scope="col">Capital</th>
<th scope="col">Population</th>
<th scope="col">Region</th>
</tr>
</tfoot>
<tbody data-bind="foreach: Data">
<tr>
<td data-bind="text: name"></td>
<td data-bind="text: capital"></td>
<td data-bind="text: population"></td>
<td data-bind="text: region"></td>
</tr>
</tbody>
</table>
</div>
Here is a full working example (JSFiddle) utilizing a REST API so that the exact problem is accurately replicated:
I think the problem with your example may be with how you're dealing with your data when you get it back from the API call.
I've put together a quick example that achieves what I think you're trying to achieve and the sorting and searching work quickly for me.
When I get the JSON data back from the API, I use the Knockout arrayMap utility function to create an array of "Country" objects that have observable properties that I have mapped the JSON data to. I've bound the table to my observableArray of Country objects.
Initialising the data table in the same way you have works fine for me in this case.
The full working solution is here: http://plnkr.co/edit/eroIox6zqBFOVnf86Mdk?p=preview
script.js
var ViewModel = function(jsonData) {
var countries = ko.utils.arrayMap(jsonData, function(item) {
return new Country(item)
});
this.Countries = ko.observableArray(countries);
};
var Country = function(jsonItem) {
this.Name = ko.observable(jsonItem.name);
this.Capital = ko.observable(jsonItem.capital);
this.Population = ko.observable(jsonItem.population);
this.Region = ko.observable(jsonItem.region);
};
window.onload = function() {
$.getJSON('https://restcountries.eu/rest/v1/all', function(data) {
ko.applyBindings(new ViewModel(data));
$("#example").dataTable();
});
}
index.html
<table id="example" cellspacing="0" class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th scope="col">Country</th>
<th scope="col">Capital</th>
<th scope="col">Population</th>
<th scope="col">Region</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="col">Country</th>
<th scope="col">Capital</th>
<th scope="col">Population</th>
<th scope="col">Region</th>
</tr>
</tfoot>
<tbody data-bind="foreach: Countries">
<tr>
<td data-bind="text: Name"></td>
<td data-bind="text: Capital"></td>
<td data-bind="text: Population"></td>
<td data-bind="text: Region"></td>
</tr>
</tbody>
</table>
Tables are fairly slow to render within Knockout, and if your table is based on a computed, I could see how you could have some issues with redrawing time. But that's not happening here.
Apart from loading the data and binding it to the table rows, there's no data manipulation going on in your viewmodel. All the data manipulation is done by the dataTable plug-in, which you initialize with a single jQuery call. Properly, that should be done within a binding handler. You also need to know what is going on within the plug-in when it sorts, filters, or whatever it does, because you may need to mediate those changes back to your observableArray within your binding handler.
Bottom line: you need a binding handler for the dataTable. There may be one already written; I haven't Googled for it. Give that a try.

Categories