I need help if it is possible to modify table data before load into table in Tabulator library. I need to convert decimal value of (8 poles)DIP switch to separate 8 bits and load it to table. I have data in json format like this:
JavaScript
x
5
1
[
2
{"id":1, "name":"DIP1", "value":15},
3
{"id":2, "name":"DIP2", "value":75}
4
]
5
and I would like format data to this (for decimal value 15):
JavaScript
1
2
1
[{"id":1, "name":"DIP1", "sw1":0,"sw2":0,"sw3":0,"sw4":0,"sw5":1,"sw6":1,"sw7":1,"sw8":1,}]
2
to this table:
JavaScript
1
17
17
1
columns:[
2
{ title:'ID', field:'id', width:50 },
3
{ title:'DIP NAME', field:'name', headerFilter:'input', editor:'input', hozAlign:'center' },
4
{ title:' DIP SWITCHES', hozAlign:'center',
5
columns:[
6
{ title:'SW1', field:'sw1', width:30, hozAlign:'center', editor:true, formatter:'tickCross', headerVertical:true, headerFilter:'tickCross', headerFilterParams:{"tristate":true}, headerSort:false },
7
{ title:'SW2', field:'sw2', width:30, hozAlign:'center', editor:true, formatter:'tickCross', headerVertical:true, headerFilter:'tickCross', headerFilterParams:{"tristate":true}, headerSort:false },
8
{ title:'SW3', field:'sw3', width:30, hozAlign:'center', editor:true, formatter:'tickCross', headerVertical:true, headerFilter:'tickCross', headerFilterParams:{"tristate":true}, headerSort:false },
9
{ title:'SW4', field:'sw4', width:30, hozAlign:'center', editor:true, formatter:'tickCross', headerVertical:true, headerFilter:'tickCross', headerFilterParams:{"tristate":true}, headerSort:false },
10
{ title:'SW5', field:'sw5', width:30, hozAlign:'center', editor:true, formatter:'tickCross', headerVertical:true, headerFilter:'tickCross', headerFilterParams:{"tristate":true}, headerSort:false },
11
{ title:'SW6', field:'sw6', width:30, hozAlign:'center', editor:true, formatter:'tickCross', headerVertical:true, headerFilter:'tickCross', headerFilterParams:{"tristate":true}, headerSort:false },
12
{ title:'SW7', field:'sw7', width:30, hozAlign:'center', editor:true, formatter:'tickCross', headerVertical:true, headerFilter:'tickCross', headerFilterParams:{"tristate":true}, headerSort:false },
13
{ title:'SW8', field:'sw8', width:30, hozAlign:'center', editor:true, formatter:'tickCross', headerVertical:true, headerFilter:'tickCross', headerFilterParams:{"tristate":true}, headerSort:false },
14
],
15
}
16
],
17
I know how to extract each bit in c:
JavaScript
1
9
1
var sw1 = bitRead( value, 7 );
2
var sw2 = bitRead( value, 6 );
3
var sw3 = bitRead( value, 5 );
4
var sw4 = bitRead( value, 4 );
5
var sw5 = bitRead( value, 3 );
6
var sw6 = bitRead( value, 2 );
7
var sw7 = bitRead( value, 1 );
8
var sw8 = bitRead( value, 0 );
9
but I dont know how to do this when data are loaded into table using ajax.
Can somebody help how to do it please?
I am newbie and I cant help myself.
Thank you!
Advertisement
Answer
You can spread the switches to a separate bit values like that:
JavaScript
1
36
36
1
// You may need to parse (JSON.parse()) if serialized
2
let data = [{
3
"id": 1,
4
"name": "DIP1",
5
"value": 15
6
},
7
{
8
"id": 2,
9
"name": "DIP2",
10
"value": 75
11
}
12
]
13
14
let transformed = data.map(({
15
value,
16
data
17
}, i) => {
18
// toString(2) transforms a number to a binary string
19
// PadStarts adds the zeros on left if neccessary
20
// split converts the string to array of 8 bits
21
let toBits = value.toString(2).padStart(8, "0").split("")
22
// this will create an object of eight bits with according values
23
.reduce((accum, bit, i) => {
24
accum["sw" + (i + 1)] = Number(bit)
25
return accum
26
}, {})
27
28
// spread operator will flatten the object
29
return {
30
id: i + 1,
31
data,
32
toBits,
33
}
34
})
35
36
console.log(transformed)
Then, you should be able to use the content transformed
as the table data like that (see http://tabulator.info/docs/4.9/data):
JavaScript
1
29
29
1
var table = new Tabulator("#example-table", {
2
// ...other options
3
ajaxResponse: function (url, params, response) {
4
//url - the URL of the request
5
//params - the parameters passed with the request
6
//response - the JSON object returned in the body of the response.
7
8
// `response` might be rather be `response.data` or such...
9
let transformed = response.map(({ value, data }) => {
10
let toBits = value
11
.toString(2)
12
.padStart(8, "0")
13
.split("")
14
.reduce((accum, bit, i) => {
15
accum["sw" + (i + 1)] = Number(bit);
16
return accum;
17
}, {});
18
19
return {
20
data,
21
toBits,
22
};
23
});
24
return transformed;
25
},
26
});
27
28
table.setData(<YOUR API URL>); // Change to your API endpoint
29