Android Question WebView tabel Click to retrieve the content of a row in the table

wuxuetie

Member
WebView tabel Click to retrieve the content of a row in the table
 

Attachments

  • 1.jpg
    1.jpg
    74.1 KB · Views: 68

jkhazraji

Active Member
Licensed User
Longtime User
Use WebViewExtras library
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Dim WebViewExtras1 As WebViewExtras
    Dim WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    WebView1.Initialize("WebView1")
    Activity.AddView(WebView1,0,0,-1,-1)
    'Simple HTML with 2x4 table
    Dim html As String = $"
    <html>
    <head>
    <style>
    table {
        width: 100%;
        border-collapse: collapse;
    }
    td, th {
        border: 1px solid #dddddd;
        text-align: left;
        padding: 8px;
    }
    tr:nth-child(even) {
        background-color: #f2f2f2;
    }
    </style>
    </head>
    <body>
    <table id='myTable'>
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
            <th>Header 3</th>
            <th>Header 4</th>
        </tr>
        <tr>
            <td>Row 1, Cell 1</td>
            <td>Row 1, Cell 2</td>
            <td>Row 1, Cell 3</td>
            <td>Row 1, Cell 4</td>
        </tr>
        <tr>
            <td>Row 2, Cell 1</td>
            <td>Row 2, Cell 2</td>
            <td>Row 2, Cell 3</td>
            <td>Row 2, Cell 4</td>
        </tr>
    </table>
    </body>
    </html>
    "$
   
    WebView1.LoadHtml(html)
    WebViewExtras1.addWebChromeClient(WebView1,"wcc")
    WebViewExtras1.addJavascriptInterface(WebView1,"B4A")
End Sub

Sub WebView1_PageFinished (Url As String)
    Dim js As String = $"
    var table = document.getElementById('myTable');
    var cells = table.getElementsByTagName('td');
   
    for (var i = 0; i < cells.length; i++) {
        cells[i].addEventListener('click', function() {
            var row = this.parentNode;
            var rowData = [];
            var cells = row.getElementsByTagName('td');
           
            for (var j = 0; j < cells.length; j++) {
                rowData.push(cells[j].innerText);
            }
           
            B4A.CallSub('row_clicked', true, row.rowIndex, JSON.stringify(rowData));
        });
    }
    "$
   
    WebViewExtras1.executeJavascript(WebView1, js)
End Sub

Sub row_clicked(rowIndex As Int, rowData As String)
    Log("Clicked row index: " & rowIndex)
    Log(rowData)
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Last edited:
Upvote 0

wuxuetie

Member
Use WebViewExtras library
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Dim WebViewExtras1 As WebViewExtras
    Dim WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    WebView1.Initialize("WebView1")
    Activity.AddView(WebView1,0,0,-1,-1)
    'Simple HTML with 2x4 table
    Dim html As String = $"
    <html>
    <head>
    <style>
    table {
        width: 100%;
        border-collapse: collapse;
    }
    td, th {
        border: 1px solid #dddddd;
        text-align: left;
        padding: 8px;
    }
    tr:nth-child(even) {
        background-color: #f2f2f2;
    }
    </style>
    </head>
    <body>
    <table id='myTable'>
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
            <th>Header 3</th>
            <th>Header 4</th>
        </tr>
        <tr>
            <td>Row 1, Cell 1</td>
            <td>Row 1, Cell 2</td>
            <td>Row 1, Cell 3</td>
            <td>Row 1, Cell 4</td>
        </tr>
        <tr>
            <td>Row 2, Cell 1</td>
            <td>Row 2, Cell 2</td>
            <td>Row 2, Cell 3</td>
            <td>Row 2, Cell 4</td>
        </tr>
    </table>
    </body>
    </html>
    "$
  
    WebView1.LoadHtml(html)
    WebViewExtras1.addWebChromeClient(WebView1,"wcc")
    WebViewExtras1.addJavascriptInterface(WebView1,"B4A")
End Sub

Sub WebView1_PageFinished (Url As String)
    Dim js As String = $"
    var table = document.getElementById('myTable');
    var cells = table.getElementsByTagName('td');
  
    for (var i = 0; i < cells.length; i++) {
        cells[i].addEventListener('click', function() {
            var row = this.parentNode;
            var rowData = [];
            var cells = row.getElementsByTagName('td');
          
            for (var j = 0; j < cells.length; j++) {
                rowData.push(cells[j].innerText);
            }
          
            B4A.CallSub('row_clicked', true, row.rowIndex, JSON.stringify(rowData));
        });
    }
    "$
  
    WebViewExtras1.executeJavascript(WebView1, js)
End Sub

Sub row_clicked(rowIndex As Int, rowData As String)
    Log("Clicked row index: " & rowIndex)
    Log(rowData)
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
thank you
WebViewExtras library
thank you!WebViewExtras library already found
 
Upvote 0
Top