diff --git a/docs/demos/27-load-json/additional_data.json b/docs/demos/27-load-json/additional_data.json new file mode 100644 index 0000000..89505b9 --- /dev/null +++ b/docs/demos/27-load-json/additional_data.json @@ -0,0 +1,306 @@ +[{ + "name": "Lareina Jones", + "extension": "8642", + "city": "East Linton", + "start_date": "2009/07/08" +}, +{ + "name": "Joshua Weiss", + "extension": "2289", + "city": "Saint-Léonard", + "start_date": "2010/15/01" +}, +{ + "name": "Kiona Lowery", + "extension": "5952", + "city": "Inuvik", + "start_date": "2002/17/12" +}, +{ + "name": "Nina Rush", + "extension": "7567", + "city": "Bo‘lhe", + "start_date": "2008/27/01" +}, +{ + "name": "Palmer Parker", + "extension": "2000", + "city": "Stade", + "start_date": "2012/24/07" +}, +{ + "name": "Vielka Olsen", + "extension": "3745", + "city": "Vrasene", + "start_date": "2016/08/01" +}, +{ + "name": "Meghan Cunningham", + "extension": "8604", + "city": "Söke", + "start_date": "2007/16/02" +}, +{ + "name": "Iola Shaw", + "extension": "6447", + "city": "Albany", + "start_date": "2014/05/03" +}, +{ + "name": "Imelda Cole", + "extension": "4564", + "city": "Haasdonk", + "start_date": "2007/16/11" +}, +{ + "name": "Jerry Beach", + "extension": "6801", + "city": "Gattatico", + "start_date": "1999/07/07" +}, +{ + "name": "Garrett Rocha", + "extension": "3938", + "city": "Gavorrano", + "start_date": "2000/06/08" +}, +{ + "name": "Derek Kerr", + "extension": "1724", + "city": "Gualdo Cattaneo", + "start_date": "2014/21/01" +}, +{ + "name": "Shad Hudson", + "extension": "5944", + "city": "Salamanca", + "start_date": "2014/10/12" +}, +{ + "name": "Daryl Ayers", + "extension": "8276", + "city": "Barchi", + "start_date": "2012/12/11" +}, +{ + "name": "Caleb Livingston", + "extension": "3094", + "city": "Fatehpur", + "start_date": "2014/13/02" +}, +{ + "name": "Sydney Meyer", + "extension": "4576", + "city": "Neubrandenburg", + "start_date": "2015/06/02" +}, +{ + "name": "Lani Lawrence", + "extension": "8501", + "city": "Turnhout", + "start_date": "2008/07/05" +}, +{ + "name": "Allegra Shepherd", + "extension": "2576", + "city": "Meeuwen-Gruitrode", + "start_date": "2004/19/04" +}, +{ + "name": "Fallon Reyes", + "extension": "3178", + "city": "Monceau-sur-Sambre", + "start_date": "2005/15/02" +}, +{ + "name": "Karen Whitley", + "extension": "4357", + "city": "Sluis", + "start_date": "2003/02/05" +}, +{ + "name": "Stewart Stephenson", + "extension": "5350", + "city": "Villa Faraldi", + "start_date": "2003/05/07" +}, +{ + "name": "Ursula Reynolds", + "extension": "7544", + "city": "Southampton", + "start_date": "1999/16/12" +}, +{ + "name": "Adrienne Winters", + "extension": "4425", + "city": "Laguna Blanca", + "start_date": "2014/15/09" +}, +{ + "name": "Francesca Brock", + "extension": "1337", + "city": "Oban", + "start_date": "2000/12/06" +}, +{ + "name": "Ursa Davenport", + "extension": "7629", + "city": "New Plymouth", + "start_date": "2013/27/06" +}, +{ + "name": "Mark Brock", + "extension": "3310", + "city": "Veenendaal", + "start_date": "2006/08/09" +}, +{ + "name": "Dale Rush", + "extension": "5050", + "city": "Chicoutimi", + "start_date": "2000/27/03" +}, +{ + "name": "Shellie Murphy", + "extension": "3845", + "city": "Marlborough", + "start_date": "2013/13/11" +}, +{ + "name": "Porter Nicholson", + "extension": "4539", + "city": "Bismil", + "start_date": "2012/22/10" +}, +{ + "name": "Oliver Huber", + "extension": "1265", + "city": "Hannche", + "start_date": "2002/11/01" +}, +{ + "name": "Calista Maynard", + "extension": "3315", + "city": "Pozzuolo del Friuli", + "start_date": "2006/23/03" +}, +{ + "name": "Lois Vargas", + "extension": "6825", + "city": "Cumberland", + "start_date": "1999/25/04" +}, +{ + "name": "Hermione Dickson", + "extension": "2785", + "city": "Woodstock", + "start_date": "2001/22/03" +}, +{ + "name": "Dalton Jennings", + "extension": "5416", + "city": "Dudzele", + "start_date": "2015/09/02" +}, +{ + "name": "Cathleen Kramer", + "extension": "3380", + "city": "Crowsnest Pass", + "start_date": "2012/27/07" +}, +{ + "name": "Zachery Morgan", + "extension": "6730", + "city": "Collines-de-l'Outaouais", + "start_date": "2006/04/09" +}, +{ + "name": "Yoko Freeman", + "extension": "4077", + "city": "Lidköping", + "start_date": "2002/27/12" +}, +{ + "name": "Chaim Waller", + "extension": "4240", + "city": "North Shore", + "start_date": "2010/25/07" +}, +{ + "name": "Berk Johnston", + "extension": "4532", + "city": "Vergnies", + "start_date": "2016/23/02" +}, +{ + "name": "Tad Munoz", + "extension": "2902", + "city": "Saint-Nazaire", + "start_date": "2010/09/05" +}, +{ + "name": "Vivien Dominguez", + "extension": "5653", + "city": "Bargagli", + "start_date": "2001/09/01" +}, +{ + "name": "Carissa Lara", + "extension": "3241", + "city": "Sherborne", + "start_date": "2015/07/12" +}, +{ + "name": "Hammett Gordon", + "extension": "8101", + "city": "Wah", + "start_date": "1998/06/09" +}, +{ + "name": "Walker Nixon", + "extension": "6901", + "city": "Metz", + "start_date": "2011/12/11" +}, +{ + "name": "Nathan Espinoza", + "extension": "5956", + "city": "Strathcona County", + "start_date": "2002/25/01" +}, +{ + "name": "Kelly Cameron", + "extension": "4836", + "city": "Fontaine-Valmont", + "start_date": "1999/02/07" +}, +{ + "name": "Kyra Moses", + "extension": "3796", + "city": "Quenast", + "start_date": "1998/07/07" +}, +{ + "name": "Grace Bishop", + "extension": "8340", + "city": "Rodez", + "start_date": "2012/02/10" +}, +{ + "name": "Haviva Hernandez", + "extension": "8136", + "city": "Suwałki", + "start_date": "2000/30/01" +}, +{ + "name": "Alisa Horn", + "extension": "9853", + "city": "Ucluelet", + "start_date": "2007/01/11" +}, +{ + "name": "Zelenia Roman", + "extension": "7516", + "city": "Redwater", + "start_date": "2012/03/03" +}] \ No newline at end of file diff --git a/docs/demos/27-load-json/index.html b/docs/demos/27-load-json/index.html new file mode 100644 index 0000000..bdf3b46 --- /dev/null +++ b/docs/demos/27-load-json/index.html @@ -0,0 +1,71 @@ + + + + + + + Load JSON - simple-datatables + + + + + + +
+

