My current code look like this:
var that = $(this).closest('tr');
for (var i = 0; i < data.length; i++) {
that.next().find("[name='prod_code']").val(data[i]["prd_code"]);
that.next().find("[name='prod_desc']").val(data[i]["prd_desc"]);
that.next().find("[name='prod_qty']").val(data[i]["prd_qty"]);
}
What it does is to set the value from the array to the input inside a table.
But currently it can only set 1 row with the last element in the array.
I found out that if I add next() function repeatedly it can insert all the product in the table, but it kinda look dumb to do that manually.
My expectation of loop (I have tried but it doesn't work):
var that = $(this).closest('tr');
for (var i = 0; i < data.length; i++) {
var next = "next()";
that.next.find("[name='prod_code']").val(data[i]["prd_code"]);
that.next.find("[name='prod_desc']").val(data[i]["prd_desc"]);
that.next.find("[name='prod_qty']").val(data[i]["prd_qty"]);
next++;
}
Someone please point out what am I missing.
Thanks in advance!!
Given that you have the i index of the row which can be aligned with the index of the object within the array, then you can use eq() to find the tr by that index in order to update the form controls within it:
var $rows = $('#yourTable tbody tr');
for (var i = 0; i < data.length; i++) {
$rows.eq(i).find('[name="prod_code"]').val(data[i]['prd_code']);
$rows.eq(i).find('[name="prod_desc"]').val(data[i]['prd_desc']);
$rows.eq(i).find('[name="prod_qty"]').val(data[i]['prd_qty']);
}
Alternatively you could loop over the tr and use their index to find the object within the array:
$('#yourTable tbody tr').each((i, tr) => {
let $tr = $(tr);
$tr.find('[name="prod_code"]').val(data[i]['prd_code']);
$tr.find('[name="prod_desc"]').val(data[i]['prd_desc']);
$tr.find('[name="prod_qty"]').val(data[i]['prd_qty']);
});
Related
I try to implement an updateStyle(...)-method inside of an Angular-Component.
With this method specific elements with unique id's shall be styled.
Following code-snippet leads to:
Property 'variable' does not exist on type 'CSSStyleDeclaration'.
Is it possible to make angular compile anyway, so the variable is filled with a legit value in runtime or do I have to implement the method for every style-declaration, that I am going to use the method on?
updateStyle(id, variable, value){
var components = document.querySelectorAll("[id]") as NodeListOf<HTMLElement>;
for(var i = 0; i < components.length; i++) {
if(components[i].getAttribute("id") == id) {
components[i].style.variable = value;
}}}
You can put variable in square brackets like this:
updateStyle(id, variable, value) {
var components = document.querySelectorAll("[id]") as NodeListOf<HTMLElement>;
for (var i = 0; i < components.length; i++) {
if (components[i].getAttribute("id") == id) {
***components[i].style[variable] = value;***
}
}
}
I'm working on an automated slide setup and depending on some opt-out variables I need to remove some of the slides if they are not desired in the final output. To solve this I have created a script that adds a simple text string {{remove-this-slide}} to the slides that need to be deleted.
However, when trying to get a script to delete the slides containing that string it keeps deleting my entire presentation...
This is what I have:
function deleteFunction() {
var currentPresentationSlide = SlidesApp.getActivePresentation().getSlides();
for (i = 0; i < currentPresentationSlide.length; i++) {
if (currentPresentationSlide[i].getPageElements().indexOf('{{remove-this-slide}}') > -1); {
currentPresentationSlide[i].remove();
}
}
}
Can anyone figure out what's going wrong here?
How about this modification?
Modification points :
The reason that the entire slides are deleted is ; after if (currentPresentationSlide[i].getPageElements().indexOf('{{remove-this-slide}}') > -1);. By this ;, if doesn't work and currentPresentationSlide[i].remove(); is always run.
The text data cannot be retrieved from currentPresentationSlide[i].getPageElements(). When you want to search the text from the text box, please use currentPresentationSlide[i].getShapes().
From your question, I was not sure where you want to search the text from. So I supposed that you want to search the text from shapes. The shape includes the text box.
Modified script :
function deleteFunction() {
var currentPresentationSlide = SlidesApp.getActivePresentation().getSlides();
for (i = 0; i < currentPresentationSlide.length; i++) {
var shapes = currentPresentationSlide[i].getShapes();
for (j = 0; j < shapes.length; j++) {
if (shapes[j].getText().asString().indexOf('{{remove-this-slide}}') > -1) {
currentPresentationSlide[i].remove();
}
}
}
}
Reference :
getShapes()
If I misunderstand your question, I'm sorry.
There is a small bug in the code from #Tanaike. Because there might be more shapes in the same slide, you have to break the loop after deleting the slide.
Otherwise the code tries to traverse the shapes of a deleted slide, producing an error.
So the correct snippet looks like:
function deleteFunction() {
var currentPresentationSlide = SlidesApp.getActivePresentation().getSlides();
for (i = 0; i < currentPresentationSlide.length; i++) {
var shapes = currentPresentationSlide[i].getShapes();
for (j = 0; j < shapes.length; j++) {
if (shapes[j].getText().asString().indexOf('{{remove-this-slide}}') > -1) {
currentPresentationSlide[i].remove();
break;
}
}
}
}
I have this function for searching a table and displaying the rows that match my search input
function searchTable() {
var input, filter, found, table, tr, td, i, j;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td");
for (j = 0; j < td.length; j++) {
if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {
found = true;
}
}
if (found) {
tr[i].style.display = "";
found = false;
} else {
tr[i].style.display = "none";
}
}
}
I would like to make one of the strings in the data clickable with a hyper link.
However this affects my search because the the link is inside my table data . So even thought it is not visible it is skewing the search results.
What argument could I add to my function to ignore the contents of my bracket and still find the text string in my table. Or how could I place my anchor outside my table data brackets and still have the string be a clickable anchor.
Ex of table row:
<tr>
<td>Abcès au cerveau</td>
<td>Neurologique</td>
<td></td>
</tr>
Sorry beginner question. I am also looking for an argument to highlight the results of my search if anyone has it.
I have an observable array in my knockout js view model that binds to an html table in my view (razor view on MVC).
This table fills with items from another table that have an "add" button, so the user can add as many of the same item to the destination table. My problem is that I want to show in my another table only one for each item...so if the user adds Item1 30 times, the table need to show Item1 - 30 and not 30 times Item1.. but of course the observable array must have those 30 Item1 items.
How can I do that?
Try this code snippet where self.items is your original array with all the items.
self.unique = ko.computed(function() {
var values = ko.utils.arrayMap(self.items(), function(item){ return item.type})
return ko.utils.arrayGetDistinctValues(values).sort();
});
Try grouping the array by count like this
function GroupArrayByCount(arr) {
var a = [],
b = [],
prev, result = [];
arr.sort();
for (var i = 0; i < arr.length; i++) {
if (arr[i] !== prev) {
a.push(arr[i]);
b.push(1);
} else {
b[b.length - 1]++;
}
prev = arr[i];
}
for (var j = 0; j < a.length; j++) {
result.push({
name: a[j],
count: b[j]
});
}
return result;
}
Now call it in viewModel like this.
self.groupedItems = ko.computed(function(){
return GroupArrayByCount( self.items());
});
Fiddle: http://jsfiddle.net/codovations/edJwu/
I want to remove an item from my dataprovider where the label in dataprovide is "zee4"
I tried:
var removeThis:Object = mylist.dataProvider[selIndex].alias;
mylist.dataProvider.removeItem(removeThis);
But this does not work. I cannot use removeItemAt because I don't know the index number. I need to remove as per the alias itself. The alias is a unique field.
Try this:
var removeThis:Object = mylist.dataProvider[selIndex];
mylist.dataProvider.removeItem(removeThis);
The reason your method is not working, is because removeThis is not an item in dataProvider. Its a property in the item that is in dataProvider.
To remove by value, try iterating through the dataProvider and removing the Item when the comparison in true:
var removeThis:Object = mylist.dataProvider.getItemAt(selIndex).dataValue;
for (var i:int = 0; mylist.dataProvider.length > 0; i++)
{
if (mylist.dataProvider.getItemAt(i).dataValue == removeThis)
{
mylist.dataProvider.removeItemAt(i);
break;
}
}