Filter only specific data from Json source Data tables

Filter only specific data from Json source Data tables

deepdicedeepdice Posts: 7Questions: 2Answers: 0
edited November 2019 in Free community support

Here is the data
https://api.myjson.com/bins/198yru?pretty=1
I want to only display those fields in my datatable
where condition game is dice

"game": "dice"

Currently i am showing my data as this

var table;
              table = $('#data-table').DataTable({
                 "data" : data,
                 columns : [
                  {"data" : "id"},
                  {"data" : "amount"},
                  {"data" : "currency"},
                  {"data" : "stateDice.condition"},
                  {"data" : "payoutMultiplier",
                    render: function ( data, type, row ) {
                           return (data).toFixed(2) + "x";
                  }},
                  {"data" : "stateDice.result",
                  render: function ( data, type, row ) {
                             return (data).toFixed(2) + "";
                              } },
                  {"data" : "nonce"}
                 ]
            });

This is good for json source having all data with game dice. but if json source has other data also like json url mentioned above it gives errors.

any way i can only filter data with game dice.

Answers

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921

    but if json source has other data also like json url mentioned above it gives errors.

    What are the errors?

    The best place to filter the data, if you can, is at the server with the server only returning the data you want. Since you are using "data" : data, then this suggests you already have the data as a Javascript variable. You can use a Javascript looping mechanism to build a new array with the dice game objects you want and use something like "data" : newData, .

    Kevin

  • deepdicedeepdice Posts: 7Questions: 2Answers: 0

    @kthorngren

    What are the errors?

    DataTables warning: table id=data-table - Requested unknown parameter 'stateDice.condition' for row 1, column 3.

    As only dice game objects have stateDice parameter others games don't have that

    You can use a Javascript looping mechanism to build a new array with the dice game objects you want and use something like "data" : newData,

    will look into that any head up how i can do that i don't know much.

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • deepdicedeepdice Posts: 7Questions: 2Answers: 0

    @colin Sorry didn't knew about those rules. will create some test cases and update here

  • deepdicedeepdice Posts: 7Questions: 2Answers: 0

    @colin can you have a look at this ? link

    you can test with 2 url's in getJSON
    1. https://api.myjson.com/bins/ql2i2?pretty=1 (Json with only game : dice )
    2. https://api.myjson.com/bins/xsex6?pretty=1 (JSON with mix game types)

    As @kthorngren said above i need to use some loop mechanism.
    But i don't know how i can process data after pulling it from json source.

    any references to manuals is welcomed.
    Hope now you all have some clear view on my problem .

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921

    use a Javascript for loop. I took your data to show an example:
    http://live.datatables.net/nucojado/1/edit

    Kevin

This discussion has been closed.