I have framed an array like below
iArray = [true, true, false, false, false, false, false, false, true, true, true, false,
true, false, false, false, false, true]
Condtional check:
If anyone of the value in this array is false I will be showing an error message
else if everything is true I will be showing success message.
I tired below code to iterate, however couldn't frame the logic in it.
var boolIteration = iArray.split(',');
var i;
for (i = 0; i < boolIteration.length; ++i) {
//conditional check
}
I'm struggling to iterate the array using the above condition.
Can anyone point me in the right direction with an efficient solution.
No need for jQuery
if (iArray.indexOf(false) !== -1) {
// error
}
Also, as previous commenters have already pointed out, iArray is already an array, there's no need to use split on it.
The Array.prototype.indexOf is not available in Internet Explorer below 9. However, this functionality could be easily added with a matching algorithm. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf for compatibility and how to create a workaround.
iArray is already an array, so there is no need to split it again (split is a method for String, Arrays don't have it)
What you need to do is check the index of a false value, if it is there then there is a false value in the array.
using jQuery - the array indexOf is not used because of IE compatibility
iArray = [true, true, false, false, false, false, false, false, true, true, true, false,
true, false, false, false, false, true]
if($.inArray(false, iArray ) != -1){
//error
}
var iArray = [true, true, false, false, false, false, false, false, true, true,
true, false,true, false, false, false, false, true];
for (var i = 0; i < iArray.length; i++) {
if (iArray[i]) {
alert("success");
}
else {
alert("error");
}
}
Aternatives:
if (/false/i.test(iArray)) { }
or
if ( ''.replace.call(iArray,/true|,/g,'').length ) { }
The jQuery.inArray function is nice for checking to see if a particular value is contained within an array.
iArray = [true, true, false, false, false, false, false, false, true, true, true, false, true, false, false, false, false, true];
if ($.inArray(iArray, false) >= 0) {
// the value "false" is contained within the array, show an error message
}
Related
Is it possible to disable all cropper functionality and only enable cropper.zoom() for example?
If I disable cropper with cropper.disable() it disables everything.
From the source code it’s seems that this option isn’t supported:
// Disable (freeze) the cropper
disable() {
if (this.ready && !this.disabled) {
this.disabled = true;
addClass(this.cropper, CLASS_DISABLED);
}
return this;
},
Source:
https://github.com/fengyuanchen/cropperjs/blob/89f0b50c7f580135582a087cbf2417126b67d5fd/src/js/methods.js#L137
You can open an issue or do it yourself...
One possible way to do it is by setting these options like this:
autoCrop: false,
dragMode: 'move
This configuration might help:
config: any = {
dragMode: 'none',
highlight: false,
modal: false,
responsive: true,
scalable: true,
autoCrop: false,
center: false,
background: false,
zoomable: true,
zoomOnWheel: true,
};
Attempting to load data from server based off of multisearch/filters, but do sorting and paging on client. i have found a few posts that say to simply set the datatype back to the original value in the beforeSearch method.
i have attempted this and it does execute against the server and return the correct data. However, the second time i filter it seems to get the data from the server but then it gets wiped out on line 4429 in the jqgrid code shown below. Any idea on how to achieve this goal?
Ln 4429 : p.lastSelectedData = query.select();
grid definition:
$element
.jqGrid({
cmTemplate: { search: true, searchoptions: { attr: { placeholder: "filter..." }, clearSearch: true }, sortable: true, align: "center" },
colModel: [
{ name: "Id", key: true, hidden: true, searchoptions: { searchhidden: true, sopt: ["eq"] } },
{ name: "TransmissionId", label: "Transmission Id", searchoptions: { sopt: ["eq"] } },
{ name: "Name", label: "Name", searchoptions: { sopt: ["cn"] } }
],
datatype: "json",
loadonce: true,
height: "auto",
ignoreCase: true,
pager: $("#" + pagerId),
pgbuttons: true,
pginput: false,
rowList: [10, 25, 50],
rowNum: 10,
toppager: true,
url: "url here",
loadComplete: function (data) {
if (this.p.datatype === "json") {
setTimeout(function () {
$element.trigger("reloadGrid", [{ page: 1 }]);
}, 50);
}
return data;
},
viewrecords: true
})
.jqGrid("filterToolbar", {
autosearch: true,
defaultSearch: "cn",
beforeSearch: function () {
$element.setGridParam({ datatype: "json", page: 1 });
}
})
.jqGrid("navGrid", "#" + pagerId, {
edit: false,
add: false,
del: false,
refresh: false,
view: false,
cloneToTop: true
});
I have trimmed down the grid to try to verify it is not custom logic that is causing this issue. the grid does have multipleSearch on. but is not defined above.
If I understand correctly your question then you should use forceClientSorting: true in combination with loadonce: true. It force that the data loaded from the server will be sorted, filtered and paged locally directly after loading from the server. Thus you can remove loadComplete, which you use currently. See the answer for a demo.
Additionally, you can add searching property, where you include all options of filterToolbar and searchGrid:
searching: {
autosearch: true,
defaultSearch: "cn",
beforeSearch: function () {
var p = $(this).jqGrid("getGridParam"), // get reference to all parameters
filters = JSON.parse(p.postData.filters);
p.datatype = "json";
// one can here analyse filters, modify it or clear it as
p.postData.filters = "";
// one can set any other parameters, which will be sent to the server
// by using p.postData.param1 = "value1";
// which will sent param1=value1 to the server.
}
}
In general you can do the same inside your current beforeSearch too.
UPDATED: Probably you should use beforeProcessing callback additionally. beforeProcessing will be called directly after receiving the data from the server. Inside of the callback you can clean-up all p.postData.filters (to "") and p.search (change it from true to false). As the result jqGrid will not try to filter locally by postData.filters the data returned from the server.
I need this for angular gridster when I add new item so I know the dimension of the new element I'm adding (when there is no space for current element), but to simplify lets assume that I have 2 dimension array with value true or false and I want to search the first free space in array to find position x,y and width,height of free space. So far I have this:
var array = [
[false, false, false, false, false, false],
[false, false, false, false, false, false],
[false, false, false, false, false, false],
[false, false, false, true, true, true],
[false, false, false, true, true, true]
];
var place = {};
loop:
for (var i=0; i<array.length; i++) {
for (var j=0; j<array[i].length; j++) {
if (array[i][j] && !place.x && !place.y) {
place.x = j;
place.y = i;
place.width = 0;
place.height = 0;
for (var y=i; y<array.length; y++) {
for (var x=j; x<array[y].length; x++) {
if (array[y][x]) {
place.width = x - j + 1;
place.height = y - i + 1;
}
}
}
break loop;
}
}
}
console.log(place);
but this will fail for array like this:
var array = [
[false, false, false, false, false],
[false, false, false, false, false],
[false, false, false, false, false],
[true, true, false, true, true],
[true, true, false, true, true]
];
How can I fix my code to make it work for array like this? The result should be:
{x:0, y:3, width: 2, height: 2}
Here's another option. I tried to divide the problem in smaller ones:
Find the first true element in the matrix
From there, find the number of true elements adjacent to 1
Here's the fiddle and here's the code. Hope it helps.
function findCoords(matrix) {
for (var row = 0; row < matrix.length; row++) {
for (var col = 0; col < matrix[row].length; col++) {
if (matrix[row][col]) {
return createCoordsObj(row, col, matrix);
}
}
}
return null;
}
function createCoordsObj(row, col, matrix) {
return {
x: col,
y: row,
width: find('width', row, col, matrix),
height: find('height', row, col, matrix)
};
}
function find(type, row, col, matrix) {
var res = 0;
while (matrix[row] && matrix[row][col]) { // will finish when element in matrix is false || undefined
res += 1;
col += type === 'width' ? 1 : 0;
row += type === 'width' ? 0 : 1;
}
return res;
}
console.log(findCoords([
[false, false, false, false, false],
[false, false, false, false, false],
[false, false, false, false, false],
[true, true, false, true, true],
[true, true, false, true, true]
]));
I guess you might do as follows; It will return you the x and y coordinates of the upper left corner along with the width and height of the free opening. If there is no opening (all false) you will be returned a false.
var array = [
[false, false, false, false, false],
[false, false, false, false, false],
[false, false, true, true, true],
[false, false, true, true, true],
[false, false, true, true, true]
],
emptyXY = (a) => { var x,
y = a.findIndex(row => (x = row.findIndex(col => col), x !== -1));
w = 0,
h = 0;
while (a[y] && a[y][x+w]) w++;
while (a[y+h] && a[y+h][x]) h++;
return !!~y && {x:x,y:y,width:w,height:h};
};
console.log(emptyXY(array));
I receive data with this format:
var options = {
"dom": '<t"bottom"lp>',
"scrollY": "400",
"scrollCollapse": true,
"paging": false,
"searching": false,
"info": false,
'columns': head,
'data': tableModal,
"lengthChange": false,
}
tableModal come from PHP but I would like add letters after the data, in this case "s" because the data are seconds, but I don't know how to do it.
Thanks for help.
how about this
var options = {
"dom": '<t"bottom"lp>',
"scrollY": "400",
"scrollCollapse": true,
"paging": false,
"searching": false,
"info": false,
'columns': {s:'s'},
'data': [1,2],
"lengthChange": false,
}
options['datas']=options['data']
delete options['data']
Check out this example.
Below is the code snippet performing this operation of adding S.
function appendS(){
$.each(options.data, function(index, data){
options.data[index] = options.data[index]+'s';
})
}
If you are using this data on charts, please use appropriate formatter instead of going for this approach.
I have a datatable style in which I want to disable/enable the initial sort based on some filters in aspx.
The setting has a aasorting property, lets assume I have a global variable "isDefaultSortingEnabled" and based on this variable I want to perform the sorting. I tried using if-else, but we cant write it inside the style setting.
var objDataTableSettings = {
"bPaginate": false,
"bFilter": false,
"aaSorting": [] , // manipulate this sorting based on a global variable
// "aaSorting": [[1, 'asc']],
"bProcessing": true,
"aoColumnDefs": [
You can use a ternary expression within the object to set the aaSorting property based on your global variable. Try this:
var objDataTableSettings = {
"bPaginate": false,
"bFilter": false,
"bProcessing": true,
'aaSorting': isDefaultSortingEnabled ? [] : [[ 1, 'asc' ]];
// other settings...
}
If you prefer to use a full if/else statement, you would need to first create the object, then change the property as required:
var objDataTableSettings = {
"bPaginate": false,
"bFilter": false,
"bProcessing": true,
// other settings...
}
if (isDefaultSortingEnabled) {
objDataTableSettings.aaSorting = [];
} else {
objDataTableSettings.aaSorting = [[ 1, 'asc' ]];
}
The former is preferred due to its brevity.
if (isDefaultSortingEnabled) {
aaSortingdData = [];
} else {
aaSortingData = [[ 1, 'asc' ]];
}
var objDataTableSettings = {
"bPaginate": false,
"bFilter": false,
"bProcessing": true,
"aaSorting": aaSortingData
}