How do i get to the table header = "1" - javascript

In this example:
function onRowClickHandler(evt){
var clickedTaxLotId = grid.getItem(evt.rowIndex).PARCELID;
var selectedTaxLot;
dojo.forEach(map.graphics.graphics,function(graphic){
if((graphic.attributes) && graphic.attributes.PARCELID === clickedTaxLotId){
selectedTaxLot = graphic;
return;
}
});
var taxLotExtent = selectedTaxLot.geometry.getExtent();
map.setExtent(taxLotExtent);
}
And they select the PARCELID from here:
<th field="PARCELID">Parcel ID</th>
<th field="OWNERNME1" >Owner 1</th>
<th field="OWNERNME2">Owner 2</th>
<th field="RESYRBLT ">Year Built</th>
<th field="SITEADDRESS" width="100%">Address</th>
But now my problem is that i have a <th field="1"> that isnt changeable. But i need to get that value like this var clickedTaxLotId = grid.getItem(evt.rowIndex).2; but in javascript that isnt possible.
table header looks like this:
<div data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'bottom'" style="height:350px;" >
<table data-dojo-type="dojox.grid.DataGrid" jsid="grid" id="grid" data-dojo-props="rowsPerPage:'5', rowSelector:'20px'">
<thead>
<tr>
<th field="0" width="auto" >
Gezocht op
</th>
<th field="1" width="auto" >
Gevonden
</th>
</tr>
</thead>
</table>
EDIT
Here i set the value of the th field. This gets done by dataForGrid. the first field is "0" and gets filled with result.foundFieldName, "1" with result.value
map.graphics.clear();
var dataForGrid = [];
//Build an array of attribute information and add each found graphic to the map
dojo.forEach(results, function(result) {
var graphic = result.feature;
dataForGrid.push([result.foundFieldName, result.value]);
var userlabel = result.value;
switch (graphic.geometry.type) {
case "point":
graphic.setSymbol(markerSymbol);
break;
case "polyline":
graphic.setSymbol(lineSymbol);
break;
case "polygon":
graphic.setSymbol(polygonSymbol);
break;
}
map.graphics.add(graphic);
});
var data = {
items: dataForGrid
};
var store = new dojo.data.ItemFileReadStore({
data: data
});
grid.setStore(store);
}

Related

JQuery - disable false and reset value from an input using css class and .each loop