+ simple-datatables +

+ Documentation + Demos +
+ +

Load JSON

+ +
+ + + + diff --git a/docs/demos/27-load-json/initial_data.json b/docs/demos/27-load-json/initial_data.json new file mode 100644 index 0000000..7fdc121 --- /dev/null +++ b/docs/demos/27-load-json/initial_data.json @@ -0,0 +1,296 @@ +[ + { + "name": "Unity Pugh", + "extension": "9958", + "city": "Curicó", + "start_date": "2005/02/11" + }, + { + "name": "Theodore Duran", + "extension": "8971", + "city": "Dhanbad", + "start_date": "1999/04/07" + }, + { + "name": "Kylie Bishop", + "extension": "3147", + "city": "Norman", + "start_date": "2005/09/08" + }, + { + "name": "Willow Gilliam", + "extension": "3497", + "city": "Amqui", + "start_date": "2009/29/11" + }, + { + "name": "Blossom Dickerson", + "extension": "5018", + "city": "Kempten", + "start_date": "2006/11/09" + }, + { + "name": "Elliott Snyder", + "extension": "3925", + "city": "Enines", + "start_date": "2006/03/08" + }, + { + "name": "Castor Pugh", + "extension": "9488", + "city": "Neath", + "start_date": "2014/23/12" + }, + { + "name": "Pearl Carlson", + "extension": "6231", + "city": "Cobourg", + "start_date": "2014/31/08" + }, + { + "name": "Deirdre Bridges", + "extension": "1579", + "city": "Eberswalde-Finow", + "start_date": "2014/26/08" + }, + { + "name": "Daniel Baldwin", + "extension": "6095", + "city": "Moircy", + "start_date": "2000/11/01" + }, + { + "name": "Phelan Kane", + "extension": "9519", + "city": "Germersheim", + "start_date": "1999/16/04" + }, + { + "name": "Quentin Salas", + "extension": "1339", + "city": "Los Andes", + "start_date": "2011/26/01" + }, + { + "name": "Armand Suarez", + "extension": "6583", + "city": "Funtua", + "start_date": "1999/06/11" + }, + { + "name": "Gretchen Rogers", + "extension": "5393", + "city": "Moxhe", + "start_date": "1998/26/10" + }, + { + "name": "Harding Thompson", + "extension": "2824", + "city": "Abeokuta", + "start_date": "2008/06/08" + }, + { + "name": "Mira Rocha", + "extension": "4393", + "city": "Port Harcourt", + "start_date": "2002/04/10" + }, + { + "name": "Drew Phillips", + "extension": "2931", + "city": "Goes", + "start_date": "2011/18/10" + }, + { + "name": "Emerald Warner", + "extension": "6205", + "city": "Chiavari", + "start_date": "2002/08/04" + }, + { + "name": "Colin Burch", + "extension": "7457", + "city": "Anamur", + "start_date": "2004/02/01" + }, + { + "name": "Russell Haynes", + "extension": "8916", + "city": "Frascati", + "start_date": "2015/28/04" + }, + { + "name": "Brennan Brooks", + "extension": "9011", + "city": "Olmué", + "start_date": "2000/18/04" + }, + { + "name": "Kane Anthony", + "extension": "8075", + "city": "LaSalle", + "start_date": "2006/21/05" + }, + { + "name": "Scarlett Hurst", + "extension": "1019", + "city": "Brampton", + "start_date": "2015/07/01" + }, + { + "name": "James Scott", + "extension": "3008", + "city": "Meux", + "start_date": "2007/30/05" + }, + { + "name": "Desiree Ferguson", + "extension": "9054", + "city": "Gojra", + "start_date": "2009/15/02" + }, + { + "name": "Elaine Bishop", + "extension": "9160", + "city": "Petrópolis", + "start_date": "2008/23/12" + }, + { + "name": "Hilda Nelson", + "extension": "6307", + "city": "Posina", + "start_date": "2004/23/05" + }, + { + "name": "Evangeline Beasley", + "extension": "3820", + "city": "Caplan", + "start_date": "2009/12/03" + }, + { + "name": "Wyatt Riley", + "extension": "5694", + "city": "Cavaion Veronese", + "start_date": "2012/19/02" + }, + { + "name": "Wyatt Mccarthy", + "extension": "3547", + "city": "Patan", + "start_date": "2014/23/06" + }, + { + "name": "Cairo Rice", + "extension": "6273", + "city": "Ostra Vetere", + "start_date": "2016/27/02" + }, + { + "name": "Sylvia Peters", + "extension": "6829", + "city": "Arrah", + "start_date": "2015/03/02" + }, + { + "name": "Kasper Craig", + "extension": "5515", + "city": "Firenze", + "start_date": "2015/26/04" + }, + { + "name": "Leigh Ruiz", + "extension": "5112", + "city": "Lac Ste. Anne", + "start_date": "2001/09/02" + }, + { + "name": "Athena Aguirre", + "extension": "5741", + "city": "Romeral", + "start_date": "2010/24/03" + }, + { + "name": "Riley Nunez", + "extension": "5533", + "city": "Sart-Eustache", + "start_date": "2003/26/02" + }, + { + "name": "Lois Talley", + "extension": "9393", + "city": "Dorchester", + "start_date": "2014/05/01" + }, + { + "name": "Hop Bass", + "extension": "1024", + "city": "Westerlo", + "start_date": "2012/25/09" + }, + { + "name": "Kalia Diaz", + "extension": "9184", + "city": "Ichalkaranji", + "start_date": "2013/26/06" + }, + { + "name": "Maia Pate", + "extension": "6682", + "city": "Louvain-la-Neuve", + "start_date": "2011/23/04" + }, + { + "name": "Macaulay Pruitt", + "extension": "4457", + "city": "Fraser-Fort George", + "start_date": "2015/03/08" + }, + { + "name": "Danielle Oconnor", + "extension": "9464", + "city": "Neuwied", + "start_date": "2001/05/10" + }, + { + "name": "Kato Carr", + "extension": "4842", + "city": "Faridabad", + "start_date": "2012/11/05" + }, + { + "name": "Malachi Mejia", + "extension": "7133", + "city": "Vorst", + "start_date": "2007/25/04" + }, + { + "name": "Dominic Carver", + "extension": "3476", + "city": "Pointe-aux-Trembles", + "start_date": "2014/14/03" + }, + { + "name": "Paki Santos", + "extension": "4424", + "city": "Cache Creek", + "start_date": "2001/18/11" + }, + { + "name": "Ross Hodges", + "extension": "1862", + "city": "Trazegnies", + "start_date": "2010/19/09" + }, + { + "name": "Hilda Whitley", + "extension": "3514", + "city": "New Sarepta", + "start_date": "2011/05/07" + }, + { + "name": "Roth Cherry", + "extension": "4006", + "city": "Flin Flon", + "start_date": "2008/02/09" + } +] diff --git a/docs/demos/index.html b/docs/demos/index.html index 0c10f68..17f35c5 100644 --- a/docs/demos/index.html +++ b/docs/demos/index.html @@ -43,6 +43,7 @@

Demos

Custom footer Cell attributes Numeric sort + Load JSON diff --git a/src/datatable.ts b/src/datatable.ts index 1303ea3..b391a40 100644 --- a/src/datatable.ts +++ b/src/datatable.ts @@ -915,7 +915,7 @@ export class DataTable { {headings?: string[], data?: (inputRowType | inputCellType[])[]} | { [key: string]: inputCellType}[])) { let rows: dataRowType[] = [] if (Array.isArray(data)) { - const headings = this.data.headings.map((heading: headerCellType) => heading.text ?? String(heading.data)) + const headings = this.data.headings.map((heading: headerCellType) => heading.data ? String(heading.data) : heading.text) data.forEach((row, rIndex) => { const r: cellType[] = [] Object.entries(row).forEach(([heading, cell]) => { diff --git a/src/read_data.ts b/src/read_data.ts index dd36ee0..2daa450 100644 --- a/src/read_data.ts +++ b/src/read_data.ts @@ -230,15 +230,25 @@ export const readTableData = (dataOption: DataOption, dom: (HTMLTableElement | u } } if (dataOption.data) { + const headings = data.headings.map((heading: headerCellType) => heading.data ? String(heading.data) : heading.text) data.data = dataOption.data.map((row: inputRowType | inputCellType[]) => { let attributes: { [key: string]: string } let cells: inputCellType[] if (Array.isArray(row)) { attributes = {} cells = row - } else { + } else if (row.hasOwnProperty("cells") && Object.keys(row).every(key => ["cells", "attributes"].includes(key))) { attributes = row.attributes cells = row.cells + } else { + attributes = {} + cells = [] + Object.entries(row).forEach(([heading, cell]) => { + const index = headings.indexOf(heading) + if (index > -1) { + cells[index] = cell + } + }) } return { attributes,