Im trying to make a function but the alerts that i put in my each loop are not displayed. This function has to disabled:false and .val("") every input with a specific class. The function is working but the problem is that the function is not going in the each loop and i dont know why. The first alert() is executed but not the second.
The function :
function resetNivPrep(){
alert("test");
// Déclaré
//LDQL
$(".5001").each(function(){
alert("test1");
$(this).attr('disabled',false);
$(this).val("");
});
};
The HTML :
<div id="tableNiveau" class="declare">
<table id="tableNivPrep" class="tabData" border="0" style="display:block">
<thead>
<tr class="entete">
<th colspan="2" rowspan="2" class="entete">Préparation</th>
<th colspan="2" class="entete">Déclaré</th>
<th colspan="2" rowspan="2" class="entete">Option</th>
<th colspan="2" rowspan="2" class="entete" style="width:20%">Offre grand compte</th>
</tr>
<tr class="entete">
<th class="entete">Exemplaires</th>
<th class="entete">Paquets</th>
</tr>
</thead>
<tbody>
Edition LDQLChoix de l'option
function traitementPublissimo(){
//PECTMA - 563 - Nico ( grâce a chithakone )
var nivServ = $('#niv_service_const').val();
var idContrat = $('#num_contrat_const').val();
idContrat = idContrat.replace(/\s+/g, '');
var numCppap = $('#num_cppap').val();
var strNumCppap = numCppap.substr(0,3);
// Ajax
$.ajax({
type : "POST",
url : "/gestion/gestDepot/ajaxgetnumcontrat",
data: {idContrat: idContrat},
async : false,
success : function(result){
var reponse = $.parseJSON(result);
var str = JSON.stringify(reponse);
console.log(str);
if(strNumCppap == "AIP"){
resetNivPrep();
// Déclaré
$("#exemplaire_50001_0").attr('disabled',true);
$("#exemplaire_50002_0").attr('disabled',true);
$("#exemplaire_50003_1").attr('disabled',true);
$("#paquet_50003_1").attr('disabled',true);
$("#exemplaire_50004_0").attr('disabled',true);
$("#exemplaire_50005_0").attr('disabled',false);
// Constaté
$("#exemplaire_const_50001_0").attr('disabled',true);
$("#exemplaire_const_50002_0").attr('disabled',true);
$("#exemplaire_const_50003_1").attr('disabled',true);
$("#paquet_const_50003_1").attr('disabled',true);
$("#exemplaire_const_50004_0").attr('disabled',true);
$("#exemplaire_const_50005_0").attr('disabled',false);
}
How i trigger the action :
$( "#num_cppap" ).focusout(function() {
traitementPublissimo();
});
$( "#niv_service_const" ).focusout(function() {
traitementPublissimo();
});
$( "#num_contrat_const" ).focusout(function() {
traitementPublissimo();
});
instead of $("#exemplaire_50005_0").attr('disabled',false);
use $("#exemplaire_50005_0").removeAttr('disabled');
Everywhere

Create an array of objects by iterating through table rows

I have an HTML table and I want to iterate through its rows and create a collection or lets say an "array of objects".
For example:
<table id="tbPermission">
<tr>
<th>User ID</th>
<th>User Name</th>
</tr>
<tr>
<td>1</td>
<td>Test1</td>
</tr>
</table>
I want to create a collection as below:
var trArray = [];
$('#tbPermission tr').each(function () {
var tdArray = [];
$(this).find('td').each(function () {
// I want to create the array of objects here …
tdArray.push();
});
// Final array
trArray.push(tdArray);
});
The arrays may be like below:
tdArray : {'UserID' : '1', 'UserName' : 'Test1'};
and:
trArray : [
{'UserID' : '1', 'UserName' : 'Test1'},
{'UserID' : '2', 'UserName' : 'Test2'}
]
Try this code
var trArray = [];
$('#tbPermission tr').each(function () {
var tr =$(this).text(); //get current tr's text
var tdArray = [];
$(this).find('td').each(function () {
var td = $(this).text(); //get current td's text
var items = {}; //create an empty object
items[tr] = td; // add elements to object
tdArray.push(items); //push the object to array
});
});
Here, I just created an empty object, filled object with references of tr and td, the added that object to the final array.
adding a working jsfiddle
This solution relies on adding thead and tbody elements which is a good idea anyways since it indicates to the browser that the table actually is a "data" table and not presentational.
jQuery has a .map() function. map is a basic function where you take an array and then replace the values with a the return value of a callback function - which results in a new array.
$([1,4,9]).map(function(){ return Math.sqrt(this) });
// [1, 2, 3]
.toArray converts the array like jQuery object we get into a "true array".
jQuery(function(){
var $table = $("#tbPermission");
var headers = $table.find('thead th').map(function(){
return $(this).text().replace(' ', '');
});
var rows = $table.find('tbody tr').map(function(){
var result = {};
var values = $(this).find('>td').map(function(){
return $(this).text();
});
// use the headers for keys and td values for values
for (var i = 0; i < headers.length; i++) {
result[headers[i]] = values[i];
}
// If you are using Underscore/Lodash you could replace this with
// return _.object(headers, values);
return result;
}).toArray();
// just for demo purposes
$('#test').text(JSON.stringify(rows));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tbPermission">
<thead>
<tr>
<th>User ID</th>
<th>User Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Test1</td>
</tr>
</tbody>
</table>
<textarea id="test"></textarea>
If you for whatever reason cannot change the HTML you could use the index of the rows to differentiate between headers and rows of data:
var headers = $table.find('tr:eq(0) th').map(function(){
return $(this).text().replace(' ', '');
});
var rows = $table.find('tr:gt(0)').map(function(){
// ...
});
I would suggest changing your html slightly.
<table id="tbPermission">
<tr>
<th>User ID</th>
<th>User Name</th>
</tr>
<tr>
<td class="userid">1</td>
<td class="username">Test1</td>
</tr>
</table>
Then in your javascript when you want to get all the elements as an array you could do.
var userIdArray = $('#tbPermission .userid').map(function(userid){ return $(userid).html(); }).toArray();
This will find all elements with a class userid on the table, collect just the values, and .toArray() that result to get a basic javascript array. You can then take that and manipulate it into whatever json structure you want, or you could possibly create your json object inside that map function.
Check the console, you will get an array with the desired objects
var arr = [];
$('#tbPermission tr:not(.header)').each(function() {
var that = $(this);
var id = that.find('td').eq(0).text();
var name = that.find('td').eq(1).text();
var obj = { 'userId': id , 'userName': name };
arr.push(obj);
});
console.log(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="tbPermission">
<tr class="header">
<th>User ID</th>
<th>User Name</th>
</tr>
<tr>
<td>1</td>
<td>Test1</td>
</tr>
<tr>
<td>2</td>
<td>Test2</td>
</tr>
</table>
It's a bit tricky based on the given structure. You may have to modify the HTML a bit to map cells to headers, like below.
var myArray = [];
$("#tbPermission").find("td").each(function() {
var $this = $(this), obj = {};
obj[$this.data("column")] = $this.text();
myArray.push(obj);
});
alert ( JSON.stringify(myArray) );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tbPermission">
<tr>
<th>User ID</th>
<th>User Name</th>
</tr>
<tr>
<td data-column="User ID">1</td>
<td data-column="User Name">Test1</td>
</tr>
</table>
Please give in some time to learn about Array.push() and Objects in Javascript. Hope that helps.

Filtering table rows upon Search

I am trying to filter the rows of a table to display the results of entered text in the search bar. The code below does the job but for some reason filters the column headings as well.
$('#search').keyup(function () {
var data = this.value.split(" ");
var rows = $(".Info").find("tr").hide();
if(this.value ==""){
rows.show();
return;
}
rows.hide();
rows.filter(function(i,v){
var t = $(this);
for (var d = 0; d < data.length; d++) {
if (t.is(":Contains('" + data[d] + "')")) {
return true;
}
}
return false;
}).show();
});
HTML
<input type = "search" name = "search" id = "search">
<table style ="width:95%" class = "Info">
<tr>
<th>Select </th>
<th>Name</th>
<th>Number</th>
<th>Date</th>
</tr>
</table>
The user adds rows which is why i haven't written any HTML for it.
Any help would be appreciated. Thanks in advance
http://jsfiddle.net/szjhngwm/
It looks like you need to filter using tbody
<table style ="width:95%" class = "Info">
<thead>
<tr>
<th>Select </th>
<th>Name</th>
<th>Number</th>
<th>Date</th>
</tr>
<thead>
<tbody>
</tbody>
</table>
var rows = $(".Info tbody tr").hide();
Another way to do this would be to use jQuery's :gt() selector.
The only thing that would change is this line:
var rows = $(".Info").find("tr:gt(0)").hide();
Notice the addition of :gt(0) to your tr.

bootstrap table unable to load json objects

My ajax script sends json objects to browser but the table is unable to load the json object.
My Ajax script:
$.ajax({
type : "POST",
url : "getLabels.jsp",
data : "mailingID=" + selectedValue, // posCodeSelected
success : function(data) {
response = $.parseJSON(data);// this statement sends data succesfully to browser
},
error : function(response) {
var responseTextObject = jQuery.parseJSON(response.responseText);
}
});
This is my bootstrap table embedded into my jsp page.
<table data-height="299" data-show-refresh="true" data-show-toggle="true" data-show-columns="true" data-search="true" data-select-item-name="toolbar1">
<thead>
<tr>
<th data-field="rowNumber" >ID</th>
<th data-field="firstname" >first name</th>
<th data-field="lastname" >last name</th>
<th data-field="organization" >organization</th>
<th data-field="city" >city</th>
<th data-field="state" >state</th>
</tr>
</thead>
</table>
Just to let you guys now this is my json response in browser:
{"rowNumber":1,"mailingID":3,"firstname":"Brian","lastname":"Fairhurst","organization":"Florida State University","city":"Tallahassee","state":"FL"}
You have to add a new row with the json object returned:
//you need to set id="tbl" to your table on the html
var table = document.getElementById("tbl");
var row = table.insertRow(table.rows.length); //insert a new row
// insert new cells with info
cells = [];
for(var i = 0;i < 6;i++){
cells[i] = row.insertCell(i);
}
// add the information store in json object
cells[0].innerHTML = response.rowNumber;
cells[0].innerHTML = response.firstname;
cells[0].innerHTML = response.lastname;
cells[0].innerHTML = response.organization;
cells[0].innerHTML = response.city;
cells[0].innerHTML = response.state;
The problem is that the HTML table isn't being populated with the data in response. Adding the data-url attribute to your HTML table should work.
So, instead of:
<table data-height="299" data-show-refresh="true" ...>
<thead>
...
You'll want to do:
<table data-height="299" data-show-refresh="true" data-url="response"...>
<thead>
...

toggle between adding and removing an element

This is a Fiddle Example(updated) of adding extra rows to the tablesorter via AJAX
I'm trying to add a function that allows people to toggle between adding and removing the same element on click. Like when you click on Class A, if it doesn't exist in the table, it will be added to the table, if exists, removed and return false. I came up with this code to check if the button's data attribute matches a row's class which is distinctive as it uses item.title from their own JSON file.
$('.area button').click(function(){
var dataterm = $(this).data('term');
if($('.tablesorter tbody tr.'+dataterm).length)
{
return false;
$('.tablesorter tbody tr.'+dataterm).remove();
}
But it isn't working. Could anyone show me how to do that?
var ajax_request;
function add_Data() {
$('.area button').click(function(){
var dataterm = $(this).data('term');
if($('.tablesorter tbody tr.'+dataterm).length)
{
return false;
$('.tablesorter tbody tr.'+dataterm).remove();
}
var source = $(this).data('feed');
$.ajax({
url: source,
success: function (data) {
$(data.query.results.json.json).each(function (index, item) {
var title = item.title,
year = item.year,
job = item.Job,
education = item.Education,
background = item.Background,
ingredient = item.Ingredient;
$(".tablesorter tbody").append('<tr style="display:table-row;" class="'+title+' trremove lastadded' + $(".tablesorter tr.trremove").length + '"><td>'+education+'</td><td>'+background+'</td><td>'+job+'</td><td>'+ingredient+'</td><td>'+year+'</td><td>'+background+'</td><td>'+year+'</td></tr>');
});
},
});
$("table").trigger("update");
var sorting = [[2,1],[0,0]];
$(".tablesorter").trigger("sorton",[sorting]);
});
return false;
};
add_Data();
$('.undo').click(function(){
$('.lastadded' + ($(".tablesorter tr.trremove").length - 2)).remove();
});
$('.remove').click(function(){
$('.trremove').remove();
$(".tablesorter").trigger("update");
});
HTML
<div class="area"><button data-term="A">Class A</button></div>
<div class="area"><button data-term="C">Class C</button></div>
<div class="area"><button data-term="D">Class D</button></div>
<table class="tablesorter" cellspacing="1">
<thead>
<tr>
<th style="visibility:hidden;">first name</th>
<th>first name</th>
<th>last name</th>
<th>age</th>
<th>total</th>
<th>discount</th>
<th>date</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
You have a return false before executing your .remove() method on the found rows.
It should be
if($('.tablesorter tbody tr.'+dataterm).length)
{
$('.tablesorter tbody tr.'+dataterm).remove();
} else {
...
}
Here's the update to your Fiddle.

Categories

Resources